gatsby-plugin-segment-js
gatsby-plugin-segment-js copied to clipboard
Change functionality of delayLoad?
Out of curiosity, was there a particular reason scroll and route changes were chosen as triggers for script load? My usecase needs to delay the load of the script for performance reasons, but an idle pageview is still valuable to us to accurately assess bounce rates.
Is it worth creating an option to listen to load
instead of scroll
? If so, I'm happy to submit a PR if the community thinks it would be useful.
This is a good point. We did it to improve TTI and should only be used with client routing. I guess the assumption was folks would always scroll or click to another route. Very rare to do neither, right? But I do see your point.
Maybe @TomPridham or @Kilian has a thought on this, as they were instrumental in helping get this plugin to where it is today.
FWIW I forked the code and changed the event listener to load
in our codebase and now the Segment script loads well after the critical resources. So far events are flowing normally with no issue.
This is indeed a caveat to account for: when you delay the load of the script, you'll potentially miss users that bounce before the script is loaded.
My understanding is that when you trigger your script on load, it still counts towards your TTI, whereas triggering it on a user action does not.
Maybe setting it on load wrapped in a requestIdleCallback
(best but not well supported) or requestAnimationFrame
has the same benefits. This can be tested.
i think you could accomplish this using the manualLoad
property already. set that to true, then have a listener in your own code that calls analytics.load(writeKey)
on load