Adsense for Hexo Blog

If you have a Hexo based blog, and would like to add adsense ads, you could do it in two ways (auto/manual).
Adding an ad depends strongly on the blog theme you use, and can be slightly different in your theme.

Preconditions are:

  • You have an adsense account

Auto ads

The easiest way is to add ads with “Auto ads”. It will add ad automatically based on your settings, to your page.
Adsense will identify the blog posts and will add “magically” ad to your post.

  • Go to Adsense -> “Auto Ads”
  • Adjust “Global” / or URL specific settings
  • Click on “Setup auto ads” and copy the code snippet

To let it work, we just need to add the script we copied to the html header part of the blog.

The theme “Next“ I use, provide you a custom folder where you can add your own scripts.
The custom folder already has 3 files (head, header, sidebar). Just add the script you got, to the “head.swig” file.

Manual ad placement

  • Go to Adsense -> “Add units”
  • Create a “new ad unit” you like
  • Copy the code you get

Like I said, adding an ad depends strongly on the blog theme you use, and can be slightly different in your theme.
The theme “Next“ I use, has a “third_party” folder,
some other themes has “partials” folder, which you can use as well.

  • Create a “ad” sub folder in “third_party” or “partials”
  • Create a “adsense.swig” file inside the ad folder
  • Copy the code you got to this file

Now you need to place the ad somewhere in your page, by adding

1
{% include '_third-party/ad/adsense.swig' %}

or

1
{% include '_partials/ad/adsense.swig' %}

the code at location where you want to display it. E.g.:

  • _layout.swig - If you would like to have it globally (on the landing page)
  • page.swig - On a specific page like about, category etc.
  • post.swig - To the blog post