ElkArte Community

Project Support => Support => Topic started by: augras on December 04, 2021, 05:09:29 am

Title: How to use Matomo
Post by: augras on December 04, 2021, 05:09:29 am
Hi,
I would like to use Matomo but i don't know where insert the code !
To use Matomo a code must be insert and Matomo says "We recommend that you place it immediately before the closing </head> tag.".
But in the index.php there isn't this tag.
Somebody knows how to do with Elkarte ?
Thanck you,
Philippe
Title: Re: How to use Matomo
Post by: badmonkey on December 04, 2021, 08:53:46 pm
Quote from: augras – Hi,
I would like to use Matomo but i don't know where insert the code !
To use Matomo a code must be insert and Matomo says "We recommend that you place it immediately before the closing </head> tag.".
But in the index.php there isn't this tag.
Somebody knows how to do with Elkarte ?
Thanck you,
Philippe


Hey Philippe! There are several options you could consider to accomplish your goal.

The first employs direct hardcode edits. In ./themes/default edit index.template.php. Find the </head> tag around line 221. Add the needed code just before it. If your site offers other themes, you'll need to edit the index.template files for them as well.

Another option would be adding an integration hook and a file containing the needed code. It could even be packaged as an add-on! If you aren't certain how to accomplish it this way, it probably isn't the option you'd want to use.

Yet another option is using an existing add on to inject the needed code. Simple Ads or Simple Portal add ons could both accomplish this. In a nutshell you'd add an html block, then inject the code like this:

Code: [Select]
<head>
.....custom code here.....
</head>

and set the block location nearest the top of the page (header, or something like that).

The best option depends on your coding skill level. Good luck!
Title: Re: How to use Matomo
Post by: augras on December 05, 2021, 04:47:30 am
Thank you @badmonkey ,

I think i have now all the options.

I was looking in /elkarte/index.php but of course it's in the template !

But it doen't work : i get the error : syntax error unexpected 'trackPageView' (T_STRING), expecting ';' or ','
And it's because the script is not correctly interpreted.

I put the code just before </head> but i can see that the color for the syntax is not good (here it's ok but in the file it's all yellow).

The code is :
Code: [Select]
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//augras.eu/matomo/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '5']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

<noscript>
<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://augras.eu/matomo/matomo.php?idsite=5&amp;rec=1" style="border:0" alt="" />
<!-- End Matomo -->
</noscript>

I will try with an addon.

Thank you,
Philippe
Title: Re: How to use Matomo
Post by: augras on December 05, 2021, 08:09:36 am
Quote from: badmonkey – The best option depends on your coding skill level. Good luck!
It's a very small level !

I tried to put the code (the code is ok : it works on other site, there is just to change a number) :
Code: [Select]
<html>
<head>
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//augras.eu/matomo/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '6']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

<noscript>
<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://augras.eu/matomo/matomo.php?idsite=6&amp;rec=1" style="border:0" alt="" />
<!-- End Matomo -->
</noscript>
</head>
</html>
in a addon subst file (UltimateMenu.subs.php, and some others) but get the error
Code: [Select]
syntax error unexpected 'trackPageView' (T_STRING), expecting ';' or ','

But maybe it's not what you mean ?

Maybe it will be a good idea to make an addon to put some scripts easily in head and in body ?
You understand i can't do that :).

Philippe
Title: Re: How to use Matomo
Post by: augras on December 05, 2021, 11:42:03 am
It works !
Maybe it's not the better way...

In ./themes/default/index.template.php, just at the beginning but after
Code: [Select]
<?php
i insert
Code: [Select]
require 'matomo.php';
In ./themes/default/ i created the file matomo.php with the code :
Code: [Select]
<html>
<head>
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//augras.eu/matomo/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '5']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

<noscript>
<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://augras.eu/matomo/matomo.php?idsite=5&amp;rec=1" style="border:0" alt="" />
<!-- End Matomo -->
</noscript>

It is important to don't close <html> and <head>.

And like this it works.

I don't know why it doesn't work as @badmonkey said, because it's nearly the same thing !

Now i need to remember that when there will be an update.



Title: Re: How to use Matomo
Post by: augras on December 21, 2021, 03:55:14 am
Quote from: augras – It works !
Maybe it's not the better way...
And it's not !

Matomo works fine but not Elkarte. you can see the problem on this post :
https://www.elkarte.net/community/index.php?topic=5934.15
There is an xml problem and Quote, Quick reply and maybe some others things doen't work anymore.

So, the question to make matomo working is still open.
Title: Re: How to use Matomo
Post by: augras on January 14, 2022, 02:33:20 am
Hi,
Anybody has an idea to make Matomo works ?
Title: Re: How to use Matomo
Post by: ahrasis on March 19, 2022, 09:26:45 pm
You can try separating the script into js file.
Title: Re: How to use Matomo
Post by: augras on March 20, 2022, 05:36:13 am
Thank's @ahrasis for the idea,
But i don't really know how to do that.
Is it just rename matomo.php to matomo.js and call matomo.js ?
Title: Re: How to use Matomo
Post by: augras on March 21, 2022, 03:31:40 am
I tried like i said : it's around the same thing.
Quick edit is now working but still not Quote, and Modify is now not working.