Analytics

Google Analytics, Plausible and consent

Hugolify ships with Google Analytics and Meta Pixel, both wired to the cookie banner. Cookieless services such as Plausible are added with a partial override.

Google Analytics

Setup

/config/_default/services.yaml

googleAnalytics:
  id: "G-XXXXXXXXXX"

This is the native Hugo service configuration. Keep it in /config/production/services.yml to avoid tracking your local and staging builds.

Google Analytics is only loaded when the cookie banner is enabled. Without it, the ID is ignored.

Enable the banner

See Cookie banner for the full banner reference.

/config/_default/params.yaml

cookie_banner:
  enable: true # Required
  cookieconsent:
    enable: true # Use CookieConsent
    analytics: true # Required to load Google Analytics

Behaviours

The banner has two modes, and each one loads Google Analytics differently.

Simple bannerCookieConsent
cookieconsent.enablefalsetrue
Required to load GAcookie_banner.enablecookieconsent.analytics
Consent Mode v2NoYes
Preferences modalNoYes
Consent cookiecookie-consent (31 days)cc_cookie
On acceptgtag with anonymize_ipanalytics_storage: granted
On refuse_ga* cookies deletedTracking disabled, _ga* cookies erased
Meta PixelNoYes

In CookieConsent mode, nothing is sent before a choice is made: consent defaults to denied for every storage type, with ads_data_redaction on and url_passthrough on.

Meta Pixel

/config/_default/params.yaml

services:
  meta: "000000000000000" # Meta Pixel ID

Loaded under the same cookieconsent.analytics: true switch as Google Analytics, and listed as a separate service in the preferences modal.

Plausible

Plausible is cookieless and collects no personal data, so it needs no consent banner. It has no dedicated parameter: add it through the script partial.

/layouts/partials/footer/scripts.html

<!-- Privacy-friendly analytics by Plausible -->
<script defer data-domain="example.com" src="https://plausible.io/js/script.js"></script>

The theme ships this partial empty, as an extension point rendered at the end of body. Any third party script that must not go through the banner belongs here.

Generated domain

data-domain is your site host, so Hugo can fill it from the baseURL. Wrapping it in hugo.IsProduction also keeps your local builds out of the statistics.

/layouts/partials/footer/scripts.html

{{- if hugo.IsProduction }}
<script defer data-domain="{{ (urls.Parse site.BaseURL).Hostname }}" src="https://plausible.io/js/script.js"></script>
{{ end -}}

The generated value has to match the site registered in Plausible, www. included. A baseURL of https://www.example.com/ gives www.example.com, not example.com. Hardcode the domain when the two differ.

Self-hosted

Replace https://plausible.io with your own instance. The data-domain value stays the site registered in Plausible.

Which one

Google AnalyticsPlausible
CookiesYesNo
Consent bannerRequiredNot required
SetupConfigurationPartial override
HostingGoogleSaaS or self-hosted

Documentation

Hugo services Plausible script