Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trigger onRouteUpdate on hash updates #2302

Closed
brandonmp opened this issue Oct 2, 2017 · 3 comments
Closed

Don't trigger onRouteUpdate on hash updates #2302

brandonmp opened this issue Oct 2, 2017 · 3 comments
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@brandonmp
Copy link
Contributor

brandonmp commented Oct 2, 2017

I export an onRouteUpdate from gatsby-browser.js like so:

export const onRouteUpdate = ({ location, action }) => {
    if (
        window.location &&
        typeof window.location.href === 'string' &&
        window.location.href.includes('www.example.com') // make sure we're on live site
    ) {
        window.analytics.page();
    } else {
        console.log('PAGE: ', window.location.href, action, location);
    }
    return;
};

This works fine, but when there are anchor links on a page, it fires every time one of them is clicked.

I tried just firing the page() on action === 'PUSH' route updates, but that blocks the event on all initial page loads.

Any way to remove anchor link events from this tracking?

@sebastienfi
Copy link
Contributor

Hi @brandonmp Maybe check in window.location.href if # character is present ?

@sebastienfi sebastienfi added type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby labels Oct 2, 2017
@sebastienfi sebastienfi changed the title double-counting pageviews in onRouteUpdate [gatsby-browser.js] Check with onRouteUpdate if the click event targets an anchor link Oct 2, 2017
@KyleAMathews KyleAMathews changed the title [gatsby-browser.js] Check with onRouteUpdate if the click event targets an anchor link Don't trigger onRouteUpdate on hash updates Oct 2, 2017
@KyleAMathews
Copy link
Contributor

@sebastienfi he's saying that a hash update (what happens when you click on an anchor) shouldn't trigger the onRouteUpdate API. Which seems pretty reasonable to me. Hash updates should only occur within a single page where the API is intended to be triggered only when switching pages.

@brandonmp would love a PR filtering out hash changes within the same page here!

apiRunner(`onRouteUpdate`, { location, action })

Perhaps put the function in a different file and add a few tests?

@KyleAMathews
Copy link
Contributor

Not sure when but we don't fire onRouteUpdate on route changes any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants