Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Last active November 13, 2017 03:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewDaniels/c0dfc95c32781f235c4498ec9e3dd8ce to your computer and use it in GitHub Desktop.
Save MatthewDaniels/c0dfc95c32781f235c4498ec9e3dd8ce to your computer and use it in GitHub Desktop.
This script iterates the Google Analytics trackers on the page (assuming the Google Analytics variable is 'ga'. It then outputs various bits of info, including the tracker name, the tracking ID, the client ID, the cookie domain and any custom dimensions that exist. Copy and paste it into the console on the page you want to find out more about.
if(window.ga) {
var arr = window.ga.getAll();
for (var i = 0, len = arr.length; i < len; i++) {
var t = arr[i];
if(console.group) {
console.group(t.get('name'));
}
console.log('Main tracker object:', t);
// let's find the object that has the data object inside it so we can present more info
var trackerData;
for ( key in t ) {
if(typeof t[key] == 'object' && 'data' in t[key]) {
trackerData = t[key].data;
break;
}
}
if(trackerData) {
// the data var does not exist on the classic tracker
console.log('Data within tracker:', trackerData.values);
}
console.log('Tracker name:', t.get('name'));
console.log('Tracking ID:', t.get('trackingId'));
console.log('Client ID:', t.get('clientId'));
console.log('Cookie Domain:', t.get('cookieDomain'));
console.log('Allow Linker:', t.get('allowLinker'));
console.log('Linker Parameter:', t.get('linkerParam'));
if(t.get('&gtm')) {
console.log('GTM Container ID:', t.get('&gtm'));
}
// create an array of up to 20 dimensions
var dimensions = [];
for(var j = 0; j < 20; j++) {
var dimensionName = 'dimension' + (j+1),
dimensionVal = t.get(dimensionName);
if(dimensionVal) {
dimensions.push({name: 'Dimension ' + (j+1), val: dimensionVal});
}
}
// if there are any dimensions in the array - iterate them, echoing them out to a new group
if(dimensions.length > 0) {
// start a new (nested) group
if(console.groupCollapsed) {
console.groupCollapsed('Custom Dimensions');
} else if(console.group) {
console.group('Custom Dimensions');
}
for(var k=0,klen=dimensions.length; k < klen; k++) {
console.log(dimensions[k].name + ':', dimensions[k].val);
}
// end the nested group
if(console.groupEnd) {
console.groupEnd();
}
}
if(console.groupEnd) {
console.groupEnd();
}
}
} else {
console.error('Error: Google Analytics object not present or not named "ga".');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment