googleanalytics / autotrack

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

hash and older browser support #193

Closed sharq88 closed 7 years ago

sharq88 commented 7 years ago

Hi.

"Since almost all browsers in use today support the History API, that's what you should be using when creating SPAs. Hash changes should only be used for in-page navigation to anchor links." https://github.com/googleanalytics/autotrack/issues/146

"If we have to support IE9 and below we have to use the same hash based routing." http://krasimirtsonev.com/blog/article/deep-dive-into-client-side-routing-navigo-pushstate-hash

I know you have a few tickets and been asked about why it doesn't support hashes a few times, but would it be possible to move the getPath() method into the options, so it can be overridden in case we need to support older browsers / hash URL's? That would be a massive help! :) https://github.com/googleanalytics/autotrack/blob/master/lib/plugins/url-change-tracker.js#L162

Thanks

philipwalton commented 7 years ago

Adding the hash option to the getPath() function wouldn't solve your issue because the plugin only "listens" for URL changes, it doesn't listen for the hashchange event.

I opted not to include hash changes since adding your own hashchange listener is pretty easy (and I still feel strongly that developers shouldn't be using hash URL for SPA).

That being said, if enough people wanted generic hash support for URLs (like when users click on in-page anchor links), I'd consider adding an option to that, but I'd probably do it as an event rather than a pageview.

sharq88 commented 7 years ago

@philipwalton - thanks for your reply. This is not down to us whether or not to use hash. If you have to support IE9 you have to use hash unfortunately. I was thinking:

if (window.onpopstate != undefined) {
    window.addEventListener('popstate', this.handlePopState);
} else {
    window.addEventListener('hashchange', this.handleHashChange);
}

but as I see:

// Feature detects to prevent errors in unsupporting browsers.
if (!history.pushState || !window.addEventListener) return;

it would be indeed more change than just the getPath method. Never mind, we'll implement a custom version...

philipwalton commented 7 years ago

@sharq88 I added a suggestion in https://github.com/googleanalytics/autotrack/issues/194 that might be helpful to you.