Skip to content

Instantly share code, notes, and snippets.

@magnificode
Created May 1, 2013 13:49
Show Gist options
  • Save magnificode/5495372 to your computer and use it in GitHub Desktop.
Save magnificode/5495372 to your computer and use it in GitHub Desktop.
Google Analytics Switcher
<?php
//hostname based google code, only run if on live site - Tom Horak
//placed right before closing body tag according to google best practice: http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55488
//moved to right before closing head tag per SEOverflow 05/25/12
$hostname = $_SERVER['HTTP_HOST']; //dev.zenman.com | localhost | Live server | etc..
switch ($hostname) {
case 'localhost': //do nothing
//echo '<!-- no google analytics code -->';
break;
case 'localhost:8888': //do nothing
//echo '<!-- no google analytics code -->';
break;
case 'dev1.zenman.com': //do nothing
//echo '<!-- no google analytics code -->';
break;
case 'test1.zenman.com': //do nothing
//echo '<!-- no google analytics code -->';
break;
case 'stage1.zenman.com': //mimic live environment...
?>
<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'GA CODE HEREEERERERERERE']); _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>
<?php
break;
default: //this is what you get on live server
?>
<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'GA CODE HEREEERERERERERE']); _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>
<?php
break;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment