Skip to content

Instantly share code, notes, and snippets.

@rodrigobarona
Last active August 29, 2015 13:56
Show Gist options
  • Save rodrigobarona/9242874 to your computer and use it in GitHub Desktop.
Save rodrigobarona/9242874 to your computer and use it in GitHub Desktop.
Tracking Google Analytics on a 1 page website using hashtags
Generically, your code could look like this:
_gaq.push(['_trackPageview',location.pathname + location.search + location.hash]);
You could either bind that code to every time you have a hash change within your application,
or you could use a generic hashchange plugin (http://benalman.com/projects/jquery-hashchange-plugin/), that uses the HTML5 onhashchange, and some
backwards compatible hacks for older browsers, and bind this code to that event, so that it
fires every time your hash changes.
Using that plugin, your code could look like:
$(window).hashchange( function(){
_gaq.push(['_trackPageview',location.pathname + location.search + location.hash]);
})
@rodrigobarona
Copy link
Author

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