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
-
Create
_data/onyxmetric.json(and the_data/folder if needed). Templates can then read it asonyxmetric:_data/onyxmetric.json { "siteId": "YOUR_SITE_ID" } -
Add the tag to the base layout's
<head>. TherunModecheck keeps the script out of--serveand--watch, and thesiteIdcheck keeps it out of builds with no ID:
<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><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>-
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.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:
--servesetsrunModetoserve, which the guard filters out. Inspect a plain build. - Eleventy 1.x:
eleventy.env.runModeappeared in 2.0. On older versions keep only theonyxmetric.siteIdcondition. - 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: noneor another base need the tag too.
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.