Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a delayed load function to improve TTI #19

Open
wants to merge 6 commits into
base: master
from

Conversation

@Kilian
Copy link

Kilian commented Aug 27, 2019

The regular snippet will count towards your TTI, which can be harmful for your SEO rating especially if Segment loads a lot of scripts. I implemented a delayLoad option (defaults to false) that only loads the segment snippet if one of two conditions are met:

  • The user has scrolled
  • The user has navigated to another page (onRouteChange)

After which there is a 1s delay and the snippet is loaded. This follows the advice in this article: https://marketingexamples.com/seo/performance

You will lose at least the first second of any interaction which is why it should default to false.

Copy link
Owner

benjaminhoffman left a comment

very interesting. i was under the impression segment would not hurt TTI because the script was async and/or deferred. I read the article and it makes sense what they are saying... is there any other evidence or documentation that Segment itself is the underlying culprit? Maybe it's the marketing tools they choose to use.

I ask because it would be a shame to punish all the tools just because one is causing an issue. For example, let's say you have a lightweight pageview snippet and a heavy chat widget. That pageview snippet should be fired immediately, on page load.

src/gatsby-ssr.js Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
@Kilian

This comment has been minimized.

Copy link
Author

Kilian commented Aug 27, 2019

Scripts may be async or deferred but that means they just end up at the back of the stack, not separate from it. So your time to first paint will be lower with async and deferred but the time to interactive still takes them into account.

I don't think Segment gives you much of an option to delay certain scripts but load others instantly so adding a delay to Segment in total is the only way to achieve this. Basically it's balancing correct marketing tracking vs SEO/customer experience, and tilting it slightly more towards the latter.

@Kilian

This comment has been minimized.

Copy link
Author

Kilian commented Aug 29, 2019

Just updated with a customisable delay time and the improvements suggested by @davej :)

@n00b2pr0

This comment has been minimized.

Copy link

n00b2pr0 commented Oct 3, 2019

Other than the drawbacks mentioned here related to bounce rate (and potential janky scroll while loading), any reason this solution shouldn't be considered?

I love the idea. Saw this solution when it was mentioned by GoodMarketingHQ on Twitter, excited to see this PR as I start to think about Segment on a Gatsby project. 🙌

@Kilian

This comment has been minimized.

Copy link
Author

Kilian commented Oct 23, 2019

Giving this a gentle bump @benjaminhoffman :)

@riencoertjens

This comment has been minimized.

Copy link

riencoertjens commented Nov 5, 2019

is anything holding back this pr? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.