Where to place ad script?

It’s kind of difficult to address your points without context. It would be very helpful if you can include more specifics about what ad network you’re referring to and how exactly it did not work for you.

That’s not entirely true and it really depends on the ad network you’re trying to use. If it’s a strict requirement that the script for the ad network is placed next to the </body> tag - which is unlikely - then it’s really the ad network’s fault for having such a strict and unreasonable requirement.

Scripts added in a theme are pulled into a theme.js file that is then injected in the <head> tag of the document.

https://github.com/discourse/discourse/commit/6acdea37c4e77723c774152e9076dfedda99493a

This gives us some level of control and allows us to implement some helpful features like preventing broken themes from taking down the entire site, for example.

https://github.com/discourse/discourse/pull/7589/files

This is also only partly true. Themes can modify anything on the client-side. What that means is that anything written in JavaScript in Discourse is accessible by themes.

In fact, the entire official ad plugin can be converted to a theme component and it would work. The only thing that would be missing is the ability to add a ads.txt file because that would require access to the back-end. The reason why we haven’t done so is because a plugin is a cleaner implementation for this purpose - namely because it includes more than one provider. So yeah, themes have limitations but that’s not really the main point here.

There’s a very important note here, Discourse is a single page application. This means that once you load the app on initial page view, all navigation that occurs after that is handled by Discourse and not your browser.

This is important to note because an ad network has to provide the hooks necessary for single page applications to update the page information and serve new ads. I don’t know much about Adbutler or Openx, but I couldn’t find any documentation for single page application support by either provider.

So, if the ad network doesn’t provide the necessary hooks, is it really Discourse’s fault that it’s not supported?

You aren’t really forced to use DFP. It’s only one of six options available in the official ad plugin and I’ll list them here for reference.

  1. DFP
  2. AdSense
  3. Google ads
  4. Amazon advertizing
  5. Codefund
  6. Carbon Ads

Again, because Discourse is a single page application, “generic” just won’t work.

Sure, you can load whatever ad scripts you want in a theme, but they would only fire on the initial page view. Anything that comes after that will be missed by the ad network script - unless the ad network provides the necessary hooks.

Here’s what such an integration looks like on an ad network that supports single page applications.

https://github.com/discourse/discourse-adplugin/blob/master/assets/javascripts/discourse/components/google-dfp-ad.js.es6#L191-L230

You do actually need to have specific code implementations for every single ad network.

Also, I think you’re mixing up house ads with ads from ad networks. The reason why the box is HTML only is because you’re supposed to use it to create your own in-house ads. It’s a very straightforward implementation that’s already been wired up in the plugin so that you don’t have to fuss around with single page hooks if you just want to serve your own ads.

Further, if you really want another ad network to be included in the ad plugin, then there’s precedent for us adding new ad networks based on community contributions.

The most recent example of that being the Carbon Ads integration

https://github.com/discourse/discourse-adplugin/pull/73/files

However, the burden to wire up everything correctly falls on you here.

If you find such implementation to be too tricky, then that’s that. I’m very competent at changing the tyres or the oil in my car, but if I need to service the transmission, then I just simply find someone who knows more than I do and pay them to do it for me.

What I’m getting at is that if you really want to use another ad network and can’t quite pull it off yourself, then I suggest creating a topic in the #marketplace and I’m sure you’ll get some developer’s attention if you have the right budget.

9 Likes