Skip to content

Instantly share code, notes, and snippets.

@craveytrain
Forked from pierceray/ga_lookup_code.js
Created June 23, 2011 01:42
Show Gist options
  • Save craveytrain/1041717 to your computer and use it in GitHub Desktop.
Save craveytrain/1041717 to your computer and use it in GitHub Desktop.
determining the GA code based on params or lack there of
//lookup object
var gaKeys = {
'tdshs_ems': {
'01': 'UA-234234-23',
'02': 'UA-234234-24'
},
'error' : 'UA-bad-code'
};
var configId = '<param>'.toLowerCase(); //error condition is 'error'
var licenseId = '<param>'.toLowerCase(); //other values are 2 digit numbers and 'error'
var page = '<param>'.toLowerCase(); //other value 'elgibility', 'guidelines', 'null'
if (!configId || (configId === "null")) {
configId = "error";
}
if (!licenseId || (licenseId === "null")) {
licenseId = 'error';
}
if (!page || (page === 'null')) {
page = 'welcome';
}
var theKey = (configId !== 'error' && licenseId !== 'error') ? gaKeys[configId][licenseId] : gaKeys[configId];
var path = // stuff goes here.
var _gaq = [
['_setAccount', theKey],
['_trackPageview', path],
['_trackPageLoadTime'],
['_setLocalRemoteServerMode']
];
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = 1;
g.src=('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
@craveytrain
Copy link
Author

Few notes:

  • Used a more concise var declaration method, just my personal style
  • Change type of licenseId to string. I realize currently it's numbers, but this way, if it were to start using anything else, it's preserved
  • Simple ORs for configId and page
  • Ternary operator for getting the key (using the falsiness of licenseId as an empty string)
  • Instead of creating an empty array and pushing to it, just initialize it with data in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment