matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.7k stars 2.62k forks source link

setSiteId = null - for deactivate tracking #20522

Open matomoto opened 1 year ago

matomoto commented 1 year ago

4.14.1

For maintenance and other situation it is necessary to deactivate matomo temporarilly. Other situations are like my case: The matomo tracking script is included via PHP include and before a config file per website is included. In thisconfig file is the matomo siteID setted as a PHP variable and this is placed in the matomo tracking code via PHP <?php echo $siteID; ?>. Few pages, that are similar in all websites (like imprint, about, etc.) are generated like a global CMS and included the CMS websites. In this pages are also the same matomo tracking script and the siteID from the same config is included. There is also only 1 config per website. But for first situation, there is free space between the two PHP include (config, matmo script). This free space is suitable to put a new value in the PHP $siteID variable for the matmo tracking code. But current, there is no way to use siteID 0, because also with this is data send to the matomo installation and gives a "Tracking failure". For second situation it is needed to set a special siteID in the config to deactivate the tracking on the complete website, ... consider, the few similar pages can not deactivate over an another way.

I had tested a siteID null, but is the same problem as siteID 0 . But a siteID null was appropriate to use it for deactivate the tracking and don't send data to the matomo installation.

if (siteID === null) {
  // deactivate tracking
}
// or in opposite
if (siteID !== null) {
  // activate tracking
}

Or make it simpler and deactivate also by siteID 0.

if (siteID) {
  // deactivate tracking
}
// or in opposite
if (!siteID) {
  // activate tracking
}

background:

Boolean(null) // false
Boolean(0) // false
Boolean(!null) // true
Boolean(!0) // true
bx80 commented 1 year ago

Hi @matomoto,

Thanks for the suggestion, I can see how the ability to disable the tracking code via a site Id could be useful for some configurations.

It might be worth consider using -1 as the siteID value to disable tracking, since broken or incomplete JavaScript tracking code could also result in a null siteID value and that should result in a tracking failure so the broken code can be fixed.

I'll assign this feature for prioritization :+1:

sgiehl commented 1 year ago

@matomoto I'm not fully sure if I understand your request. Do you want to disable the whole tracking in a Matomo instance by sending a certain siteId? Or do you simply want to send tracking requests that get ignored?

For disabling the tracking of requets in Matomo at all, you can simply change the configuration within Matomo:

[Tracker]
record_statistics = 0
matomoto commented 1 year ago

@bx80 , a -1 or a null or a 0 is OK. I prefer null. Thats explicit and not a integer. 0 as blooean is false, so that this is not so appropriate, because not explicite enough.

@sgiehl , yes, the situation is not so easy to understand it. It affects the web pages, which are the same for all websites and are generated automatically. For these, no separate intervention is possible.

Example: config.php Every website has such a file. It contents several values. imprint.php In all websites the same file with PHP script content that generates a Imprint page automatically via global content and $_SERVER['SERVER_NAME'] and the values in the config.php. It is not possible, or not needed, or difficult, to change the PHP script inside this files. imprint.php:

<?php
include "/path/to/config.php"; // inside (among other things): $siteID = 1;

include "/path/to/matomo-script.php"; // inside (among other things): _paq.push(['setSiteId', '<?php echo $siteID; ?>']);
?>

It is only possible to set 1 siteID in the config.php, when the imprint.php (and similar pages) should be untoched. With a siteID = null, the tracking in this pages should be disabled.

When a siteID = null is possible, this provide, to set in single pages a new value to the $siteID like this (maybe for tests): page1.php:

<?php
include "/path/to/config.php"; // inside (among other things): $siteID = 1;

$siteID = null;

include "/path/to/matomo-script.php"; // inside (among other things): _paq.push(['setSiteId', '<?php echo $siteID; ?>']);
?>

Or enable the tracking only for one or few pages like this page2.php:

<?php
include "/path/to/config.php"; // inside (among other things): $siteID = null;

$siteID = 1;

include "/path/to/matomo-script.php"; // inside (among other things): _paq.push(['setSiteId', '<?php echo $siteID; ?>']);
?>

That was very usable.

It is prefered a solution without sending a request to the matomo installation, when siteID = null. I have found this code in the piwik.js with a regex idSite=([0-9]+), but i don't understand it really. Its possible only for isOverlaySession, whatever that may be. https://github.com/matomo-org/matomo/blob/5.x-dev/js/piwik.js#L2088 But such a regex or a simple if is needed, to break the request, when siteID = null.