How to add ads.txt to Discourse the "easy way" without a plugin

All web admins know that just about every advertiser requires the ads.txt file if they want optimal rates for publishing ads.

The official discourse plugin offers ads.txt support; but what happens if you are running your own ad code in a template component or simple theme modification?

Well, there is a very easy way to do it; and here it is:

Let's say you are running the Discourse standard install app with standard shared volume at the location:

/var/discourse/shared/standalone

Simple copy your ads.txt file to this shared directory, so it looks like this:

/var/discourse/shared/standalone/ads.txt

Then add this line to the bottom of your app.yml file in the run section:

run:
  - exec: echo "Beginning of custom commands"

  - exec: cp /shared/ads.txt /var/www/discourse/public

  - exec: echo "End of custom commands"

Now, when you rebuild your container your ads.txt file will work perfectly without the need for a plugin and you can run any custom ad tag you want directly by adding an <iframe> ad tag to the theme.

Because ads.txt generally does not need to change often, the method will work for most people.

Let's say you get a new ad client, you run a single container configuration, and your client wants you to update your ads.txt file; you can manually add the entry to your ads.txt file in the container at location:

/var/www/discourse/public/ads.txt

and you can also update your staged version of ads.txt so the next time you rebuild your container, you will not need to manually edit the file in the container:

/var/discourse/shared/standalone/ads.txt

If you run multiple containers behind a reverse proxy like we do, this is not an issue at all, because rebuilding and launching a new container and switching it over live takes zero downtime anyway.

In a nutshell, it is really easy to run ads.txt in this manner, or you can use the official Discourse plugin if that works best for you!

Questions or Comments?

2 Likes

Here is the log file entry for this action during container rebuilds:

I, [2022-03-19T04:12:13.407366 #1]  INFO -- : > echo "Beginning of custom commands"
I, [2022-03-19T04:12:13.408624 #1]  INFO -- : Beginning of custom commands

I, [2022-03-19T04:12:13.408714 #1]  INFO -- : > cp /shared/ads.txt /var/www/discourse/public
I, [2022-03-19T04:12:13.410334 #1]  INFO -- : 
I, [2022-03-19T04:12:13.410505 #1]  INFO -- : > echo "End of custom commands"
I, [2022-03-19T04:12:13.411937 #1]  INFO -- : End of custom commands

In this case, ads.txt exists but is empty, since we do not run any display ads on this site. However, having the ads.txt file removes the 404 error from the apache2 log file, which is good.

Screen Shot 2022-03-19 at 10.57.17 AM

BTW, we have tentative plans to run display ads on community.unix.com to guest users someday to encourage guest users to sign-up.