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

delayLoad and delayLoadTime did not work #58

Open eDubrovsky opened 1 year ago

eDubrovsky commented 1 year ago

Hi! At version 4.0.3, the delay load didn't work.

delayLoad: true,
  delayLoadTime: 1000

Segment load without page scroll event or Link click.

newhouse commented 1 year ago

I'll have a look. I guess nobody really has been using that or watching closely?

Are you a new user of this library, or that option? Wondering how long it's been broken.

newhouse commented 1 year ago

Hi @eDubrovsky I think you just need to set the trackPage: true option...the delayLoad functionality is working for me.

eDubrovsky commented 1 year ago

@newhouse Hi!

I have used this plugin for about 10 months, and version 3.7.1 works perfectly. :)

I found the problem scenario. My current setting:

trackPage: true,
trackPageDelay: 50,
delayLoad: true,
delayLoadTime: 1000

In this case, the delay time = trackPageDelay = 50. And script loaded without a page scroll event. But delayLoadTime and loading script by scroll event no applied in this case.

If I set delayLoad to false, the script loading without delay and works correctly.

newhouse commented 1 year ago

Hi @eDubrovsky maybe I'm misunderstanding. Are you saying that:

  1. With the settings listed above
  2. you expected the pageview to not occur until the User scrolls?

I think perhaps:

Can you try to clarify a bit for me - I think I'm not following. Thank you!

eDubrovsky commented 1 year ago

I am trying to improve my website's TTI for PageSpeed Insights by Google. When I set delayLoad: true I am expecting this scenario:

1) User opens my site, and Segment is NOT loading (it helps with a better result in PageSpeed Insights) 2) After the user scrolls the page, the Segment loading with delayLoadTime: 1000 3) After that analytics.page() trigger with trackPageDelay: 50

CleanShot 2022-12-20 at 00 24 33@2x

newhouse commented 1 year ago

OK, I think I understand.

You expect to not see the loading occur until either (a) the user scrolls or (b) a page routing change.

I think what I've done in 4.x is more like breaking-change and expected behavior from this issue: https://github.com/benjaminhoffman/gatsby-plugin-segment-js/issues/37

At this point, what I think I should do is leave things how they are with the options you've chosen, but add an additional option like delayLoadUntilScrollOrRouteChange that if true will do what you're expecting....which was the old actual behavior but not the new current behavior.

How does that sound?

newhouse commented 1 year ago

Code back at 3.7.1:

https://github.com/benjaminhoffman/gatsby-plugin-segment-js/tree/897db9ba7f2b0bab91aab3c87bac1682fcf36396 https://github.com/benjaminhoffman/gatsby-plugin-segment-js/blob/897db9ba7f2b0bab91aab3c87bac1682fcf36396/src/gatsby-browser.js https://github.com/benjaminhoffman/gatsby-plugin-segment-js/blob/897db9ba7f2b0bab91aab3c87bac1682fcf36396/src/gatsby-ssr.js

eDubrovsky commented 1 year ago

@newhouse delayLoadUntilScrollOrRouteChange option sounds cool, yep! 🤟

newhouse commented 1 year ago

@eDubrovsky can you please install 5.0.0-beta.0 and play around with it a bit to see if it's working correctly?

You can see the new options here: https://github.com/benjaminhoffman/gatsby-plugin-segment-js/pull/59/files

newhouse commented 1 year ago

Actually 5.0.0-beta.1, please.

eDubrovsky commented 1 year ago

Yes, it works! Like it! 🤟

p.s.: how can I turn off plugin console.logs?

CleanShot 2022-12-22 at 18 17 07@2x