Skip to content

Instantly share code, notes, and snippets.

@tjtate
Last active August 29, 2015 14:00
Show Gist options
  • Save tjtate/31626d69755f350ec649 to your computer and use it in GitHub Desktop.
Save tjtate/31626d69755f350ec649 to your computer and use it in GitHub Desktop.
Google Universal Analytics Primary and Secondary Tracking Code
//
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
/**
The afga class is used to handle all custom google analytics tracking across all web properties
@class afga
@constructor
*/
var afga = {
currentProperty : window.location.host,
currentGaid : '',
currentUserID : getParameterByName('userid'),
autoLinkArray : [],
afProperties : [
{
name : 'aftestingsite1.dev',
gaid : 'UA-XXXXXXXX-5'
},
{
name : 'aftestingsite2.dev',
gaid : 'UA-XXXXXXXX-6'
}
],
/**
The setSiteValues method is used to set the current google analytics (currentGaid)
property with the current site google analytics id and to set the auto link tracker array (autoLinkArray)
@method setSiteValues
*/
setSiteValues : function () {
for (var i = 0; i <= afga.afProperties.length - 1; i++) {
if (afga.currentProperty == afga.afProperties[i].name) {
afga.currentGaid = afga.afProperties[i].gaid;
}
else {
afga.autoLinkArray.push(afga.afProperties[i].name);
}
};
},
/**
The createPrimaryTracking method is used to set the primary "catch all" google analytics tracking
@method createPrimaryTracking
*/
createPrimaryTracking : function () {
ga('create', 'UA-XXXXXXXX-4', 'auto', {'name': 'primaryTracker', 'userId': afga.currentUserID, 'cookieName' : '_ga_primary'});
ga('primaryTracker.require', 'displayfeatures');
ga('primaryTracker.send', 'pageview', {
'dimension1' : afga.currentUserID
});
},
/**
The createSecondaryTracking method is used to set the "site specific" secondary google analytics tracking
@method createSecondaryTracking
*/
createSecondaryTracking : function () {
if (afga.currentGaid) {
//creating the secondary tracking objects
ga('create', afga.currentGaid, afga.currentProperty, {'name': 'secondaryTracker', 'userId': afga.currentUserID, 'cookieName' : '_ga_secondary'});
ga('secondaryTracker.require', 'displayfeatures');
ga('secondaryTracker.send', 'pageview', {
'dimension1' : afga.currentUserID
});
}
},
/**
The send method is used to call the send method on both analytics profiles
@method send
@params type {string} The type of send i.e. event
@params parameters {string or object} The parameters associated with the type reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
*/
send : function (type, parameters) {
ga('primaryTracker.send', type, parameters);
ga('secondaryTracker.send', type, parameters);
},
/**
The set method is used to call the set method on both analytics profiles
@method set
@params type {string} The type of send i.e. event
@params parameters {string or object} The parameters associated with the type reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
*/
set : function (type, parameters) {
ga('primaryTracker.set', type, parameters);
ga('secondaryTracker.set', type, parameters);
},
/**
The init method is used to check if the ga object is created and fire all custom tracking
with optional callback
@method init
@params callback {function} Optional callback function for when tracking is created
*/
init : function (callback) {
//check if the ga object is there
if (window.ga) {
afga.setSiteValues();
afga.createPrimaryTracking();
afga.createSecondaryTracking();
if (callback) {
callback();
}
}
else {
//console.log('GA Isn\'t Loading');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment