How to Add Analytics to a Hugo Site (No Cookies, No Consent Banner)

Hugo builds fast sites. You shouldn't slow them down with a bloated analytics script.

Here's how to add privacy-friendly analytics to a Hugo site — config-driven, no cookies, no consent banner.

Step 1: Create a partial

Create a new file at layouts/partials/analytics.html:


<script
  src="https://app.fairlytics.dev/js/tracker.v1.js"
  data-site=""
  data-api="https://app.fairlytics.dev"
></script>

This only renders the script when a site ID is configured, so it won't fire in local development unless you want it to.

Step 2: Include the partial in your base layout

Open layouts/_default/baseof.html and add the partial before </body>:

  
</body>
</html>

If your theme uses a different layout structure, add it to whichever template renders the closing </body> tag.

Step 3: Add your site ID to config

In your hugo.toml (or config.toml):

[params]
  fairlyticsId = "YOUR_SITE_ID"

Or in hugo.yaml:

params:
  fairlyticsId: "YOUR_SITE_ID"

Deploy your site, and you're done.

Environment-aware configuration

Want analytics only in production? Use Hugo's environment system:

# config/production/params.toml
fairlyticsId = "YOUR_SITE_ID"

Leave the development config empty, and the partial won't render during hugo server.

Why not Hugo's built-in Google Analytics?

Hugo ships with a built-in google_analytics template. It's convenient, but it loads Google's full ~82 KB tracking script, sets cookies, and requires a consent banner in the EU.

The Fairlytics script is 510 bytes gzipped. No cookies. No consent banner needed. It matches Hugo's philosophy of keeping things lean.

What you get

Without any cookies or personal data:

All while keeping your Hugo site fast and your visitors' privacy intact. Here's how visitor counting works without cookies.

Full baseof.html example

<!DOCTYPE html>
<html lang="">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title> — </title>
  
</head>
<body>
  
  <main>
    
  </main>
  
  
</body>
</html>

Sign up for Fairlytics — free, no credit card. Get your site ID and deploy in under 2 minutes.