In our quest to make our site loads as fast as possible and pleasing search engines in the process, we always take the time to carry out a comprehensive on-page optimisation and surgically identify web page issues that may be dragging the speed of your site.
One crucial action a webmaster can do is to keep the total number of HTTP requests low, by:
- Minifying and inlining small scripts
- Lazy-loading images and iframes
- Optimising the delivery of third-party scripts and resources like live chats, Google Analytics, Google Tag Manager, Facebook Pixel, Remarketing Tags, web font files, etc.
The first three actions above can easily be done with robust caching plugins like WP Rocket. However, things can be a bit tricky when it comes to manipulating third-party scripts such as Google Analytics and Facebook Pixels.
Why third-party scripts are such a bummer
The trouble with third-party scripts like live chat add-ons or Google Analytics is that they’re all hosted elsewhere and there’s no way you can manipulate its headers and how they should render in a web browser. While these scripts normally are loaded asynchronously, these scripts pull additional external resources which can potentially impair user experience.
Site Speed Optimisation: Loading Third Party Scripts When Needed
Here’s an example implementation to demonstrate the idea:
The above script instructs the browser to load Health Engine script during scroll or mousemove, which gives us this nice, acceptable page speed performance:
Before optimisation, our customer has this standard embed:
Looks tiny, right?
Well, as it turns out, that miniscule-looking Health Engine resource pulls additional 28 resources for each page load, as seen below:
Using scroll and mousemove to load third-party scripts
The scroll event works reliably on desktop while mousemove is used as fallback for mobile and tablets, giving us the confidence that third-party scripts will work just fine to humans who interact the site. Be sure you do plenty of tests before finally rolling this method out to production sites.
Need help improving your site’s speed? Just get in touch.