exclude adsense
There are many reasons why you might want to disable Google AdSense on specific pages of your website. Whether it’s for privacy, better user experience, or policy compliance, selectively showing ads can be a smart move. In this article, we’ll explore how to do it properly, without affecting your overall monetization strategy.
To avoid Google policy violations
Google AdSense enforces strict content rules. Pages with sensitive or restricted topics (like adult content, mental health, politics, etc.) should not display ads to prevent account warnings or suspensions.
Example:
You have a blog section about grief support. Although helpful, it might be flagged as sensitive.
<?php if (!in_category('sensitive-topics')): ?>
<!-- AdSense Code -->
<?php endif; ?>
Landing pages are designed for user action. Ads may distract users, slow down loading time, or divert attention, leading to fewer conversions.
Example:
An ebook download page:
<?php if (!is_page('ebook-download')): ?>
<!-- AdSense Code -->
<?php endif; ?>
Or using JavaScript:
if (!window.location.pathname.includes("ebook-download")) {
// Insert AdSense code here
}
During checkout or account creation, every element should support the completion of the task. Ads can be distracting or create distrust.
Example:
<?php if (!is_page(array('checkout', 'register'))): ?>
<!-- AdSense Code -->
<?php endif; ?>
Pages like privacy policies, cookie notices, or legal disclaimers don’t generate monetizable traffic. Ads on these pages offer no value and might appear unprofessional.
Example:
<?php if (!is_page(array('privacy-policy', 'cookie-policy', 'terms-conditions'))): ?>
<!-- AdSense Code -->
<?php endif; ?>
Loading fewer AdSense scripts means faster load times, better user experience, and improved SEO metrics. Focusing ads only on high-traffic, monetizable pages increases CTR and earnings per page.
Disabling Google AdSense on selected pages is a smart way to comply with policies, improve user experience, and maximize ad performance. Here are the three most common methods.
if
statements in code (PHP or WordPress)If your site is powered by PHP or WordPress, you can conditionally control AdSense visibility with simple logic.
<?php if (!is_page('privacy-policy')): ?>
<!-- AdSense Code here -->
<?php endif; ?>
<?php if (!is_page(42)): ?>
<!-- AdSense Code -->
<?php endif; ?>
<?php if (!is_page(array('privacy-policy', 'checkout', 'register'))): ?>
<!-- AdSense Code -->
<?php endif; ?>
This method is highly reliable and ensures AdSense scripts are never loaded on excluded pages.
If you use a static site or a JavaScript framework like React or Vue, you can use a conditional check based on the URL.
if (!window.location.href.includes("privacy-policy")) {
// Insert AdSense code here
}
const excludedPaths = ["privacy-policy", "checkout", "register"];
const currentPath = window.location.pathname;
if (!excludedPaths.some(path => currentPath.includes(path))) {
// Load AdSense code here
}
Note: This method is less performant, as the AdSense code may still start loading before being excluded.
If you use WordPress, there are robust plugins to control ad placement:
<?php if (!is_page('checkout')) { ?>
<!-- AdSense code -->
<?php } ?>
These tools are ideal if you want to manage ads visually without coding.
When excluding AdSense from certain pages, it’s crucial to avoid implementation mistakes that can lead to account violations or performance issues.
display: none
Some developers attempt to hide ads using CSS like this:
<div style="display:none;">
<!-- AdSense code -->
</div>
Why it’s wrong:
The AdSense script still loads and Google considers this as manipulating ad visibility, which violates their policiesand may lead to account suspension.
Others include the AdSense script in the <head>
, but don’t actually show any ad elements:
<head>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>
<!-- No ads in the body -->
Why it’s wrong:
Even if no ad is shown, Google detects the script loading, which implies that ads are being served. This can lower your site’s quality score or generate policy warnings.
Use PHP or JavaScript conditions to completely skip script loading on excluded pages.
<?php if (!is_page(array('privacy-policy', 'checkout', 'register'))): ?>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXXX"
data-ad-slot="1234567890"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php endif; ?>
const excludedPaths = ["privacy-policy", "checkout", "register"];
const path = window.location.pathname;
if (!excludedPaths.some(p => path.includes(p))) {
const script = document.createElement('script');
script.async = true;
script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
document.head.appendChild(script);
script.onload = () => {
const ad = document.createElement('ins');
ad.className = "adsbygoogle";
ad.style.display = "block";
ad.setAttribute("data-ad-client", "ca-pub-XXXXXXXXXXXXXXX");
ad.setAttribute("data-ad-slot", "1234567890");
ad.setAttribute("data-ad-format", "auto");
document.body.appendChild(ad);
(adsbygoogle = window.adsbygoogle || []).push({});
};
}
If you’re optimizing your monetization strategy, remember that Dopstart offers a free first consultation. We can help you configure Google AdSense correctly, avoid penalties, and improve performance with a fully customized approach.
We will send you periodical important communications and news about the digital world. You can unsubscribe at any time by clicking the appropriate link at the bottom of the newsletter.
An interspecies communication breakthrough: Google develops an AI to decode dolphin vocalizations A voice from…
Adding PHP code to WordPress pages can unlock advanced customization, integrate third-party tools, or display dynamic content.…
Understanding your rivals is key. This article explores the importance of competitor analysis in digital…
Google Search Console is an essential tool for monitoring and optimizing your site's visibility in…
Our SEO Agency rarely has requests to optimize sites made with Google Blogger. However, it…
We have received several reports of problems with the Godaddy domain connected to Blogger. In…