-
Notifications
You must be signed in to change notification settings - Fork 26.2k
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
with-segment-analytics example reports incorrect data on client-side transitions #11535
Comments
@pjaws have you tried wrapping export const useAnalytics = () => {
React.useEffect(() => {
const handleRouteChange = (url: string) => {
requestAnimationFrame(() => window.analytics.page(url))
}
Router.events.on('routeChangeComplete', handleRouteChange)
return () => Router.events.off('routeChangeComplete', handleRouteChange)
}, [])
} |
I'm not 100% certain but after testing locally, I found inconsistencies with data being reported in Segment (similar to @pjaws example) based on the current TLDR:
I'd be willing to bet many folks get similar behavior if they follow the example repo exactly. Here's why:
I am still testing so please take this with a grain of salt, but so far I think this solution works pretty well:
|
Hi @benjaminhoffman, I'm just following up on your comment here, which lead me to this Github issue. Did you get it all up and running with expected behaviour? I am in the process of setting up Segment with a new Next.js app and am a little confused as to what the latest official recommended approach is. Should I be using As noted by you earlier, I also noticed this excerpt:
Do we even need to install any of these dependencies? If we just copy and paste the generated snippet created by Segment, it should just load the latest version from the CDN right? |
@shih-js im using snippet bc i couldn't figure out why it was bad not to use it. if you can get the quickstart snippet working, go for it! lemme know your implementation bc i'd love to refactor mine. (i never bothered to even try). where i think you'll have the most issue is with |
Hey @benjaminhoffman @shih-js |
Bug report
Describe the bug
Following the example for client-side
analytics.page()
calls results in data from the previous page being sent.To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
analytics.page()
is called withpath
,url
, etc. from the previous pageExpected behavior
analytics.page()
should be called with the current page infoScreenshots
In this screenshot I called
analytics.page(url)
inside ofRouter.on('routeChangeComplete', (url) => {...})
as per the example. Path should be/start
, but instead is/
. Also worth noting the/start
page event you see at the bottom of the screenshot was from a full page refresh, not a client-side transition. You can clearly see the difference.System information
Additional context
In my case I fixed the issue by overriding the default Segment properties with properties I parsed from the
url
in the callback (using new URL()). This is a little tricky and hard to generalize, since it can depend on whether or not you're using absolute or relative paths in yourasPath
s.The real solution is probably figuring out how Segment determines these values and why there is an inconsistency between those and the values in
next/router
whenrouteChangeComplete
is fired.The text was updated successfully, but these errors were encountered: