Integration Guides
Craft CMS
Add OnyxMetric analytics to your Craft CMS site
Craft renders pages from Twig templates in the templates/ folder. Put the tag in the base template that your section templates extend.
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 Craft CMS
- Open the template that owns your
<head>. In a fresh install that istemplates/index.twig. In a project with a base template, it is the underscore-prefixed layout file. - Add the tag before
</head>:
<!DOCTYPE html>
<html lang="{{ currentSite.language }}">
<head>
<meta charset="utf-8">
<title>{{ siteName }}</title>
<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>- Any section template that extends the layout now loads the script:
{% extends "_layout" %}
{% block content %}
<h1>{{ entry.title }}</h1>
{{ entry.body }}
{% endblock %}To keep the ID out of your templates, read it from .env with {{ getenv('ONYXMETRIC_SITE_ID') }}, or keep it in a Global Set.
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
- Template caching: if the head sits inside a
{% cache %}block, the change appears after the cache expires or afterphp craft clear-caches/all. - Live Preview counts as visits: the control panel's Live Preview renders your template in an iframe with
x-craft-live-previewin the query string. Wrap the tag in a condition that skips requests carrying that parameter. - Control panel is not tracked:
/adminuses Craft's own templates, so the tag never runs there. - Several base templates: if some sections extend a different layout, add the tag to each one, or move it into a partial and include that partial everywhere.
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.