OnyxMetric
Integration Guides

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

  1. Put your site ID under params in the Hugo config (hugo.toml, or config.toml on older versions):

    hugo.toml
    [params]
      onyxmetricSiteID = "YOUR_SITE_ID"
  2. Create layouts/partials/onyxmetric-analytics.html (and the layouts/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.IsServer leaves the script out of hugo server, and the ID check leaves it out of builds without an ID.

  3. Include the partial in <head> of your base template, usually layouts/_default/baseof.html. If a theme owns that file, copy it into your project's layouts/_default/ first, so theme updates cannot erase your change:

    layouts/_default/baseof.html
    <head>
      ...
      {{ partial "onyxmetric-analytics.html" . }}
    </head>
  4. Run hugo and deploy public/.

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.js returns 200 and 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 server never emits it because of the hugo.IsServer guard. Look at a production hugo build.
  • Theme overrides: a layouts/_default/baseof.html in your project beats the theme's copy, so make sure you edited the one Hugo renders.

Next steps

On this page