Skip to content

Analytics and SEO

Connect Google Analytics or another service if you want visit counts, and check the canonical, hreflang, social-card, sitemap and robots output search engines read.

An OINK site reports nothing to anyone until you connect an analytics service: Google Analytics through one Hugo setting, anything else through a one-file hook. The theme loads no analytics, form or advertising script of its own, so the decision, and the line in your privacy statement, are yours.

Set baseURL to your deployed address and give each page a description. OINK generates the search metadata, social cards and structured data for your published HTML.

Connecting Google Analytics

Put your GA4 measurement ID in Hugo’s service configuration:

hugo.yml
services:
  googleAnalytics:
    id: G-YOUR_MEASUREMENT_ID

The snippet loads only in production builds, so the default hugo server reports nothing. hugo builds for production by default, even on a preview host, so build pull-request and staging deployments with another environment, as Keeping preview deployments unindexed shows. Without an ID nothing loads, and a Universal Analytics UA- ID is no longer accepted: it produces a build warning instead of a snippet.

Use only this key; the top-level googleAnalytics key is deprecated. If you do not want analytics, delete the block rather than leaving a placeholder, because a placeholder ID still loads Google’s script.

Google Analytics leaves your network

Once configured, page views and events go to Google, which rules it out for a network-isolated site, and a strict Content Security Policy has to allow it; see Content Security Policy.

Connecting another analytics service

Plausible, Umami, Matomo and similar services need only their script on the page. The theme renders two hook partials for exactly this, so you add a file to your site and leave the theme untouched:

layouts/_partials/hooks/head-end.html , InsertedBefore </head>, ahead of the Google Analytics snippet
Analytics scripts, cookie-consent scripts, meta tags the theme does not write
layouts/_partials/hooks/body-end.html , InsertedAfter all the page’s other scripts
Third-party code that affects interaction rather than the first paint

For Plausible, use the domain registered in your own account:

layouts/_partials/hooks/head-end.html
{{ if hugo.IsProduction }}
<script defer data-domain="your-site.example"
        src="https://plausible.io/js/script.js"></script>
{{ end }}

Keep the hugo.IsProduction guard. Without it, every local preview by every author reports visits to your analytics account.

head-end runs before Google Analytics

A cookie-consent script placed in head-end.html therefore runs first and can stop analytics from starting.

The “was this page helpful?” widget is separate and off by default. It makes no request of its own and records answers through your Google Analytics integration when one exists; see Repository links and page info.

Duo view panes

If you enable Duo view (params.ui.compare), opening it loads both pages in same-origin frames. The Google Analytics integration (services.googleAnalytics.id) skips those panes, so opening the reader does not count two extra page views. To do that, OINK renders the snippet from its own pane-aware template instead of Hugo’s built-in one, so an override at layouts/_partials/google_analytics.html has no effect while Duo view is on.

Scripts in head-end.html still run in every pane. Check window.OinkComparePane before recording a page view:

layouts/_partials/hooks/head-end.html
{{ if hugo.IsProduction }}
<script>
  if (!window.OinkComparePane) {
    var script = document.createElement('script');
    script.defer = true;
    script.dataset.domain = 'your-site.example';
    script.src = 'https://plausible.io/js/script.js';
    document.head.appendChild(script);
  }
</script>
{{ end }}

Server and CDN logs see the pane requests too; they carry the query oink_compare_pane=1, which you can filter on.

Page descriptions

<meta name="description"> takes the first non-empty value of:

  1. The page’s description front matter
  2. The page summary Hugo computes (.Summary)
  3. params.description in the site configuration

So one description per page is the only SEO work an author has to do, and it pays three times: as the snippet under the search-engine result, as the card subtitle on its section’s index, and as the preview in site search.

content/docs/install.md
---
title: Install PostgreSQL
description: Install PostgreSQL 18 from the official packages on Debian, Ubuntu or RHEL and start the first cluster.
---

On a multilingual site, write the description in each language instead of copying the English one, and set the site-wide fallback per language too:

hugo.yml
languages:
  en:
    params:
      description: A Hugo theme for engineering docs
  zh:
    params:
      description: 为工程而设计的 Hugo 文档主题

canonical and hreflang

Each interactive content page has one canonical link. An indexable page whose canonical points to itself also lists its eligible HTML translations in the same space and version. When two or more such translations exist, hreflang="x-default" points to the space’s default language (including /en/... when defaultContentLanguageInSubdir: true), or to the first available translation in Hugo language order when that page has no translation in the default language:

rendered output (this page)
<link rel="canonical" href="https://oink.pgsty.com/docs/admin/analytics/">
<link rel="alternate" hreflang="en-US" href="https://oink.pgsty.com/docs/admin/analytics/">
<link rel="alternate" hreflang="zh-CN" href="https://oink.pgsty.com/zh/docs/admin/analytics/">
<link rel="alternate" hreflang="x-default" href="https://oink.pgsty.com/docs/admin/analytics/">

The hreflang codes come from each language’s locale (en-US and zh-CN on this site), and the links follow Hugo’s translation pairing. A page without a translation gets no language-specific hreflang or og:locale:alternate for the missing language, even though the language switcher still offers that language’s home page.

In a native multi-version build, older versions automatically point their <link rel="canonical"> at the matching page in the space’s default version so older manuals do not compete with the latest release in search results. If the default version lacks an indexable HTML counterpart on the same origin and in the same language, the newest intermediate version with such a page wins. If none qualifies, the page keeps its own canonical URL.

Page 1 of a blog index follows the same version rule. From page 2 on, each page uses its own canonical URL and omits hreflang because translated archives can have different page boundaries.

Canonical links are built from baseURL. A wrong baseURL sends search engines to addresses that do not exist, which is harder to notice than a failed build, so go through the deployment checklist before launch. Language configuration itself is covered in Languages.

Social cards

The theme emits Open Graph and Twitter cards. Their description uses the same plain text as the page metadata and structured data. A blank page description falls through to the text of the summary, then to the site description. On paginated blog indexes, og:url identifies the current page.

rendered output (this page)
<meta property="og:title" content="Analytics and SEO">
<meta property="og:type" content="article">
<meta property="og:url" content="https://oink.pgsty.com/docs/admin/analytics/">
<meta property="og:locale" content="en_US">
<meta property="og:locale:alternate" content="zh_CN">
<meta name="twitter:card" content="summary">

To show an image when the link is shared, set images in the page’s front matter:

content/blog/release-3.0.md
---
title: Version 3.0 released
images: [/images/release-3.0.webp]
---

For a site-wide fallback image, set the same key under params:

hugo.yml
params:
  images: [/images/social-card.webp]

With an image, twitter:card becomes summary_large_image and the output gains og:image and twitter:image. This site sets neither key, which is why the output above has no image tags.

Structured data

Every interactive HTML page emits one <script type="application/ld+json"> @graph block in <head> instead of legacy microdata tags:

  • Home pages emit WebSite.
  • Documentation, Book and Swagger pages emit TechArticle; Blog posts emit BlogPosting with Person authors resolved from the authors taxonomy or author front matter.
  • Author profile pages emit ProfilePage with a Person mainEntity; Landing pages emit WebPage; section and taxonomy indexes emit CollectionPage.
  • Pages with visible breadcrumbs append a BreadcrumbList when at least two linked crumbs exist.

Print, Markdown, llms.txt, RSS, 404.html, empty role sites and redirect stubs omit the structured-data script.

Sitemap

In production, Hugo’s index lists the sitemap for each space, version and language with at least one eligible URL. An archive whose pages all have newer canonical URLs keeps its empty XML file. Production robots.txt advertises the index or, in its absence, the published maps for this host. It omits the directive when there are no eligible maps.

Production sitemaps list indexable, self-canonical pages with a published HTML or Markdown URL. A translation must have its own sitemap entry to appear as an XML alternate. When at least two eligible HTML translations remain, their entries also include x-default. In Hugo 0.171, disabling the sitemap kind in the default language can suppress the other languages’ sitemap files as well. To omit just the default language’s entries, apply sitemap: {disable: true} in a cascade targeting that language.

Preview builds leave each site’s URL list empty. To watch sitemap changes locally, run hugo server --environment production --disableFastRender. With fast render enabled, an HTML page can update while its XML remains stale. If the site uses Google Analytics, set privacy.googleAnalytics.disable: true in your local configuration for this check.

the structure under public/
sitemap.xml        ← the index, pointing at the two below
en/sitemap.xml
zh/sitemap.xml

The site default and the per-page override are both Hugo’s own keys:

hugo.yml
sitemap:
  changefreq: monthly
  filename: sitemap.xml
  priority: 0.5
content/release/notes.md
---
title: Release notes
sitemap:
  priority: 0.8
---

Search engines treat changefreq and priority as hints and may ignore them. What matters more is checking before launch that drafts, private content and non-canonical copies stay out of the sitemap, and that every language with sitemaps enabled is reachable from the published entries.

robots.txt and staying unindexed

Hugo writes robots.txt only when the site asks for it:

hugo.yml
enableRobotsTXT: true

The theme supplies the content, which depends on the build environment:

a production build
User-agent: *
Allow: /

Sitemap: https://oink.pgsty.com/sitemap.xml
a non-production build
User-agent: *
Disallow: /

The page’s robots meta tag emits index, follow in a production build, and noindex, nofollow in non-production builds, print output, 404.html, empty role sites, the xx language, older-home redirect stubs, and spaces configured with params.ui.roles.<role>.index: false.

To keep a published page, section or retired version out of search engines while still letting crawlers follow its outgoing links, set noindex: true in the page’s front matter, a section cascade, or params.ui.noindex:

content/internal/_index.md
---
title: Internal notes
cascade:
  noindex: true
---

In production HTML, noindex: true emits <meta name="robots" content="noindex, follow">, keeps a self-referential canonical URL, and omits the page from sitemap.xml and every hreflang cluster (reader language switchers still link to it). A child page beneath a noindex: true cascade can opt back in with noindex: false. To avoid publishing a page altogether, use draft: true or Hugo’s build options. Invalid values warn once per value. Search site configuration and content front matter for noindex, including section cascades.

Checking indexing

A week or two after launch, confirm that search engines see the site you think you published:

  1. Crawl permission: <baseURL>/robots.txt reads Allow: /, not Disallow: /.
  2. Page inventory: open each sitemap advertised in robots.txt, follow any index entries and check the page counts.
  3. Indexed pages: a site:yourdomain search returns about the right number. The order of magnitude is enough.
  4. Canonical addresses: results open the canonical URL, not a version with ? parameters or an old domain.
  5. Submission: add the site to Google Search Console and Bing Webmaster Tools and submit the published sitemap address, which gets it crawled sooner than waiting.

Verify

Run this from the site root, with PAGE set to a page your site generates (including any language prefix):

Terminal
hugo --gc --minify --printPathWarnings
PAGE=public/docs/getting-started/index.html
test -f "$PAGE"

# Canonical should use the real production address; production robots allow indexing.
grep -o '<link[^>]*canonical[^>]*>' "$PAGE"
grep -o '<meta[^>]*robots[^>]*>' "$PAGE"
cat public/robots.txt
head -5 public/sitemap.xml

# No match is expected when Google Analytics is not configured.
grep -o '<script[^>]*googletagmanager[^>]*>' "$PAGE"

In the browser’s Network panel, confirm that analytics requests carry your measurement ID or registered domain. A deployment built with --environment staging must make no analytics request, and a site with no analytics configured makes none in either environment, though other integrations you enabled still contact their own services.

  • Deploy — baseURL, the checklist and unindexed preview deployments
  • Repository links and page info — the feedback widget, edit links and last-modified dates
  • Languages — language configuration decides hreflang and translation pairing
  • AI-agent support — the .md output and llms.txt written for models
  • Configuration — services, sitemap, enableRobotsTXT and the rest