OnyxMetric
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

  1. Open the template that owns your <head>. In a fresh install that is templates/index.twig. In a project with a base template, it is the underscore-prefixed layout file.
  2. Add the tag before </head>:
templates/_layout.twig
<!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>
  1. Any section template that extends the layout now loads the script:
templates/blog/_entry.twig
{% 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.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

  • Template caching: if the head sits inside a {% cache %} block, the change appears after the cache expires or after php 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-preview in the query string. Wrap the tag in a condition that skips requests carrying that parameter.
  • Control panel is not tracked: /admin uses 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

On this page