Using Adsense in Next js needs a different approach than a traditional request-based website. This article explains how to use dynamically inserted Google Adsense ad units in a Next js application. You will be able to retrieve and display ad units embedded in your post pages.
Traditional Google AdSense implementation, most of the time, does not work with websites built with Next js. The traditional Adsense implementation in Next js works in some situations but not always. In this article, I will explain when it works and when it doesn't. We will also look at implementing Adsense for websites built with Next js.
If you have already registered with Google Adsense, you may have the Ad code handy to be inserted into your HTML code. Your ad code may look like the one below.
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-xxxxxxxx"
data-ad-slot="xxxxxxxx"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Note that if you have multiple ads on one page, you don't need to repeat
In Next js, you only need to load the
If you have difficulty finding such a page,
Go ahead and place
script in the
Remember I mentioned situations that don't always work with Next js? For the sake of understanding, I will give some explanations.
Now place the remaining script on places where you want to show ads on your page.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-xxxxxxxx"
data-ad-slot="xxxxxxxx"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
That is the Adsense standard implementation. This approach works in some situations but not always.
The standard Adsense implementation works in Next js only with the initial page load or page refresh. Any internal navigation that occurs with Next Link does not trigger Adsense. Therefore, your visitors will see ads on pages they land, not on pages they navigate through internal links.
In Google Adsense, each ad unit needs individual initialization by running
In other words, five ad units need five pushes by running the script five times. In Next js, page scripts are executed only when the page is initially loaded or refreshed. That's why the standard Google Adsense implementation doesn't always work with Next js. Any subsequent internal navigation that happens with Next links does not trigger page scripts, therefore
There is no standard or unique way to do this. But I will show you one way to solve the problem. Our goal is to execute the push script
Remove the script from each ad unit.
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
So, your each ad unit will look like below.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-xxxxxxxx"
data-ad-slot="xxxxxxxx"
data-ad-format="auto"
data-full-width-responsive="true">
</ins>
As we discussed earlier, this can be your layout or
useEffect(() => {
}, []);
If your ad unit count is always the same, execute the
script number of times equivalent to the number of ad units.
Our solution assumes the number of ad units on each page can vary.
To execute the push script for each occurrence of the ad unit, we need to know how many ad units are on the page.
We can use the existing ad unit class name
useEffect(() => {
var ads = document.getElementsByClassName("adsbygoogle").length;
for (var i = 0; i < ads; i++) {
try {
(adsbygoogle = window.adsbygoogle || []).push({});
} catch (e) { }
}
}, []);
This solution will solve the Adsense problem with Next js applications. Suppose you have any client-side rendered components on your page, like sidebars loaded asynchronously. In that case, you must ensure the Adsense initialization script is triggered after all the ad units are loaded on the screen.