Hugo Website: Search Engine Optimisation (SEO)
Getting your website or blog indexed by search engines is achieved by requesting indexing via their webmaster tools, as well as adding code snippets into page headers. Here’s how to implement it on your Hugo site!
Table of Contents
Hugo Variables: Enable Google Analytics
Sign up to Google Analytics and obtain an ID.
Add the googleAnalytics
parameter to ./config.toml. This will prompt Hugo (during builds) to insert the analytics.js
script into the HEAD section of each of your pages.1
googleAnalytics = "UA-{your-Google-Analytics-ID}"
Alternatively, add the same code directly to ./layouts/partials/head.html:1
2
3
4
5
6
7
8
9
10
11<script>
var doNotTrack = false;
if (!doNotTrack) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-{your-Google-Analytics-ID}', 'auto');
ga('send', 'pageview');
}
</script>
… or an alternative version, optimised for modern browsers:1
2
3
4
5
6
7
8<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-{your-Google-Analytics-ID}', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
Hugo Variables: Enable Robots
Add the enableRobotsTXT
parameter to ./config.toml.1
enableRobotsTXT = true
Hugo Variables: Enable Sitemap
Add the [sitemap]
section and parameters to enable generation of the sitemap.xml file in the root directory of the generated output. Instruct search engine webmaster tools (via their web interfaces) to download the file to assist with site indexing.1
2
3[sitemap]
changefreq = "weekly"
priority = 0.5
Google Analytics: Global Site Tag (gtag.js)
Add the gtag.js
script to ./layouts/partials/head.html.1 Ensure you replace the Google Analtics tag (mine is ‘UA-120904581-1’) with your own. :)
Access the site tag administration via: Google Analytics (web console) > Property > Tracking Info > Tracking Code1
2
3
4
5
6
7
8<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-120904581-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-120904581-1');
</script>
- 1: Adding
gtag.js
script may not actually be required if you are already using Google’sanalytics.js
script (via thegoogleAnalytics
parameter in ./config.toml). If anyone knows the answer to this, please let me know!
SEO Schema
Create ./layouts/partials/seo_schema.html and add {{ partial "seo_schema.html" . }}
to ./layouts/partials/head.html.21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "BlogPosting",
"mainEntityOfPage" : {
"@type" : "WebPage",
"@id" : "{{ .Site.BaseURL }}"
},
"articleSection" : "{{ .Section }}",
"name" : "{{ .Title }}",
"headline" : "{{ .Title }}",
"datePublished" : "{{ .Date }}",
"dateModified" : "{{ .Date }}",
"url" : "{{ .Permalink }}",
"inLanguage" : "en-US",
"author" : "{{ range .Site.Author }}{{ . }}{{ end }}",
"creator" : "{{ range .Site.Author }}{{ . }}{{ end }}",
"publisher" : "{{ range .Site.Author }}{{ . }}{{ end }}",
"accountablePerson" : "{{ range .Site.Author }}{{ . }}{{ end }}",
"copyrightHolder" : "{{ range .Site.Author }}{{ . }}{{ end }}",
"copyrightYear" : "{{ .Date.Format "2006" }}",
"wordCount" : "{{ .WordCount }}",
"keywords" : [ {{ with .Keywords }}{{ delimit . ", " }}{{ end }}{{ if .Site.Params.keywords }}, {{ delimit .Site.Params.keywords ", " }}{{ end }} ],
"description" : "{{ if .Description }}{{ .Description }}{{ else if .IsPage }}{{ .Summary }}{{ else }}{{ .Site.Params.description }}{{ end }}",
}
</script>
Search Engines
Get the website added to major search engines search consoles. Most engines offer a variety of methods to achieve this; the most common being to add a code <meta />
snippet into the <HEAD>
section of index.html and other content pages. In Hugo, this is achieved by adding the code snippets to the <HEAD>
section of ./layouts/partials/head.html. See below for links and examples.
NB! Even though I implemented this properly in Hugo, this method failed when I tried it. I suspect that this is because of the way that Bitbucket.io delivers its hosting solution. As a workaround, I used the alternative method involving uploading a file to the root of my website. That worked!
<meta name="google-site-verification" content="1O0_ZR_paMTxYpRrkAClQUn6nB61x0OWJo-JO3aAzt8" />
Yandex (and DuckDuckGo)
<meta name="yandex-verification" content="f66db17cfde52a1f" />
Bing
<meta name="msvalidate.01" content="39C20C116A98D458A32F32A1F8D78F65" />
Social Media
Feedburner (via Twitter)
Activate Feedburner to manage a RSS feed and automatically post new content to Twitter. - https://feedburner.google.com/fb/a/socializeSubmit
References
- 2: Thanks to https://keithpblog.org/post/hugo-website-seo/ for many of the above suggestions.