Skip to content

Add Google Adsense using code snippet

Watch tutorial on YouTube.

Google Adsense is one of the effortless ways to monetize your website with ads. You display ads on your website and earn money.

Use this code snippet to add Google Adsense to your WordPress website.

Code Snippet

First add your website to google adsense → https://adsense.google.com/

Default (not optimized) Adsense code

Important: Replace ca-pub-XXXXXXXXXXXXX with your Adsense ID.

/* change ca-pub-XXXXXXXXXXXXX with your ID from https://adsense.google.com/ */
add_action( 'wp_footer', function () { ?>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXX"
crossorigin="anonymous"></script>
<?php } );

Optimized (delayed) Adsense code

This version inserts Adsense code dynamically after page completes loading. All important elements like text, images etc. will be visible faster.

Important: Replace ca-pub-XXXXXXXXXXXXX with your Adsense ID.

/* change ca-pub-XXXXXXXXXXXXX with your ID from https://adsense.google.com/ */
add_action( 'wp_footer', function () { ?>
<script> 
/* GAS */
setTimeout(function() {
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.async = 'async';
newScript.crossorigin= 'anonymous';
newScript.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXX';
document.getElementsByTagName("head")[0].appendChild(newScript);
}, 1500);
</script>
<?php } );

FAQ

Which websites should not use Adsense?

Business websites that directly earn from customers should not use Adsense or display any other adverts. Because you do not want your customers leave your website and go to competitors.

Main goal of a business website is get leads, nurture leads and convert to clients. You do not want to distract potential clients with external ads.

Which websites are good for Adsense?

Websites that offer free content, games, images, fonts, calculators, quizzes, blog posts, user generated content, classified ads, informational articles, free educational content etc. Free content with lots of traffic can be monetized with Adsense.

So you can continue provide value for free and earn via ads.

Which Ad Placement performs best?

Automated placement always beats manual placements.

I use manual and automated ad placement across my websites. Best performer is automated ad placement with Vignette ads.

With manual placements you have to find best location for your ads and constantly optimize. Trying to show ads in custom locations is time consuming and not always easy.

Automated Vignette ads (full page ads that appear between page loads) works without any optimization. Place it and forget about it. It will always beat your manual placement.


What is next:

Leave a Reply

Your email address will not be published. Required fields are marked *