Skip to content

Instantly share code, notes, and snippets.

View dcvogi's full-sized avatar

Dimitris Vogiatzis dcvogi

View GitHub Profile
@dcvogi
dcvogi / trackBatteryUsage.js
Last active December 11, 2017 09:36
Tracks the battery on load and before unload
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;
(function pageVisibility(){
function initializeTracking(){
//...
}
function handleVisibilityChange(event){
if(document.hidden){
return;
}
@dcvogi
dcvogi / trackNetworkInformation.js
Last active December 6, 2017 07:46
Track the Network Information
(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';
(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);
@dcvogi
dcvogi / errorTracking.js
Last active October 18, 2018 21:14
Track the on-page errors on Google Analytics
(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;
@dcvogi
dcvogi / gaGet.js
Last active April 24, 2018 20:34
Apps Scripts template to query data from Google Analytics
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
@dcvogi
dcvogi / top5pages.js
Last active July 7, 2022 06:40
Queries the data for the top 5 pages of the website.
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);