This script allows to track websites with Matomo without revealing the Matomo server URL.
This is useful for users who track multiple websites on the same Matomo server, but don't want to show the Matomo server URL in the source code of all tracked websites.
To run this properly you will need:
http://trackedsite.com
allow_url_fopen=On
In your Matomo server:
You need to install the proxy on the server where your websites are hosted. You can do it both ways:
:zap: Important note about where to install the proxy |
---|
To ensure the highest data accuracy possible, and that your Matomo cookies are set correctly, please install the proxy in your main website domain name and web server. This proxy should be ideally installed on your webserver directly under trackedsite.com . If you installed the proxy in a sub-domain under analytics.trackedsite.com then this would cause data to be less accurate. (Why? because if the sub-domain analytics.trackedsite.com was to resolve to a CNAME that does not match trackedsite.com OR if it was to resolve to A/AAAA addresses that do not match the first half of the A/AAAA addresses running trackedsite.com , then the cookies set by the Matomo Tracker Proxy in the response would only have a lifetime of maximum 7 days on Safari >= 16.4.) |
matomo.php
matomo.php
, download piwik.php
, proxy.php
, matomo-proxy.php
& if you are using the Heatmaps and Session recordings plugin also download plugins/HeatmapSessionRecording/configs.php
to your website root directory, for example at http://trackedsite.com/matomo.php
, http://trackedsite.com/piwik.php
, http://trackedsite.com/proxy.php
, http://trackedsite.com/matomo-proxy.php
& http://trackedsite.com/plugins/HeatmapSessionRecording/configs.php
$MATOMO_URL
should contain the URL to your Matomo server$PROXY_URL
should contain the URL to the tracker-proxy server$TOKEN_AUTH
should contain the token_auth
git clone https://github.com/matomo-org/tracker-proxy.git matomo
into your website root directory (for example at http://trackedsite.com/matomo/matomo.php
)cp config.php.example config.php
config.php
:
$MATOMO_URL
should contain the URL to your Matomo server$PROXY_URL
should contain the URL to the tracker-proxy server$TOKEN_AUTH
should contain the token_auth
By using git you will later be able to update by simply running git pull
.
Be aware that with this method, matomo.php
and other files are in a matomo/
subdirectory. Keep that in mind when applying the instructions for the next step.
The proxy file (http://trackedsite.com/matomo.php
) will be called by the Matomo Javascript tracker instead of calling directly the (secret) Matomo server (http://your-matomo-domain.example.org/matomo/
).
To achieve this, change the Matomo Javascript Code that is in the footer of your pages:
go to Matomo > Settings > Websites > Show Javascript Tracking Code.
copy the Javascript snippet and change the last lines to the following:
[...]
(function() {
var u="//trackedsite.com/";
_paq.push(["setTrackerUrl", u+"matomo.php"]);
_paq.push(["setSiteId", "tracked-site-id-here"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
g.type="text/javascript"; g.async=true; g.defer=true; g.src=u+"matomo.php"; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
What has changed in this code snippet compared to the normal Matomo code?
matomo.js
becomes matomo.php
(or matomo/matomo.php
if you used the git method): matomo.php is the proxy scripttracked-site-id-here
with your idsite<noscript>
is present in your tracking code, you can remove it (it contains the secret Matomo URL which you want to hide)paste the modified Matomo Javascript code in the pages you wish to track.
This modified Javascript code will then track visits/pages/conversions by calling trackedsite.com/matomo.php
, which will then automatically call your (hidden) Matomo Server URL.
At this stage, example.com should be tracked by your Matomo without showing the Matomo server URL. Repeat the step 3. for each website you wish to track in Matomo.
The tracker-proxy also supports proxying the matomo opt out form. To use this, change the URL used in the opt out iframe to use
the tracker proxy's matomo-proxy.php
file.
Note: you can get the opt out iframe from inside the Administration > Privacy > Users opt-out page in your Matomo instance.
By default, the matomo.php
proxy will wait 5 seconds for the Matomo server to return the response.
You may change this timeout by editing the $timeout
value in config.php
.
By default, the matomo.php
proxy will contact your Matomo server with the User-Agent of the client requesting matomo.php
.
You may force the proxy script to use a particular User-Agent by editing the $user_agent
value in config.php
.
If you have found a bug, you are welcome to submit a pull request.
Before running the tests, create a config.php file w/ the following contents in the root repository directory:
<?php
$MATOMO_URL = 'http://localhost/tests/server/';
$TOKEN_AUTH = 'xyz';
$timeout = 5;
The tests need a webserver to be pointed to the root of this repository. The simplest way is to just use Vagrant:
$ vagrant up
$ vagrant ssh
$ cd /vagrant/tests
$ composer install
allow_url_fopen = Off
in your webserver php.iniallow_url_fopen = Off
vendor/bin/phpunit
allow_url_fopen = On
in your webserver php.iniallow_url_fopen = On
vendor/bin/phpunit
Be advised that the tests require at least PHP 5.4 to run, but the proxy itself can run with PHP 5.3.