Code Monkey home page Code Monkey logo

delaying-3rd-parties's Introduction

Delaying third-party scripts

The solution has been adopted from Gatsby (https://github.com/Kilian/gatsby-plugin-segment-js).

It works as follows:

  • wait for the user to scroll or update the site (for example via clicking the burger menu)
  • do a setTimeout for 1 second
  • then load the third-party scripts

Due to this time delay, if user visits the website, page appears and starts to be interactive immediately (it depends on first-party scripts though) and the third-party scripts load only if user starts scrolling or making interactions on the page.

It's a perfect solution to load third-parties such as ads, chats, widgets (that are below-the-fold). In case of Google Analytics - feel free to try it, however be ready for inaccurate stats. If user visits your website and does not scroll or make any action, Google Analytics will never track that user because the script is not loaded in that case.

How does it work technically?

All the third-party scripts you want to delay, should use <fscript> tag instead of <script>. So in case of script such as:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "...", enable_page_level_ads: true });</script> 

you should convert it to:

<fscript async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></fscript>
<fscript>(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "...", enable_page_level_ads: true });</fscript> 

This way, the real third-party code is not executed.

Then, the JavaScript code looks for all <fscript> tags and listens to scroll or click events on the website, waits 1 second and converts all the <fscript> into real <script>, making them executed (wrapped with requestIdleCallback if supported in user's browser).

How to use it?

1. Add CSS

fscript {display: none;}

2. Add JS code

See src/index.html in the <head> section.

3. Convert the third-party scripts you want to delay

As explained in the introduction.

Work done :-)

delaying-3rd-parties's People

Contributors

bartek-studiosidekick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.