Skip to content

Instantly share code, notes, and snippets.

@ralphsaunders
Created November 14, 2011 19:21
Show Gist options
  • Save ralphsaunders/1364848 to your computer and use it in GitHub Desktop.
Save ralphsaunders/1364848 to your computer and use it in GitHub Desktop.
Switch GA Code based on URL Hash
<!doctype html>
<html>
<head>
<title>Index</title>
<script type="text/javascript">
var _gaq = _gaq || []; // GA array to push to
/**
* window.location refers to the location of the document, i.e.
* http://example.com/example-document.html.
*
* hash refers to the segments of the URL that appear after the
* document we've loaded, e.g:
*
* the URL: http://example.com/document.html?#modulePage123
* the hash: #modulePage123
*/
var hash = window.location.hash;
/**
* The pattern we're going to use in the regular expression to grab
* the "#modulePage" in "#modulePage123".
*/
var pattern = '.*[a-z]';
// An array of your GA codes
var codes = [ "UA-12345678-1", "UA-12345678-2" ];
if( hash.match( pattern ) == '#modulePage' ) { // if we got a match
_gaq.push(['_setAccount', codes[1]] ); // Put in the second GA code
} else {
_gaq.push(['_setAccount', codes[0]] ); // Put in the first A code
}
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
Index page
<a href="section.html?#modulePage123456789" title="Link to special page">Link to special page</a>
</body>
</html>
<!doctype html>
<html>
<head>
<title>Special</title>
<script type="text/javascript">
var _gaq = _gaq || []; // GA array to push to
/**
* window.location refers to the location of the document, i.e.
* http://example.com/example-document.html.
*
* hash refers to the segments of the URL that appear after the
* document we've loaded, e.g:
*
* the URL: http://example.com/document.html?#modulePage123
* the hash: #modulePage123
*/
var hash = window.location.hash;
/**
* The pattern we're going to use in the regular expression to grab
* the "#modulePage" in "#modulePage123".
*/
var pattern = '.*[a-z]';
// An array of your GA codes
var codes = [ "UA-12345678-1", "UA-12345678-2" ];
if( hash.match( pattern ) == '#modulePage' ) { // if we got a match
_gaq.push(['_setAccount', codes[1]] ); // Put in the second GA code
} else {
_gaq.push(['_setAccount', codes[0]] ); // Put in the first GA code
}
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
Special page
<a href="index.html" title="Link to regular age">Link to regular page</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment