How to add analytics to Hugo

Hugo themes build the <head> from a partial. Add the tracker through your theme's head-extension partial, or override the head partial in your own layouts folder so theme updates don't wipe it.

No cookies, no banner ~3 KB script About 2 minutes

Before you start

Create a free Cool Analytics account and add your site. You'll get a site ID — replace YOUR_SITE_ID in the code below with it. The dashboard shows the snippet with your ID already filled in.

Install on Hugo

  1. 1Check your theme for a hook partial such as extend_head.html or custom_head.html. If it has one, create that file in your site's layouts/partials/ folder.
  2. 2Otherwise copy the theme's head partial (e.g. themes/<theme>/layouts/partials/head.html) to layouts/partials/head.html and edit the copy.
  3. 3Paste the snippet, wrapped in the production check, then run hugo and deploy.
layouts/partials/extend_head.html
{{ if hugo.IsProduction }}
<script defer src="https://coolanalytics.dev/hb.js" data-site="YOUR_SITE_ID"></script>
{{ end }}

Check that it works

Open your live site in a new tab, then open your Cool Analytics dashboard. Your visit appears on the live map within seconds — with the page you're on, where you came from and your browser.

Nothing showing up? Make sure you're looking at the published site (not a preview or editor), that an ad blocker isn't blocking the script, and that you're not testing on localhost.

Track signups, purchases and clicks

Pageviews, sources, countries and outbound link clicks are tracked automatically. For the moments that matter to you, send a custom event. Events named like a signup show up as signups, and events with an amount show up as revenue — on the live map and per traffic source.

custom events
// anywhere in your Hugo site's browser code
window.hb?.track("signup");

// with an amount, it shows up as revenue
window.hb?.track("purchase", { amount: 49 });

Hugo tips

  • hugo.IsProduction is false while you run hugo server, so local previews never reach your stats.
  • Files in your site's layouts folder take precedence over the theme's, which is why overriding is safe across theme updates.

Want to keep your own visits out of the stats? Open your site once with #hb-ignore at the end of the URL. That browser is excluded until you visit with #hb-track.

Hugo analytics FAQ

Which partial does my theme use?+

Search the theme folder for </head>. The file that contains it is the one to extend or override.

Does it work with Hugo modules?+

Yes. Overrides in your site's layouts folder win over module files too.

Do I need to rebuild after adding it?+

Yes — run hugo (or let your CI build) so the snippet ends up in the generated HTML.

See who's on your Hugo site right now.

One line of code. No cookies. 7 days free.