MATOMO tracking system with GRAV

Hello,

I am wanted to generate statistic about visiting users to my site developed with GRAV.

Searching this forum, I found the suggestion about using MATOMO, an open source alternative to GoogleAnalytics.

I successfully installed MATOMO on my server. In the final steps of its configuration, it ask to add a code to every page of the monitored site. I don’t know how to inject such code to every page of my site. (I am using only the admin interface).

Thinking in circumvent that, I search Google for plugins. I found three plugins to configure and use MATOMO with GRAV. I installed one of them (https://github.com/ricardo118/grav-plugin-matomo) that claim not need to touch any code on the site.

I visit the site from other computers (and friends from other cities) but nothing appear at the MATOMO stats page.

My doubts are:
1- How do I inject the code from MATOMO in all the site pages?
2- Using the plugin will do that task? How?
3- Will take time to MATOMO process the visitors information or this is instantaneously?

Regards,

Camps

I didn’t have any luck with the plugin.

I’ll share what I’m doing. Keep in mind that I’m not using an inherited theme. Instead, I’m modifying a theme (Quark, in my case) directly. If your theme is an inherited theme, you may want to adjust accordingly.

user/blueprints/config/site.yaml:

@extends:
  '@parent'

form:
    fields:
        content:
            fields:
                matomo_tracking_code:
                    type: editor
                    size: large
                    label: 'Matomo Tracking Code'
                    help: 'The tracking code given by Matomo setup goes here'
                    codemirror:
                        mode: 'javascript'
                        indentUnit: 2
                        autofocus: true
                        indentWithTabs: false
                        lineNumbers: true
                        styleActiveLine: true
                        gutters: ['CodeMirror-lint-markers']
                        lint: true

After adding the above blueprint, you can go into the grav admin > Configuration > Site and add the tracking code that Matomo gives you under “Matomo Tracking Code”. This will place an item matomo_tracking_code in users/config/site.yaml.

(Or if you’re not using the admin panel, you can skip the blueprint above, and just add an item to users/config/site.yaml like this:

matomo_tracking_code: "[your tracking code here]"

)

Create file themes/[your theme or inherited theme]/templates/partials/matomo.html.twig:

{% if site.matomo_tracking_code %}
    {% set matomo = site.matomo_tracking_code %}
    {{ matomo|raw }}
{% endif %}

In user/themes/templates/partials/base.html.twig add the following just above the </head> tag:

{% include 'partials/matomo.html.twig' %}

(Note: if you’re using an inherited theme, you may not want to create a modification of base.html.twig. In that case, you can place the above include in default.html.twig or modular.html.twig, and so on, of your inherited theme.)

The strategy I used above is slightly more complicated than the minimum that would be necessary. The very minimum would simply be to ignore all of the files except base.html.twig and add the HTML tracking code that Matomo gives you directly to it (in place of the include that I described above).

Hope that helps. And in answer to your question 3, in my experience, Matomo collects information instantly. On your Matomo dashboard, if you click “Visits in Real-time”, you should see any visitors displayed right away.

1 Like

Thank you very much for your advises.

1 Like

I am using Matomo on several websites. No cookie banners necessary under certain conditions in France.
I installed it on my server from the cPanel using Softaculous.
Then, I added the tracking code in my Themes > Theme name > Templates > Partials > base.html.twig, just before the </head> . No plugin needed.
I added the objection iFrame as HTML in the markdown of my legal page
Works fine.

3 Likes

I agree with @red … however, this won’t work if you’re using a downloaded theme that you’re updating regularly from its repository. But if you’re editing your own theme, just add the Matomo-provided JavaScript to the partials/base.html.twig (or wherever your basic HTML page data is stored). Only I would add it to the end of so that it will only be loaded after your actual page contents. In my opinion there’s no need for a complicated blueprint file.

1 Like