XF 2.2 Trying to create conditional to block running ad code on all "prevent ads" templates

bzcomputers

Well-known member
Looking to surround the main Adsense async script with a conditional to allow it to only run if the current template is not included in the the list of "Prevent ads showing in these templates". Currently the way XenForo addresses blocking ads in certain templates does not prevent Adsense "Auto Ads" from showing in these templates. The main Adsense javascript, which is typically referenced outside of any specific ad code block, is the only Adsense code that is required for "Auto Ads" to load, which means "Auto Ads" will show on all templates, which breaks Adsense TOS.

I've seen this code mentioned previously:
Code:
in_array($template, $xf.options.adsDisallowedTemplates|split('nl'))

and was trying to do something like...
Code:
<xf:if is="!in_array($template, $xf.options.adsDisallowedTemplates|split('nl'))">
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"</script>
</xf:if>

but the above code does not work as I attempted to include it. Any help with that conditional or similar would be appreciated.
 
Solution
Code:
<xf:if is="$xf.app.templater.isA($xf.app, '\XF\Pub\App') && !in_array($xf.reply.template, $xf.options.adsDisallowedTemplates|split('nl'))">
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"</script>
</xf:if>
This is odd because on my forum the Adsense script doesn't seem to run on the disallowed templates.

Edit: Just doing some more testing - maybe it does!
 
After more testing It does not seem to be showing on disallowed pages.

Are you using "Auto Ads" like Anchor Ads?

-------------------------------------------------------
I assume many like me run the main Adsense javascript code (adsbygoogle.js) only once per page by referencing it somewhere like the helper_js_global template, so it is only referenced once per page no matter how many ads may exist on a page. This works great until you turn Adsense "Auto Ads" on. With "Auto Ads" on this single javascript reference will allow Auto Ads to show on every page, completely bypassing the "Prevent ads showing in these templates". The "prevent ads showing" template references only block individual ad codes setup through Advertising, and currently XenForo has nothing in place to block the main Adsense javascript that you may place elsewhere.

The solution for now is to surround your main Adsense javascript code with a conditional like I'm trying to do above.

The best way to fix this is to eventually have Xenforo add an additional Advertising setting for admins to be able to place custom Adsense code once per page and limit it to running on just the templates not listed in "Prevent ads showing in these templates".

Right now most XenForo sites using Adsense "Auto Ads" are likely breaking TOS by placing these Auto Ads on pages where Adsense does not allow advertising.

For reference here is a pretty thorough list of XenForo 2.x templates that advertising should not be showing on in order to not break Adsense TOS:
Code:
account_alerts
account_avatar
account_banner
account_bookmarks
account_connected
account_connected_associate
account_details
account_email
account_following
account_ignored
account_preferences
account_privacy
account_reactions
account_security
account_signature
account_upgrades
account_upgrades_purchase
account_username
approval_queue
contact_form
conversation_add
conversation_edit
conversation_invite
conversation_leave
conversation_list
conversation_reply
conversation_view
error
find_threads_list
forum_post_thread
help_index
help_page
login
login_password_confirm
login_two_step
lost_password
lost_password_confirm
message_page
news_feed
notice_dismiss
online_list
register_complete
register_confirm
register_connected_account
register_form
report_list
search_form
search_results
watched_forums_list
watched_threads_list
whats_new_profile_posts
xfmg_category_add
xfmg_media_edit_image
xfmg_watched_albums
xfmg_watched_categories
xfmg_watched_media
xfrm_category_add_resource
xfrm_resource_edit
xfrm_watched_categories
xfrm_watched_resources
xfrm_overview
xfrm_resource_view
xfrm_resource_updates
xfrm_resource_history
xfrm_thread_view_type_resource
 
Are you using "Auto Ads" like Anchor Ads?
Yes I have done but don’t like it. However they never showed on the disallowed pages. I ran a test today with auto anchor ads at top and bottom after seeing your post and they showed only on the pages/templates not in the disallowed list. Nor show on page source for those pages, nor for user groups that are excluded.
 
Code:
<xf:if is="$xf.app.templater.isA($xf.app, '\XF\Pub\App') && !in_array($xf.reply.template, $xf.options.adsDisallowedTemplates|split('nl'))">
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"</script>
</xf:if>
 
Solution
Code:
<xf:if is="$xf.app.templater.isA($xf.app, '\XF\Pub\App') && !in_array($xf.reply.template, $xf.options.adsDisallowedTemplates|split('nl'))">
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"</script>
</xf:if>

Thanks for the attempt but that does not seem to work for me Kirby. (Edit: Code is working fine.)

Yes I have done but don’t like it. However they never showed on the disallowed pages. I ran a test today with auto anchor ads at top and bottom after seeing your post and they showed only on the pages/templates not in the disallowed list. Nor show on page source for those pages, nor for user groups that are excluded.

It may be because I am using a slightly modified code that forces anchor ads to only be on the bottom so it doesn't interfere with the XenForo menu system.

Code:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXX" data-overlays="bottom" crossorigin="anonymous">
 
Last edited:
Well, it does work just fine for me.

What's the issue you are experiencing?

I was still seeing ads on pages/templates referenced in the "Prevent ads showing in these templates" list. I'll go back and double check, it may have been a caching issue but I thought I cleared cache.

----------------------
Edit: Appears it was a caching issue just retested your code and it is working perfectly, thanks. Out of curiosity what is the first portion of the conditional $xf.app.templater.isA($xf.app, '\XF\Pub\App') actually checking?
 
Last edited:
Back
Top Bottom