Embedding

This guide will explain how you can embed the JavaScript Tag Manager into your website.

Finding the embed code

To find the code to embed your container into your website, please log in to your Matomo and click on "Tag Manager". Now you will see a list of containers for the current selected site. Now you have two options:

  • Select "Manage Containers" and click on the "Install Code" icon on the very right next to a container.
  • Select a container and click on "Install code".

Further options are:

  • You can find the code by going to "Administration => Tracking Code". However, you will only see the JavaScript code for environments that have currently a published version. If you just created a container, and have not published (deployed) any version to an environment, you won't find the install code there.
  • You can retrieve this code snippet via the Matomo HTTP API by calling the method TagManager.getContainerEmbedCode($idSite, $idContainer, $environment).

The code looks as follows:

<!-- Matomo Tag Manager -->
<script type="text/javascript">
    var _mtm = _mtm || [];
    _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src='https://{$MATOMO_URL}/js/container_{$CONTAINER}.js'; s.parentNode.insertBefore(g,s);
</script>
<!-- End Matomo Tag Manager -->

In your code, {$MATOMO_URL} will be replaced by your Matomo URL, and {$CONTAINER} by a combination of containerId and possibly other factors.

We recommend to embed this HTML block as early as possible within the <head> element of your website.

Multiple Containers

Matomo Tag Manager lets you embed multiple containers at once. This may be useful when for example different people manage different containers and you need to embed all of them. For example, one team manages all ad related containers, and another team manages all other tags.

To use both tags, simply copy both embed codes inside your website, ideally one directly followed by the other.

Loading a container from your website

When a user is visiting your website, the container will be loaded through your Matomo URL. Sometimes, a Matomo URL may be blocked for example by privacy or ad blockers causing the container to not load. If you want to reduce the likelihood of this, you may want to download the JavaScript file and upload it onto the server of your website. The disadvantage is, that the debug/preview feature won't work out of the box and you always need to update the JavaScript file whenever you publish a new version to an environment.

To download the JavaScript of a container, simply open the URL in g.src='https://{$MATOMO_URL}/js/{$CONTAINER}.js' and save the file by clicking on "File => Save As" in your browser.

Accessing the Matomo Tag Manager directly

A container is usually loaded asynchronously. This means you won't know at which point the tag manager has been initialized. If you want to perform a certain action, as soon as the tag manager JavaScript file has been initialized, simply define the global function winddow.matomoTagManagerAsyncInit. This method will be executed as soon as the global window.MatomoTagManager object is available but before any container has been set up.

window.matomoTagManagerAsyncInit = function () {
    MatomoTagManager.THROW_ERRORS = false;
}