Hugo
Add OnyxMetric analytics to your Hugo site
Hugo builds pages from templates in layouts/, so the neatest approach is a partial that your base template includes on every page.
Copy your tracking snippet
In OnyxMetric, open your site's Site Settings and copy the tracking script. It looks like this:
<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>YOUR_SITE_ID is the numeric ID of your site.
Add the snippet to Hugo
-
Put your site ID under
paramsin the Hugo config (hugo.toml, orconfig.tomlon older versions):hugo.toml [params] onyxmetricSiteID = "YOUR_SITE_ID" -
Create
layouts/partials/onyxmetric-analytics.html(and thelayouts/partials/folder if needed):layouts/partials/onyxmetric-analytics.html {{ if and (not hugo.IsServer) .Site.Params.onyxmetricSiteID }} <script src="https://app.onyxmetric.com/api/script.js?siteId={{ .Site.Params.onyxmetricSiteID }}" async></script> {{ end }}not hugo.IsServerleaves the script out ofhugo server, and the ID check leaves it out of builds without an ID. -
Include the partial in
<head>of your base template, usuallylayouts/_default/baseof.html. If a theme owns that file, copy it into your project'slayouts/_default/first, so theme updates cannot erase your change:layouts/_default/baseof.html <head> ... {{ partial "onyxmetric-analytics.html" . }} </head> -
Run
hugoand deploypublic/.
Check that it works
Load your live site and click through a few pages. Pageviews show up in the OnyxMetric dashboard within seconds.
If nothing arrives:
- View the page source and search for
script.js?siteId=to confirm the tag is present. - In the browser's Network tab, check that
script.jsreturns200and that requests go to/api/track. - Turn off ad blockers, or proxy the script through your own domain.
- Read script troubleshooting for other causes.
Troubleshooting
- Tag missing locally:
hugo servernever emits it because of thehugo.IsServerguard. Look at a productionhugobuild. - Theme overrides: a
layouts/_default/baseof.htmlin your project beats the theme's copy, so make sure you edited the one Hugo renders.
Next steps
- Track custom events such as signups, purchases, and clicks.
- Identify users to tie sessions to accounts.
- Proxy the script through your own domain to get past ad blockers.
- Script attributes let you skip or mask URLs and tag events.