googleanalytics / autotrack

Automatic and enhanced Google Analytics tracking for common user interactions on the web.
Other
4.93k stars 563 forks source link

Can urlChangeTracker detect the change of url after #? #194

Closed flybreeze closed 7 years ago

flybreeze commented 7 years ago

url1: http://127.0.0.1:8080/usercenter/#/userindex/test1 url2: http://127.0.0.1:8080/usercenter/#/userindex/test2

Help, when changing from url1 to url2, how can urlChangeTracker detect it?

philipwalton commented 7 years ago

As the urlChangeTracker documentation says:

Note: this plugin does not support tracking hash changes as most Google Analytics implementations do not capture the hash portion of the URL when tracking pageviews.

I recommend you update your application to use real URLs via pushState instead of hash URLs. If you cannot update, then you can add hash change tracking yourself via the hashchange event.

Something like this:

window.addEventListener('hashchange', function() { 
  var page = location.pathname + location.search + location.hash;
  ga('set', 'page', page);
  ga('send', 'pageview');
});

But be careful when doing this, as this will also track a pageview when a user clicks on in-page anchor links (another reason not to use hash URLs in SPAs).

flybreeze commented 7 years ago

thanks a lot. Get much.