gatsby-plugin-segment-js icon indicating copy to clipboard operation
gatsby-plugin-segment-js copied to clipboard

Change functionality of delayLoad?

Open abohannon opened this issue 4 years ago • 4 comments

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.

abohannon avatar Oct 30 '20 20:10 abohannon

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.

benjaminhoffman avatar Nov 02 '20 21:11 benjaminhoffman

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.

abohannon avatar Nov 02 '20 23:11 abohannon

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.

Kilian avatar Nov 03 '20 08:11 Kilian

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

TomPridham avatar Nov 03 '20 17:11 TomPridham