with-segment-analytics example reports incorrect data on client-side transitions
See original GitHub issueBug 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:
- Install Segment in a Next.js project according to the example repo
- Click a “next/link”, resulting in a client-side route change
analytics.page()
is called withpath
,url
, etc. from the previous page
Expected behavior
analytics.page()
should be called with the current page info
Screenshots
In this screenshot I called analytics.page(url)
inside of Router.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
- OS: macOS
- Browser: Chrome
- Version of Next.js: 9.3.3
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 your asPath
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
when routeChangeComplete
is fired.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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
with-segment-analytics
example. Let me report what I’ve found and I’m curious to see what others may be finding.TLDR:
name
value was being reported as theurl
url
was always the homepage, even if I loaded a test page first (in incognito)path
was always/
(same asurl
issue described above)I’d be willing to bet many folks get similar behavior if they follow the example repo exactly. Here’s why:
According to the Segment docs: “if you pass only one string to page it is assumed to be name.” In the nextjs example, we are passing
url
but Segment is interpreting that asname
.Unless you explicitly set page canonical, Segment will report only the homepage (or maybe only the first page you load — please test to confirm)
Not really related to this issue/bug but in the example repo, we use Segment snippet. As instructed in the docs: “The recommended way to use analytics.js is to follow the analytics.js quickstart guide. If you absolutely need to generate a snippet dynamically, this is an alternate solution.” … it would be helpful if we could update the example to discourage the use of this snippet.
I am still testing so please take this with a grain of salt, but so far I think this solution works pretty well:
url
like in the next exampleHey @benjaminhoffman @shih-js Great info above - first time I’m setting up Segment in a NextJS App for the same time and I had the exact same questions/concerns as to which approach to take.
I believe the ‘headaches’ being experienced with analytics.page are mentioned here.
TL/DR: “to avoid event duplication in Segment, do not forget to turn of the automatic page call from the _document.js snippet like”