benjaminhoffman / gatsby-plugin-segment-js

Gatsby plugin for segment.com's analytic.js snippet
https://www.npmjs.com/package/gatsby-plugin-segment-js
MIT License
40 stars 28 forks source link

`title` reflects the previous page #25

Closed chriswhong closed 4 years ago

chriswhong commented 4 years ago

Thanks for a great plugin!

I got it working but noticed in my segment dashboard that the title property always reflects the previous page instead of the one being reported in the pageview.

For example, navigating to /contact from /download shows up as:

analytics.page({
  path: '/contact',
  referrer: '',
  search: '',
  title: 'Download Qri',
  url: 'http://localhost:9000/contact'
});

I'm wondering if anyone has encountered this before or has an idea what could be going wrong.

I have delayLoad set to true and delayLoadTime set to 1000

benjaminhoffman commented 4 years ago

@chriswhong this is an interesting observation. this could potentially be an issue. personally i haven't experienced this but i also have never used the delayLoad. will ask around.

Kilian commented 4 years ago

Are you doing anything custom with your routing or in your gatsby-browser.js?

chriswhong commented 4 years ago

I don't think so. Here's my site's source code if anyone wants to clone and try to reproduce. We ended up using gatsby-plugin-segment-analytics

bwlng commented 4 years ago

I’ve seen the same issue on my site. Segment's debug tool always shows the previous page’s info when using onRouteUpdate in gatsby-browser.js to call window.analytics.page() on pages visited after the initial page load.

The location object available via onRouteUpdate does have the correct page info, but there must be a race condition where Segment's tracking is handled before the page/DOM are actually updated. In my tests, delaying the analytics call by 1 second when the route updates works around the issue. I haven't seen a Gatsby API that would be a better fit than onRouteUpdate.

Did you find a more reliable solution?

benjaminhoffman commented 4 years ago

super excited to announce, this issue is fixed! hat tip to @TomPridham. solved via https://github.com/benjaminhoffman/gatsby-plugin-segment-js/pull/30

issue is now closed. please upgrade your version of this plugin to v3.3.0 and definitely let us know if you are still experiencing any issues!

cc @bwlng @chriswhong @Kilian