Google Analytics and hash/anchor doesn't work

2019-07-20 06:23发布

问题:

I hope you can help me.

I have a gallery in Javascript. Each picture has a specific hash.

www.example.com/gallery.html#title_1

My stats are on Google Analytics but hash doesn't exist even when I tried this in the respective code :

_gaq.push(['_trackPageview', location.pathname + location.search  + location.hash]);

or this :

_gaq.push(['_setAllowAnchor', true]);

Any idea to solve this problem ?

Thanks for your answers.

回答1:

_setAllowAnchor is for a completely different use case.

The right way to do it is as you said:

_gaq.push(['_trackPageview', location.pathname + location.search  + location.hash]);

But you need to execute this after the hash has changed. I think you are executing this when the page loads. When the hash changes the page doesn't reload so you need to run this function call explicitly again.