Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking Google Analytics on a 1 page website using hashtags

I have a long single page website using hashtags in the URL when you get to a certain section from scrolling or clicking. What technique can I use to track these hashtags as "pages" in google analytics?

like image 646
Dan Avatar asked Dec 22 '22 05:12

Dan


1 Answers

(Recycling from a prior answer...)

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, 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]);

})
like image 108
Yahel Avatar answered Dec 24 '22 01:12

Yahel