1 min read

How to create Google AdSense "Ads.txt" file using Netlify build command

How to create a Google AdSense "Ads.txt" file using the Netlify build command. It suggests a workaround to add dynamic files without making them public or adding them to a repository. The post provides instructions on accessing the site settings, adding content to the file using the command line.
How to create Google AdSense "Ads.txt" file using Netlify build command

If you want to add Google AdSense to your site, and you're using Netlify, like me, you encounter a problem with the "Ads.txt" file, maybe you don't want to make this file public, or probably you don't want to add this to a repository (is this my case).

In this opportunity I tell you a "hack" to add dynamic but, simple files using Netlify and the command line.

First you need to go to your site settings -> build & deploy then scroll down to the Build settings

Now in the Build command you can add files and content to this file

yarn build && touch ./public/Ads.txt && echo "google.com, pub-7660824024567312, DIRECT, f08c47fec0942fa0" >> ./public/Ads.txt

What happening here ?

Well I'm using the command line to create the file Ads.txt with the touch command, then using the echo command I put the content inside the double quotes on the file using the >> operator.

In fact, this is no a great way to do this, but working when you don't want to add some simple files on a build.