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

Analytics treats in-page anchor jumps as pageviews #51

Closed
Justineo opened this issue Feb 22, 2023 · 6 comments
Closed

Analytics treats in-page anchor jumps as pageviews #51

Justineo opened this issue Feb 22, 2023 · 6 comments

Comments

@Justineo
Copy link

There seems to be a potential bug in /_vercel/analytics/script.js:

history.pushState = function(...t) {
    let n = t[2]
      , i = "string" == typeof n ? location.pathname !== n.split("?")[0] : n && location.href !== n.href;
    e(...t),
    i && r()
}

It is comparing pathname with the target URL apart from the search part. But for target URLs like /foo#bar it will be count as a new page view. And I'm also aware that if we strip hashes as well this may break the use case for those SPAs with hash router. So I think we should expose an option to let users to configure their routing behavior.

@tobiaslins
Copy link
Collaborator

Hey @Justineo :)

We consider adding a way to disable automatic page view collection (and triggering them manually).
For now you will need to use beforeSend to skip tracking page views with # in the URL.

<Analytics beforeSend={function (event) {
    if(event.url.includes("#")) return 
    return event;
}} />

Something like that would work, but sadly there is no way to find out if it was a crosspage navigation or same page hash navigation.
https://vercel.com/docs/concepts/analytics/audiences/redacting-sensitive-data

@Justineo
Copy link
Author

Justineo commented Feb 22, 2023

I think this workaround won't work as the page view event does not expose enough information. The page view from /foo to /bar#baz shouldn't be skipped.

We need both the URL before navigation and after it to see if history change is caused by in-page anchors but in current implementation we only got url.

interface PageViewEvent {
type: 'pageview';
url: string;
}

@Justineo Justineo changed the title Analytics is including in-page anchor jumps as page views Analytics treats in-page anchor jumps as pageviews Mar 3, 2023
@Justineo
Copy link
Author

Hello, I'm following up on the status of this issue. Has there been any progress?

@tobiaslins
Copy link
Collaborator

tobiaslins commented May 3, 2023

@Justineo
You could keep the previous page in useRef and only skip the event when its a same page navigation! What do you think?

@Justineo
Copy link
Author

Justineo commented May 3, 2023

I think this is a common use case and shouldn’t require such workaround. Technically we can rewrite any client side logic but I think it should be handled by the analytics SDK itself.

@tobiaslins
Copy link
Collaborator

tobiaslins commented Jul 21, 2023

We just rolled out a new version that now doesn't track same-page hash navigations anymore!
This should work now as expected without any changes on your side!

Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants