Before we use Matomo for our clients, we want to test multiple Trackings in Matomo Tagmanager. All the tests would be made with a known IP adress. As I already know the feature for excluding IPs, I wanted to ask if there also is a feature to only include traffic from certain IPs.
i can’t find an option in matomo settings for this, but you could use a server-side rewrite rule (in .htaccess) like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^matomo.php$ - [F]
RewriteRule ^piwik.php$ - [F]
</IfModule>
or if you want a client-side solution you could modify the tracking code to check if the current IP is your desired IP before calling the tracking functions, like this:
(function() {
var desiredIP = '123.45.67.89'; // Replace this with the desired IP address
var userIP;
// Get the user's IP address (or use any other way)
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => {
userIP = data.ip;
// If the IP address matches, enable Matomo tracking
if (userIP === desiredIP) {
var _paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u = "//your-matomo-url/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']); // Your Matomo site ID
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript'; g.async = true; g.src = u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
}
});
})();
(both codes examples by ChatGPT and without testing)