Skip to content

Instantly share code, notes, and snippets.

@NickTomlin
Created March 28, 2012 19:50
Show Gist options
  • Save NickTomlin/2229934 to your computer and use it in GitHub Desktop.
Save NickTomlin/2229934 to your computer and use it in GitHub Desktop.
Roots Google Analytics Asynch
<?php
// change this
echo "\n\t<script>\n";
echo "\t\tvar _gaq=[['_setAccount','$roots_google_analytics_id'],['_trackPageview'],['_trackPageLoadTime']];\n";
echo "\t\t(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];\n";
echo "\t\tg.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';\n";
echo "\t\ts.parentNode.insertBefore(g,s)}(document,'script'));\n";
echo "\t</script>\n";
// to this (using this snippet from: http://googlecode.blogspot.com/2009/12/google-analytics-launches-asynchronous.html )
echo "\n\t<script>\n";
echo "\t\tvar _gaq = _gaq || [];\n";
echo "\t\t_gaq.push(['_trackPageview'])\n";
echo "\t\t(function() {var ga = document.createElement('script');\n";
echo "\t\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n";
echo "\t\tga.setAttribute('async', 'true');\n";
echo "\t\tdocument.documentElement.firstChild.appendChild(ga); \n";
echo "\t</script>\n";
// Is this a stupid idea?
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment