OnyxMetric
Integration Guides

Eleventy

Add OnyxMetric analytics to your Eleventy site

Eleventy renders pages through layouts in _includes/, so the tag goes in your base layout's <head>. Store the site ID in a global data file so the layout stays free of hard-coded values.

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 Eleventy

  1. Create _data/onyxmetric.json (and the _data/ folder if needed). Templates can then read it as onyxmetric:

    _data/onyxmetric.json
    { "siteId": "YOUR_SITE_ID" }
  2. Add the tag to the base layout's <head>. The runMode check keeps the script out of --serve and --watch, and the siteId check keeps it out of builds with no ID:

_includes/base.njk
<head>
  ...
  {% if eleventy.env.runMode == "build" and onyxmetric.siteId %}
    <script src="https://app.onyxmetric.com/api/script.js?siteId={{ onyxmetric.siteId }}" async></script>
  {% endif %}
</head>
_includes/base.liquid
<head>
  ...
  {% if eleventy.env.runMode == "build" and onyxmetric.siteId %}
    <script src="https://app.onyxmetric.com/api/script.js?siteId={{ onyxmetric.siteId }}" async></script>
  {% endif %}
</head>
  1. Build and deploy _site/:

    npx @11ty/eleventy

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: --serve sets runMode to serve, which the guard filters out. Inspect a plain build.
  • Eleventy 1.x: eleventy.env.runMode appeared in 2.0. On older versions keep only the onyxmetric.siteId condition.
  • Custom data folder: if your config sets dir.data, put the JSON file there.
  • Pages without the tag: a page only gets it if its layout chain includes the layout you edited. Pages using layout: none or another base need the tag too.

Next steps

On this page