admin管理员组

文章数量:1201413

I have a problem in a project where an AdSense Auto Ad code has been pasted, one of the ads automatically placed have ruined the web page layout and design. Is there a method to prevent a specific ad to be shown inside a certain element or a container. Here is a sample of what the ad has done:

Code before AdSense Auto Ad was implimented:

 <div class="row">
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>

Code after AdSense Auto Ad was implimented:

 <div class="row">
  <div class="col-md-4">
   somecontent
  </div>
  <div class="google-auto-placed">
    ad content
  </div>
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>

I have a problem in a project where an AdSense Auto Ad code has been pasted, one of the ads automatically placed have ruined the web page layout and design. Is there a method to prevent a specific ad to be shown inside a certain element or a container. Here is a sample of what the ad has done:

Code before AdSense Auto Ad was implimented:

 <div class="row">
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>

Code after AdSense Auto Ad was implimented:

 <div class="row">
  <div class="col-md-4">
   somecontent
  </div>
  <div class="google-auto-placed">
    ad content
  </div>
  <div class="col-md-4">
   somecontent
  </div>
  <div class="col-md-4">
   somecontent
  </div>
Share Improve this question edited Jul 5, 2018 at 5:47 user5380448 asked Jul 5, 2018 at 5:16 Surya NeupaneSurya Neupane 98410 silver badges20 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 11

My solution for this problem is only with CSS. If I don't want to display auto placed google ad in specific container then I add some class to that container, e.g. "no-ads".

Then my CSS is simple:

.no-ads .google-auto-placed {
   display: none !important;
}

I just found an easier way to do this.

You should go :

  • Sign in to your AdSense account.
  • Click Ads.
  • On the "Auto ads" page, under "Global settings", click edit Edit button.

Here you can see how would google generate the auto ads in specific page. you can also hit a remove button on the ad it self.

You could try to make a JavaScript file that executes after (important word) the Google AdSense script. Something like:

var ad = document.querySelector(".google-auto-placed"); //Can be replaced any identifying trait depending on the actual Ad Div
ad.innerHTML="";
<html>

<body>

  <div class="row">
    <div class="col-md-4">
      somecontent
    </div>
    <div class="google-auto-placed">
      ad content
    </div>
    <div class="col-md-4">
      somecontent
    </div>
    <div class="col-md-4">
      somecontent
    </div>

</body>

</html>

This removes the ad (if it is executed after the Google AdSense script). However, this may set of AdBlocker detectors.

Essentially what this does is it selects the google-auto-placed class using document.querySelector(); which will select a specific element depending on the prefix, . for class in this case. Sadly, this may not work with some older IE versions. If you have multiple elements you may want to check the parentElement to check if it is the correct ad. You can read about that here.

To read up more about document.querySelector() have a look here.

If you don't want to see auto ads you can close it ("google-auto-placed" ads.) Just turn off auto ads in adsense page. Your unit ads still will be showing. https://support.google.com/adsense/answer/9214966?hl=en

I recommend against using CSS to hide AdSense ads. Here's why:

1. Ad Serving and Bidding Process:

When an AdSense Auto Ad is placed on your site, the ad request triggers a bidding process where advertisers bid to have their ads displayed. This process occurs regardless of whether the ad is ultimately visible to the user. If you hide the ad with CSS (e.g., using display: none), the ad has still been served and counted as an impression. This means that the ad space was filled, and your account will be billed for the impression, even though users didn’t see the ad.

2. Resource Usage and Performance:

Even though the ad is hidden, the browser still downloads all the associated resources (like images, scripts, and iframes). This can increase page load times, consume additional bandwidth, and potentially degrade the overall performance of your site. Since these resources are fetched without contributing to the user experience or generating revenue, it’s a waste of both server and client resources.

3. Impact on Viewability and Revenue:

Viewability is a critical factor in ad performance. Ads that are hidden have zero viewability, which can negatively impact your overall ad quality score. Over time, this can lead to lower bids on your ad inventory, reducing your potential revenue. Advertisers are less likely to bid high on placements that historically have poor viewability metrics.

4. Policy Compliance Issues:

Google's AdSense policies require that ads be visible and not manipulated in ways that prevent users from seeing them. Hiding ads with CSS could be interpreted as an attempt to circumvent these policies, leading to warnings, reduced ad serving, or even suspension of your AdSense account.

Recommended Approach:

Instead of hiding ads with CSS, try Google’s AdSense tools to manage Auto Ads placement effectively. Google allows you to preview your Auto Ads placement in real time and disable the locations you find undesireable.

add a class to row div from:

<div class="row">

to

<div class="row" class="no-ads">

this will inform adsense script not to place ads in that div only

本文标签: javascriptPrevent AdSense Auto Ad from showing ad in specific areaStack Overflow