October 8, 2024
Slow loading ads can have a significant impact on your website’s revenue. With users becoming more impatient and expecting instant page load times, every second of delay could cost your business potential customers and reduce ad viewability. But it’s not just about loading ads faster; it’s about optimizing the entire ad delivery process to increase engagement and improve your website's performance.
In this article, we will explore the relationship between slow ads and revenue loss, and provide actionable solutions complete with code examples for ensuring that ads load quickly, improving user experience and boosting your business's profitability.
Lazy loading is a technique that delays the loading of ads until they are about to enter the viewport, reducing initial load time and improving page performance. Using the IntersectionObserverAPI is a modern and effective way to implement lazy loading for ads.
Here’s an example of how you can use it to load ads only when they are visible:
// Script to lazily load ads using IntersectionObserver
document.addEventListener("DOMContentLoaded", function() {
const lazyAds = document.querySelectorAll('.lazy-ad');
let adObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
let ad = entry.target;
ad.src = ad.dataset.src; // Set the src to load the ad
ad.classList.remove('lazy-ad');
observer.unobserve(ad); // Stop observing once ad is loaded
}
});
});
lazyAds.forEach(ad => {
adObserver.observe(ad);
});
});
This example ensures that ads are loaded only when they come into the user's view, significantly improving perceived performance and reducing unnecessary resource consumption for ads that may not even be seen.
One of the reasons ads load slowly is the blocking of page rendering by JavaScript, especially third-party scripts that are injected into your page. To mitigate this, you can use the async or deferattributes on your script tags to ensure that the main content of the page loads first before ads.
Here’s a breakdown of how you can implement these strategies:
<!-- Async: Ads load as soon as possible without blocking the HTML parser -->
<script async src="https://ad-network.com/ad.js"></script>
<!-- Defer: Ads load only after the HTML is completely parsed -->
<script defer src="https://ad-network.com/ad.js"></script>
Explanation:
By deferring non-essential JavaScript files, including ad scripts, you significantly reduce the Time-to-Interactive (TTI) metric, leading to a better user experience.
Prefetching and preloading assets are powerful techniques for improving the performance of ads. Prefetch allows the browser to fetch resources for ads that will likely be requested soon, while preload forces the browser to download critical resources early, making them available sooner.
Here’s how to implement these techniques in your Next.js project for ad optimization:
<!-- Preload ad scripts to prioritize loading -->
<link rel="preload" href="https://ad-network.com/ad.js" as="script">
<!-- Prefetch future ad resources -->
<link rel="prefetch" href="https://ad-network.com/future-ad.js">
Why It Works:
These methods help streamline ad delivery and improve user engagement by ensuring that ads are loaded and visible when needed.
Download our service guide to understand how we can help you optimise your site speed
Another way to optimize ad performance is by reducing the size of the scripts and assets associated with your ads. Compressing and minifying JavaScript and CSS files used in your ads can significantly improve load times.
For example, here’s how you can configure Brotli compression in a Node.js server that serves your Next.js app:
// server.js - Adding Brotli compression
const express = require('express');
const compression = require('compression');
const next = require('next');
const app = next({ dev: false });
const handle = app.getRequestHandler();
const server = express();
// Enable Brotli compression with fallback to Gzip
server.use(compression({
level: 9,
threshold: 10240, // Compress assets larger than 10KB
filter: (req, res) => {
if (req.headers['accept-encoding'].includes('br')) {
res.setHeader('Content-Encoding', 'br');
}
return compression.filter(req, res);
}
}));
server.all('*', (req, res) => handle(req, res));
server.listen(3000, (err) => {
if (err) throw err;
console.log('Server running on http://localhost:3000');
});
By enabling Brotli or Gzip compression, you can drastically reduce the payload size of your ad scripts, reducing the amount of data that needs to be transferred over the network. This leads to faster load times and improved ad performance.
The efficiency of your Content Delivery Network (CDN) plays a major role in how fast ads load on your site. If your ads are being served from a geographically distant server, latency increases, negatively impacting load times.
Here’s a quick guideline on choosing the right CDN for your ads:
For example, here’s how you can integrate a custom CDN for your ad resources in a Next.js project:
// next.config.js
module.exports = {
images: {
domains: ['your-cdn.com'],
loader: 'imgix', // Leverage CDN for ad image assets
path: 'https://your-cdn.com/',
},
};
Using a CDN ensures that ad resources are delivered quickly, minimizing latency and improving the overall ad loading experience for users.
The speed at which ads load on your website has a direct impact on user experience, engagement, and, most importantly, your revenue. By optimizing ad loading using techniques such as lazy loading, script deferral, compression, and leveraging a reliable CDN, you can significantly improve the speed of your ads and, in turn, maximize revenue potential.
Implementing these strategies not only improves the user experience but also enhances ad viewability and reduces bounce rates, ensuring that your business capitalizes on every opportunity.
Catch Metrics is the leading solution to help solve ad stack performance problems.Get in touchto learn more from our experts
Download our service guide to understand how
we can help you optimise your site speed