All Articles

Lets add google ads with no code changes!

This is the same way to add google analytics

Go to your site -> Site settings -> Build & deploy -> Post processing -> Snippet injection

Click the drop down -> hit before body.

To save you some time here is the code I use to dynamically inject the ads.

This script will include the google ad scripts. wait 800 ms and add the google ad block to your sidebar. wait 1.5 sec to start serving ads.

These times could be shortened, but i put them this way to ensure the page is fully loaded.

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=YOUR-AD-ID"
     crossorigin="anonymous"></script>

<script>
 setTimeout(() => {
document.querySelector(".sidebar-links").appendChild(document.createElement("div")).innerHTML = '<ins class="adsbygoogle" style="display:block" data-ad-client="YOUR-AD-ID" data-ad-slot="1001042129" data-ad-format="auto" data-full-width-responsive="true"></ins>'
}, 800);

</script>
<script>
 setTimeout(() => {
(adsbygoogle = window.adsbygoogle || []).push({});
}, 1500);
     
</script>

Published Apr 6, 2022

Looking for something new