Matomo with Cookie Compliance

Cookbook · guide Last verified View raw markdown (for agents)

In short: keep the Cookie Compliance snippet as the first script in <head>, then add Matomo below it. A normal <script src=".../matomo.js"> is recognised on its own; Matomo's standard inline snippet builds the file name in pieces, so mark that block yourself.

You need #

Where it goes #

Matomo's docs place the code just after the opening <body> tag or within <head>. Either spot is fine as long as it stays below the banner snippet.

Normal external file (recognised on its own):

<!-- Cookie Compliance snippet first, then: -->
<script src="https://example.com/matomo.js" async defer></script>

Standard inline snippet (builds the file name in pieces — mark it):

<script class="hu-block" data-hu-category="3">
  var _paq = window._paq = window._paq || [];
  /* … Matomo's inline tracker that assembles matomo.js … */
</script>

What the order controls #

A script file named matomo.js is recognised under category 3 Site Optimisation by default. The standard inline snippet builds the file name in pieces, so mark it class="hu-block" data-hu-category="3" (see trackers). In regions whose rule requires consent first, Matomo waits until the visitor allows that category; it starts after Allow. A script placed above the banner snippet runs straight away.

Confirm the result #

View the page source: the banner snippet comes first in <head>, Matomo after it (or just after <body>). Before choosing, in a region whose rule requires consent first, _pk_id and _pk_ses are absent; the held script shows type="javascript/blocked" in the Elements panel. After Allow, those cookies appear. Browser checks: plain-html; full procedure: verify-install.

Gotchas #

More help #