How to Implement Lazy Loading

How to Implement Lazy Loading

Summary

Lazy loading is a technique that improves page speed and ad viewability by pausing the loading of ads until they are about to enter the user's viewport. It is ideal for long articles or pages with infinite scroll. This guide provides step-by-step instructions on how to integrate lazy loading for both WordPress and non-WordPress sites.


1. What is Lazy Loading?

Lazy loading allows you to load pages faster, reduce resource consumption and contention, and improve the display rate by pausing the request and rendering of ads until they approach the user's window. This means that Lazy Loading consists of adding an ad tag asynchronously, every X paragraphs of your articles.

2. How does Lazy Loading work?

Our system normally provides only two megabanners (top and bottom) per page for several reasons:

-We don't want our publishers to abuse this format, which would result in lower CPMs. Too many banners on the same page lowers the average CPM of the page.
-Advertisers prefer pages where they are the only ones to advertise.
-Header bidding technology is more efficient if few formats are loaded in the header.
-Creating additional formats could create unnecessary latency for formats that are not necessarily visible at page load.

Lazy loading provides a solution to display more ads on long pages without these negative effects.

3. When to use Lazy Loading

When you have long articles or infinite scroll pages, we recommend dynamically duplicating our tags as you wish in iframes within the same page. 


Here is an example page: https://www.themoneytizer.com/demo/preview_lazy_loading

In this example site, the 2 megabanners at the top of the page are loaded normally via a copy-paste integration of our JS tags in the code, they load at the same time as the page. On the other hand, the two other megabanners that are located further down in the articles, which are located in the article's flow, are triggered as soon as their location becomes visible to the user during the scrollingof the page.


4. How to Integrate Lazy Loading

a) Lazy Loading with WordPress

Prerequisites:

  • Blank Slate extension

  • Ad Inserter extension

Implementation:

  • Step 1: Create the Ad Page

    1. Go to the "Pages" tab and create a new page.

    2. In the right sidebar, under "Page Attributes", select the "Blank Slate" template.

    3. In the page content, add a "Custom HTML" block and paste your ad tag.

    4. Publish this page and return to the Pages list.

    5. Rename the Slug field of this new page to tmz-lazy-loading

  • Step 2: Configure Ad Inserter

1.Go to the "Settings" tab then "Ad Inserter".
2.Select any of the 16 positions and add the following
    1. <iframe> code:


<iframe src="../tmz-lazy-loading" loading="lazy" scrolling="no" width="310" height="260" frameborder="0"></iframe>
 

ATTENTION: This code has the dimensions of a Top Medium Rectangle (300x250).

If you use a different format, you must modify this code with the correctdimensions, adding 10px as a precaution. For example, a megabanner code would be:


<iframe src="../tmz-lazy-loading" loading="lazy" scrolling="no" width="738" height="100" frameborder="0"></iframe>

3.In the settings below the code block, select "Posts".
4.In the "Insertion" drop-down menu, select "After paragraph".
5.In the middle field, add % followed by the number of paragraphs you want between each ad
(For example, %3 will add a tag every 3 paragraphs).
6.Select "Center" for the "Alignment" option 
7.Click "Save Settings 1-16".

b) Lazy Loading without WordPress

Prerequisites:

  • FTP access for your website

  • Notions in Javascript

Implementation:

Step 1: Create the HTML File Create a new file named “tmz-lazy-loading.html” via your FTP access. In this file, add only the ad tag you want to use.
Step 2: Use the iframe and Javascript
      1.You can now use the
<iframe> code below to display the tag multiple times on your site.
<iframe src="../tmz-lazy-loading" loading="lazy" scrolling="no" width="310" height="260" frameborder="0"></iframe>
ATTENTION: This code is for a Top Medium Rectangle (width:300 and height 250). If you use a different format,
you must modify the width and height, adding 10px as a precaution.

  • For example, if you choose a megabanner in step 1, your code will be:

<iframe src="../tmz-lazy-loading" loading="lazy" scrolling="no" width="738" height="100" frameborder="0"></iframe>


You now need to create a Javascript script that will target your paragraphs and add the iframe every X paragraphs of your articles.

Each script is unique to your site, so the example below will most likely need to be modified to work on your site:

HTML


<script type="text/javascript">

  (function() {

    setTimeout(function() {

      for (var j = 1; document.querySelectorAll("p").length > j; j++) {

        if (j % 3 === 0) {

    document.querySelectorAll("p")[j].insertAdjacentHTML('afterend', 

          '<iframe src="../tmz-lazy-loading.html" loading="lazy" scrolling="no" width="738" height="100" frameborder="0"></iframe>');

        }

      }

    }, 2000);

  })();

</script>



    • Related Articles

    • How to Integrate with the Ad Inserter Plugin

      Summary The Ad Inserter plugin allows you to manage many essential aspects of monetization on your WordPress site. This guide provides step-by-step instructions on how to use it for the integration of the ads.txt file, the CMP code, the placement of ...
    • How to Manually Integrate Ad Formats

      Summary If you choose manual integration, you will be able to add the ad formats to your site yourself. In your Moneybox, you will find all the necessary information for each format, including its size, a demo of how it looks, and where it needs to ...
    • How to Implement Your ads.txt File: A Complete Guide

      Summary Authorized Digital Sellers, or ads.txt, is an IAB Tech Lab initiative that helps ensure that your digital ad inventory is only sold through sellers who you've identified as authorized. Creating your own ads.txt file is a mandatory step that ...
    • How to Manually Integrate ads.txt on WordPress (Without FTP)

      Summary It is possible to manually integrate your ads.txt file on a WordPress website without FTP access by using a simple WordPress plugin. This guide provides the step-by-step instructions to install and use the "File Manager" plugin to add the ...
    • How to Manually Integrate a CMP Script on a WordPress Site

      Summary This guide provides step-by-step instructions on how to manually integrate The Moneytizer's Consent Management Platform (CMP) script on a WordPress website using the theme editor. This method is an alternative to using a plugin and ensures ...