Inserting a custom script after Header

Hi guys,

I already have the official discourse ad plugin working for me and showing ads on my site. The question is what if someone wants to place any custom script or ad that isn’t any of the ones that are built-in with Discourse?

I tried going to customize->theme->edit html/css->after header. I pasted a script this script block there and reloaded my site but I don’t see it. In fact, the only time I can see it is if I put it in the which causes the ad to appear at the very bottom of the page. The “After Header” however is the best position and it doesn’t load. Mind you, there is the async in this script.

I mean why do we need a complicated plugin to simply copy paste an html block? Even Google gives you their script and you can simply copy paste it after the header. This is what people normally do for websites, but it doesn’t seem to work for Discourse because something is blocking the script. It’s not CSP since I have https: and inline-code already as filters which already should allow every js that starts with “htttps”. Here’s a google ad:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Ad 1 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-3368174556925488"
     data-ad-slot="4362547659"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Try to make that above code work manually without using the Adsense plugin and place it in the theme after the header.

#plugin:advertising

Update: Apparently, the answer was House Ads. However, even when posting some scripts in House Ads, those ads don’t show up so Discourse is blocking something since if I post the same script on any html page, it will show up.

1 Like

You can try this using a theme component now. Have you tried it?

Spoiler: ads won’t refresh because Discourse is a single-page app. If AdSense has changed and can now automatically refresh ads without using adsbygoogle.push({}), then it’s news to me.

1 Like

Actually, I just ended up using iframe. I put an iframe inside House Ads and that worked. The ads from Google Adsense do update if you refresh the page.

The only disadvantage is that if I want to place an affiliate ad from House Ads at the bottom of the page, there’s no option to do that. I can only place it at the top of the topic list or between. It would be nice if bottom is also added, giving us more freedom.

Except that Google will not recognize the subject of your forum and will serve your visitors ads that are way less relevant, totally tanking your revenue.

They won’t if you simply navigate to another page.

2 Likes

Please provide a reference for this statement.

It should make no different if ads are served in an IFRAME tag or a SCRIPT tag for Google AdSense and other ad bots to crawl the site for context.

Looking forward to reading some Google developer references for this, for sure!

Thanks.

When an AdSense ad is served inside a nested iframe, browser security features prevent the ad from detecting the page URL or domain to use for targeting. As a result, the most relevant ad is not served and ad revenue is lower than it could be.

3 Likes

Yes, but that is only a nuance, not the entire picture of how ad context is created.

All sites which run Adsense are crawled by the Google Adsense crawler, so Google servers ads in context to the site based on the crawler which crawls the site.

This means that a tech site which discusses Ruby on Rails, which get served ads relevant to that context (if available), on a site level.

However, on the page level, of course there is an issue with context in nested IFRAME tags.

Not all ad tags served in IFRAME tags are nested.

Here is an example of nested IFRAME tags:

<iframe src="http://www.w3schools.com" width="1000" height="1000" id="frame1" name="frame1">
   <iframe width="200" height="200" src="http://www.bing.com" id="frame2" name="frame2">
   </iframe>
</iframe>

Personally, I have rarely seen this happen on a practical manner, to be honest; and when we check it (which we have done many times over the past 15 years or so), the only time we have seen this issue is with passback tags.

Hope this helps.

You wanted a reference. That’s not what they meant with ‘nested’. That is clear from the paragraph title “How can I serve an AdSense ad inside an iFrame?”. Using an iframe even was a policy violation in the old days.

I’ve tried every trick in the book with Adsense since 2007 and I wrote the first Discourse Adsense plugin with @michaeld.

I guess we both have different experiences. Good to hear that you managed to get away with it. For anyone else wanting to try it: I won’t be there to dry your tears.

3 Likes

Well, to be frank, we have served over 1,000,000,000 ads over the past decade, and have not served AdSense directly in many years (we don’t like Adsense for many reasons), so let’s look at this “ad serving” from a high level view, from the perspective of what is going on now, in display advertising.

Context (Relevance)

Most ad networks which serve direct-buy ads, like for example, IDN Technet, or other context-based ad networks, do not crawl sites for content. They market the site to their ad buyers, and for example will say, “Hey this is Discourse meta and they have great content for all things tech dev, ruby, javascript and so forth”. We highly recommend them.

Then, they provide the publisher an ads.txt entry this days and they will serve ads to the publisher from their ad network based on the site category and subcategories, not from a crawler (like the Adsense crawler).

This is the rule these days, and not the exception.

The same is true for video content and video-based ads. Our video partner sells ads for our site based on the context of the site, in a brochure, not based on a “crawling robot” looking for context.

In fact, we have not served ads for years from any ad network which depends on a crawler to classify a site; especially for passback tags. Passback tags always work best when the context his hard-coded into the ad network, versus derived from a crawler doing classification.

Passbacks

It is always hard to get relevant context from passback ads (tags).

My experience from a publisher perspective who has served well over 1 billion display ads over the years is to not use an ad network which develops context context from a crawler for passbacks ad networks.

It is best, based on my experience publishing ads over the last decade, to use passback tags based on hard-coded context, for example, if I have a tech site, and I want to serve passbacks to India, then I will use passback tags of tech interest to techies from India.

AdSense

We used Adsense maybe 15 to 10 years ago, and for the past decade I cannot recall when we served AdSense directly. AdSense is not really a great way to generate revenue, in my view.

Adsense does develop context from their crawler, so in general, if your tag is not a nested IFRAME tag (an IFRAME passback tag within an iFRAME tag), you will be fine with IFRAME tags.

Some Advantages of IFRAME TAGS

  • The browser allocates some space for an inline frame beforehand and takes this into account when rendering the document.

  • Iframes implement the cross domain origin policy which can be useful for pulling in sites / content/Ad from other domain names and they are also relatively safe and that is why most of the web advertising solutions are based on iframes

  • If contents to be loaded first and then Ads , it can be done using Iframe.

  • rame helps in improving performance.

  • Iframe loads independently from other components on the page.

  • If a user has javascript disabled, iframes will work.

  • An iframe tag has attributes “height” and “width,” which allows the designer great latitude with dimensions and format like 300×250 , 728×90 depending on the Ad size.

  • Iframe tag can appear anywhere on the page and several iframes can be added if wished to.

Some Disadvantage of IFRAME TAGS

  • Iframe tags may cause accessibility problems.

  • Iframe tags are not supported by all the browsers and can display them improperly or even can serve blank.

  • A key reason for using iframe in web advertisement is security in cross domain support but performance is not perfect.

  • Iframes can not be seen by many handheld devices.

  • Iframe have been known lower SEO, at times.

Advantages and Disadvantages of JS Tags:

  • Can add this later if anyone is interested.

Supply and Demand

This is the year 2020. The number of web-site supplying advertisers with ads has grown exponentially since 2000. The supply for ads has far out driven the demand. The means that for most sites, the ones who are here talking about setting up ads on Discourse, it is not likely they are going to get premium ad traffic unless they have a strong demand and an established niche audience.

Every techie and his sister can publish ads on their blogs and forums in the year 2020 and they do. This means that the supply will continue to outgrow the demand for display ads into the future. Most niche sites will have a hard time to generate ad revenue from a new site until they become well established.

Discourse and Crawlers

Discourse does not serve the SPA to crawlers, so this means that adbot crawlers will also not see the SPA and the full implications of this have not been “third party, objectively” analyzed (by an independent third party), to my knowledge.

Summary

I think the vast majority of new Discourse users running Discourse as a Javascript SPA will not generate great revenue from display ad revenue networks (like Adsense). This is especially true in 2020 where the ad budgets of advertisers are much smaller due to the pandemic. In addition, the supply is high, and demand is low.

If you run display ads, you will have issues on all SPA because of how the pages “do not reload” and this is an issue with JS tags and IFRAME tags; there needs to be better plugin code for SPAs to optimize all types of third-party tags.

Closing

We have not serve Adsense for many years and have no plans to do so in the future; so the point of how Adsense gains context is moot for us. For others, It’s better to run working IFRAME tags than to pull your hair out trying to fight SPA issues serving third-party script tags and CORS headaches, IMHO.

Crawl before you walk. Walk before you run.

Hope this helps.

Take care.


PS: Sorry for any typos, my wife is waiting by the door and want to go shopping before it rains!

This is not for Google. As discussed earlier, this is for affiliate ad units that aren’t Google, amazon, etc. For Google, I already use the built-in Adsense plugin. My Google ads already work and adapt the subject of the ad to my visitors. In addition, I can control the genre of the ad through Adsense.

My question pertains to a static affiliate ad that will always be the same. Hence, I wanted to use House ads which is designed for that purpose. Yet, it limits me in terms of position to place that ad unit.

Currently, I’ve just used iframe and placed it at the footer of my theme html which works just fine. The only problem with this is that the iframe will display for all user groups even those with high trust level when before I could choose not to display the ad units to users with trust level 3 or greater through the ad manager in Discourse.