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

add delay to page tracking call #30

Closed TomPridham closed 4 years ago

TomPridham commented 4 years ago

adding a delay to the tracking call gives the analytics.js page event and the gatsby router time to sync. i'm not sure where the actual break down is, but this seems to fix the page being one route change behind and the initial load event not firing fixes #25, fixes #28, fixes #26

TomPridham commented 4 years ago

@benjaminhoffman would you be able to look at this?

benjaminhoffman commented 4 years ago

yes! will do. thank you

benjaminhoffman commented 4 years ago

@TomPridham Does this https://github.com/benjaminhoffman/gatsby-plugin-segment-js/commit/42a2c1e6765c93dcb29f7df96d8f6a6361311635 not solve this problem yet already? My thought was that if we delay the loading of this script, then it too should delay the page call thereby allowing the router to set the correct page title.

Thoughts? Happy to merge this though.

TomPridham commented 4 years ago

The initial page tracking call is correct, but every page call after that is one behind. Below is the behavior I am seeing.

For what it's worth, I tried the other Gatsby - Segment plugin that other people mentioned having worked for them and observed the same behavior. So, I don't think the issue is with this plugin, but with the Gatsby router and when it's triggering the routeChange event that the plugin hooks into

benjaminhoffman commented 4 years ago

thanks @TomPridham !

benjaminhoffman commented 4 years ago

@TomPridham i made a few minor adjustments: https://github.com/benjaminhoffman/gatsby-plugin-segment-js/commit/9e75509fea67caa6d62801811cb84d81d7f41b4d

thanks again for this! going to deploy the new version today as a minor update