This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main(){ | |
// Set up the parameters and variables | |
var sheetName = '<name>'; // The name of the sheet (not the Spreadsheet) we want to write the data e.g Sheet1 | |
var tableId = '<table id>'; // The id of the view to query the data from e.g ga:123456 | |
var startDate = 'yyyy-MM-dd'; // The start date of the query with the appropriate format e.g 2018-04-01 (1 April 2018) | |
var endDate = 'yyyy-MM-dd'; // The end date of the query with the appropriate format e.g 2018-04-30 (30 April 2018) | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName(sheetName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gaGet(tableId, startDate, endDate, metrics, options) { | |
// Apply standard options | |
options = options || {}; | |
options['max-results'] = options['max-results'] || '10000'; | |
// If errors persist up to 5 times then terminate the program. | |
for (var i = 0; i < 5; i++) { | |
try { | |
return Analytics.Data.Ga.get(tableId, startDate, endDate, metrics, options); // 503 | |
} catch (err) { | |
// https://developers.google.com/analytics/devguides/reporting/core/v3/coreErrors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function errorTracking() { | |
window.onerror = function(msg, url, lineNo, columnNo, error) { | |
var errorStack = error ? JSON.stringify(error.stack) : 'stack unavailale'; | |
var content = [ | |
msg, url, lineNo, columnNo, errorStack | |
].join(' - '); | |
ga('send', 'event','error tracking', content); | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function trackGeolocation(){ | |
if (!window.navigator.geolocation) { | |
ga('send', 'event', 'browser support features', 'geolocation information', 'geolocation api is not supported'); | |
return; | |
} | |
function geoSuccess(position) { | |
ga('send', 'event', 'geolocation', 'position', position.coords.latitude + ',' + position.coords.longitude); | |
} | |
function geoError(positionError) { | |
ga('send', 'event', 'geolocation', 'error', positionError.message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function trackNetworkInformation(){ | |
// Check if network information is supported | |
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection; | |
if (!connection){ | |
ga('send', 'event', 'browser support features', 'network information', 'network information api is not supported'); | |
return; | |
} | |
var connectionType = connection.hasOwnProperty('type') ? 'type' : 'effectiveType'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function pageVisibility(){ | |
function initializeTracking(){ | |
//... | |
} | |
function handleVisibilityChange(event){ | |
if(document.hidden){ | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function collectBatteryUsage(stage){ | |
navigator.getBattery().then(function(batteryManagerData){ | |
ga('send', 'event', 'battery usage', stage, String(batteryManagerData.level)); | |
}); | |
} | |
(function trackBatteryUsage(){ | |
if(!window.Promise){ | |
ga('send', 'event', 'browser support features', 'Promises', 'unable to handle Promises'); | |
return; |