XF 2.0 Prevent AdSense code from appearing in wrong parts of the template

JackieChun

Well-known member
For those of you who have "auto ads" enabled for AdSense, have you figured out how to prevent the ads from appearing in stupid places like inside header navigation? I am not used to not having control over where the ads appear, so this is new territory for me. I do understand that Google's algorithms are smarter and better than most humans are at optimizing ad placement, but they could really use a few lessons in usability.

In the case of my forum, a huge 1204x282 ad banner is appearing between the logo and the nav bar's section links, which is very ugly and is clearly the wrong place for it (even though it might earn more clicks). I would like to change that, if possible.
 
Try something like this in extra.less template:

CSS:
.p-header > div.google-auto-placed, .p-nav-Sticky > div.google-auto-placed, .p-section-Links > div.google-auto-placed {
display:none !important;
}

The code above will block auto ads from displaying anywhere within the header (p.header), navigation sections (.p-nav-sticky) and section links (.p-section-Links).

Edit the code by just removing or adding section classes as needed.
 
Ah, that's very clever! But I'm afraid display:none will not do the trick here. Google will only place an X number of ads per page. Sometimes it's only 1 ad. So if you're simply hiding the ad from visitors, you will be losing impressions, because Google's bots will think that the ad is placed correctly and working. It will probably also totally mess up your ad analytics, because some ads will have 0 percent click-through rates.
 
You might want to try it to check. I personally don't use auto ads just because of the lack of control, but I do use "display:none !important;" on Adsense ad classes to block specific ad sizes from showing depending on screen size.

Google sees the css code and recognizes the ad will not display and in turn does not attempt to show an ad. So all the analytics (impressions, CTR, etc.) remain accurate. In my case the css is applied directly to the ad and not the container, but Google may still recognize it.

You'll know if it's working the way you want if you see something like this in the rendered page code:
HTML:
<!--No ad requested because of display:none on the adsbygoogle tag-->
 
Last edited:
That is awesome! Thank you so much, I will definitely try this.

What does > in your code mean?

How about instead of banning an ad in the template outright, just ban huge ad containers like the example above? For example, do not display ads taller than 400px in the sidebar or do not display ads longer than 600px in the header?
 
The html code I posted above is created by the Google Adsense Script itself, the adsense script looks at the css applied to the ad prior to requesting the ad. Then in the place the ad would normally display it places this "commented out" code.

There are probably ways to get what you want with auto ads, but if you are going to spend that much time tweaking auto ads why not just use regular ad units and that may you know you will always get exactly what you want where you want with full control of position and size if you want it.
 
You're right, I'm not going to spend that much time on that, just wanted to have a code snippet handy in case I see something outrageous but don't necessarily want to ban ads in that location. It will also force Google to continue seeking out new ad spots.

You can't imagine how comforting it is to know that there are fellow forum owners who use AdSense and know what the heck they're talking about!
 
My suggestion is to use auto ads on guests and place fitting ad blocks for registered users.

Bounce rates with guests are high, of course, but CTR on an auto ad is much higher than a blended ad unit.

Give your members a better UI experience while still getting impressions, and possibly clicks, but lower.
 
I've put the code below in extra.less, and ads just stopped appearing in desktop view.

Code:
.uix_headerContainer > div.google-auto-placed {
display:none !important;
}

The ads are gone not just from the header but all other places where they used to appear, e.g. the sidebar. The ads still appear on mobile.

I also don't get see phrase posted by @bzcomputers appear anywhere in site code.
Code:
<!--No ad requested because of display:none on the adsbygoogle tag-->
 
It was worth a try, but it doesn't look like it will work correctly with auto ads. It appears the css needs to be attached directly to the ad, which can't be done with auto ads.
 
The ads are slowly starting to appear... in weird places, including another huge ad in the middle of the page that totally pushed the sidebar from the side and below the main container. 😂

Looks like the AdSense bots just need time to figure things out... and they'll be making mistakes and need a guiding hand along the way. This still beats manual placement because it will work against ad blindness by changing things up and also place more than 3 units per page on pages with a lot of content. Auto Ads will also run automatic experiments and there's machine learning involved. Human oversight + machine learning should be a winning combination. :D

But even with the ads appearing, I still don't see the language "<!--No ad requested because of display:none on the adsbygoogle tag-->" in the site code.
 
But even with the ads appearing, I still don't see the language "<!--No ad requested because of display:none on the adsbygoogle tag-->" in the site code.

You probably won't. I though it was a long shot to begin with using auto ads. I believe the Adsense script is probably only looking at the css applied directly to the ad and will not see the code applied to the container (which is all you can do with auto ads). Which means all your analytics will also be off if you are attempting to hide any auto ads.

Adsense is still not that smart. If you don't set specific ad sizes it can and will break page formatting often. Especially on a site running a pretty complex page templates like XenForo. Even the responsive ad units still break page formatting if you don't set specific size limits.

Yes, ad blindness is a thing, but I think Auto Ads are still a long way off from being the best way to run Adsense if you are looking to earn the most you can. Users will be much more turned off by ads "distracting" from their site experience because of odd placements or page flow breaking issues.

I'm personally more comfortable placing ads in places that are proven to get attention and clicks. One thing Adsense has greatly improved upon over the last couple years is what ads it serves. They not only match page content, but match a user's interests by looking at their internet use (browser history, previous ad clicks, purchases, etc.). The later of which in general gets a substantially higher CTR.
 
the Adsense script is probably only looking at the css applied directly to the ad and will not see the code applied to the container (which is all you can do with auto ads). Which means all your analytics will also be off if you are attempting to hide any auto ads.

I did some research, and it looks like modern browsers have evolved to simply not load code relegated as hidden in order to save resources and as a best practice. Which means analytics will probably be OK. It will be easy enough to check once the reports come in after a month or two.
 
@bzcomputers was right, AdSense is just not smart enough to optimize its own unit placement for XenForo's complex layout. I think I am going to combine manual and auto placement in desktop view and go full auto on mobile. Do you guys know the code to insert a unit between specific forum categories on the forum index page?
 
I recommend you get this: https://xenforo.com/community/resources/ads-manager-2-by-siropu.6153/

It is coded well and gives a lot more options for ad placements out of the box.

Including:
Capture.webp

...which is what you are currently looking for.

Not to mention is also does its' own analytics (impressions, CTR, etc.) which is nice to view right within XenForo.

I currently don't use 80% of what the add-on can do, but the 20% I do use is extremely useful.
 
Thanks, I'll take a look. It's on the pricey side at $55 per installation. If anyone can figure out the manual code for units between forum categories on the index page, please let me know.
 
Thanks, I'll take a look. It's on the pricey side at $55 per installation. If anyone can figure out the manual code for units between forum categories on the index page, please let me know.
Worth the $55 for me!

You probably won't. I though it was a long shot to begin with using auto ads. I believe the Adsense script is probably only looking at the css applied directly to the ad and will not see the code applied to the container (which is all you can do with auto ads). Which means all your analytics will also be off if you are attempting to hide any auto ads.

Adsense is still not that smart. If you don't set specific ad sizes it can and will break page formatting often. Especially on a site running a pretty complex page templates like XenForo. Even the responsive ad units still break page formatting if you don't set specific size limits.

Yes, ad blindness is a thing, but I think Auto Ads are still a long way off from being the best way to run Adsense if you are looking to earn the most you can. Users will be much more turned off by ads "distracting" from their site experience because of odd placements or page flow breaking issues.

I'm personally more comfortable placing ads in places that are proven to get attention and clicks. One thing Adsense has greatly improved upon over the last couple years is what ads it serves. They not only match page content, but match a user's interests by looking at their internet use (browser history, previous ad clicks, purchases, etc.). The later of which in general gets a substantially higher CTR.
We recently were contacted by Google for a “consultation”. After a week of them asking questions we would think they have the answer to their big revelation is that we should use auto ads. I have tried it before a year ago and it made a mess of the site.

Do you mind sharing what ad types and placements work well for you? We are using responsive unit above top breadcrumb as well as native in-line mixed with responsive after every 5th post in a thread. It’s not really doing that well.
 
Top Bottom