Skip to content

Instantly share code, notes, and snippets.

@anselmh
Created June 27, 2011 09:15
Show Gist options
  • Save anselmh/1048549 to your computer and use it in GitHub Desktop.
Save anselmh/1048549 to your computer and use it in GitHub Desktop.
prerender-avoid-ga
// if you have analytics, set them in pagevisibility mode to get real-user-trackig according to prerender-issues in Chrome/WebKit.
var viewCounted = false;
function handleVisibilityChange(evt) {
//Only count the view once we're done prerendering AND the document is done loading
if (document.webkitVisibilityState == 'prerender'
|| document.readyState != 'complete' || viewCounted)
return;
/* Implementation of view counting system starts here */
// Code for Google Analytics | Change UA-XXXXX-X to be your site's ID
var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview'],['_trackPageLoadTime']];
(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'));
/* Implementation of view counting system ends here */
viewCounted = true;
}
//We'll call checkCountView on load no matter what.
window.addEventListener('load', handleVisibilityChange, false);
if (document.webkitVisibilityState == 'prerender') {
document.addEventListener('webkitvisibilitychange', handleVisibilityChange, false);
}
@anselmh
Copy link
Author

anselmh commented Aug 3, 2011

This is obsolete since yesterday: http://www.google-analytics.com/ga.js now included.

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