Direct Answer & TL;DR
- Script Injection: Add the AdSense snippet to
layouts/partials/head/custom.html. - Ownership Verification: Create
static/ads.txtand include your publisher information. - Review & Approval: Wait for Google’s final review process to complete for approval.
How to Initialize Google AdSense on a Hugo Blog
After building a blog with Hugo, many users look for ways to integrate Google AdSense to sustain their platform. However, since Hugo is a Static Site Generator (SSG), the configuration process differs from traditional CMS platforms like WordPress, which can be challenging for those unfamiliar with Hugo’s layout system.
When I first transitioned to Hugo, identifying the optimal placement for the AdSense script within the theme’s layout structure was one of the initial challenges I encountered.
Today, I’m providing a definitive, “clean” guide to setting up AdSense in Hugo (specifically for the hugo-theme-stack), based on my own practical implementation. This is Part 1 of our AdSense series, focusing on the technical configuration steps.
Injecting AdSense ads into a Hugo blog with a bit of humor (and duct tape?)
Injecting the AdSense Code Snippet
The first requirement for AdSense approval is placing their code snippet into your blog’s <head>.
You need to copy this code from AdSense and embed it in your blog.
Since Hugo is a Static Site Generator (SSG), there’s no “Header Plugin” to click. However, managing it manually is actually much cleaner once you know the pattern.
Using a Custom Partial
If you’re using the hugo-theme-stack, you should use the Override method instead of modifying the theme’s core files.
- Create a directory
layouts/partials/head/in your project root. - Create a file named
custom.htmlinside it. - Paste the AdSense script exactly as provided.
<!-- layouts/partials/head/custom.html -->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
crossorigin="anonymous"></script>
By doing this, hugo-theme-stack will automatically include this script in the <head> section during the build process. Plus, your settings won’t disappear when you update the theme!
Proving Ownership with ads.txt
Have you seen the “Earnings at risk - You need to fix some ads.txt file issues” warning? This document tells the ad system: “This site is mine, so send the revenue to me!”
In Hugo, the static/ directory is mapped directly to the root of your site.
How to set it up:
- Go to your project’s
static/folder. - Create a text file named
ads.txt. - Paste your publisher info from Google:
google.com, pub-XXXXXXXXXXXXXXXX, DIRECT, xxxxxxxxxxxxxxxx
After setting this up, visit https://yourdomain.com/ads.txt. If the text appears, you’re all set! (Show me the money…)
With the verification file uploaded, the technical setup is complete. Now, it’s time to wait for Google’s decision.
Understanding the Site Review and Approval Process
Ads don’t show up the moment you save your code. Now comes the AdSense review process—the period where even the most patient bloggers might start refreshing their email every hour. This process generally takes a few days, but can sometimes stretch up to two weeks.
- Site Review Underway: Google’s bots will crawl your site to check for policy compliance and content quality. Make sure your site is easily navigable.
- The “Hands-Off” Rule: Try not to delete the AdSense code or make drastic layout changes during this phase. Consistency is key for the bots to verify your ownership and content.
- Keep Writing: Use this waiting time to post one or two more high-quality articles. It shows Google that your blog is active and growing, which definitely helps with the approval odds. (
Don’t worry, I also checked my mailbox like 20 times a day during my review period😅)
Conclusion: Configuration is Only the Beginning
The technical setup is done! Now comes the real challenge: consistent posting. (I know, writing is hard… I feel your pain.)
If you have any questions or if your theme has a different layout structure, drop a comment! I wish you a speedy approval and high earnings! 🚀