question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

with-segment-analytics example reports incorrect data on client-side transitions

See original GitHub issue

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:

  1. Install Segment in a Next.js project according to the example repo
  2. Click a “next/link”, resulting in a client-side route change
  3. analytics.page() is called with path, 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.

Screen Shot 2020-03-31 at 1 21 07 PM

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 asPaths.

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:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
benjaminhoffmancommented, Oct 12, 2021

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:

  1. name value was being reported as the url
  2. url was always the homepage, even if I loaded a test page first (in incognito)
  3. path was always / (same as url issue described above)

I’d be willing to bet many folks get similar behavior if they follow the example repo exactly. Here’s why:

  1. 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 as name.

  2. Unless you explicitly set page canonical, Segment will report only the homepage (or maybe only the first page you load — please test to confirm)

  3. 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:

  1. always set page canonical tag (I used next-seo)
  2. do NOT pass url like in the next example
  3. here is my code:
const trackPageView = (
  category: string = null,
  name: string = null,
  properties: object = {},
  options: object = {},
  callback?: () => void,
) => {
  window.analytics.page(category, name, properties, options, callback);
};
0reactions
gbsimon87commented, Feb 15, 2022

Hey @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”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Analytics.js | Segment Documentation
In May 2018, Segment released a change to Analytics.js that collects client-side performance metrics in Analytics.js.
Read more >
Segments - Acoustic Help Center
You can use Segments to limit report results to data that match your specified criteria. Choose your segments for your desired report by ......
Read more >
Troubleshooting Cross-Domain Tracking In Google Analytics
Once the data hits the Google Analytics reports, you should find your single session when applying the segment. After that, go to the...
Read more >
IBM Cognos Analytics - Reporting Version 11.1 : User Guide
US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with. IBM Corp. Page 3. Contents. Chapter...
Read more >
Cisco Webex Contact Center Analyzer User Guide
Click Settings to display the segments and variables associated with the ... The time period for which the Self-service analytics data is reported....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found