New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Built-In Country Pre-Filter #16352
Comments
Hi, I think this might be easily implemented as a plugin that during tracking checks if the detected country match your list and if so, simply drop the request (similar to DNT). In theory listening to this event matomo/core/Tracker/VisitExcluded.php Line 97 in 17ec900
should be enough, but I am not sure if at this point Geolocation has already run. If so, it should be pretty easy to implement. |
Hi, thanks, I do wish to go that route at first, this is just a suggestion, as I think this actually can be a very useful feature. add: I see your edited answer. I will try to test it later today. Thanks! |
It seems like my simple idea using |
@dev-101 actually that could work using <?php
return [
'observers.global' => \DI\add([[
'Tracker.isExcludedVisit',
function(&$excluded, \Piwik\Tracker\Request $request) {
$provider = \Piwik\Plugins\UserCountry\LocationProvider::getProviderById(\Piwik\Common::getCurrentLocationProviderId());
$location = $provider->getLocation(['ip' => $request->getIp()]);
if (strtolower($location[\Piwik\Plugins\UserCountry\LocationProvider::COUNTRY_CODE_KEY]) !== 'de') {
$excluded = true;
}
}
]])
]; That should exclude all visits not coming from Germany. Note: This might not be the optimal solution, as would do the geo location twice per request. |
Thank you, it looks promising, although putting this into config.ini.php will probably get overwritten during the next update (I assume). Wonder if that code can be placed inside a plugin? I have added the code, but for now I am just simulating it. Thanks! |
Unfortunately, it's not working:
It seems that we must not put PHP code inside ini file, as it confuses Piwik/Matomo. update: I tried putting this code inside return array of global.php (seems "logical"), but can't say it is working. |
The hint by @sgiehl helped me create a plugin that at least in my limited test seems to work fine:
|
Thank you, Findus23! |
minor issue:
But, so far, tracking of included countries seems to be working fine. |
@dev-101 Not sure why exactly this happens, but I now exclude invalid settings. |
According to time stamps, it was at the time I installed and configured plugin, there were like 20-30 lines of above notice in the log. Afterwards nothing, so it works ok. I "fixed" it with @ |
I will close this issue now. Thanks everyone 👍 |
@dev-101 I'll reopen the issue for now just in case anyone wants to develop something like this as a plugin |
Ok :) btw. Findus23's plugin works great so far... it's a done deal imho. It is fully configurable in the General Settings page, at least, it satisfies both points 1. and 2. from the opening post. |
The plugin has been published: https://plugins.matomo.org/ExcludeCountries |
Hi @Findus23 could you please bump plugin compatibility with Matomo 4? Thanks! edit: I see Matomo pulled updated version automatically after update, I received a warning before upgrade about compatibility and that it will be disabled after upgrade. Sorry. |
Hi @dev-101, No problem. I wasn't sure myself if I didn't just forget the plugin. |
@dev-101 Just for your information: There is an official plugin currently in development that supports everything my plugin can do and even more: |
Thanks, this is great! Happy NY :) |
Just a heads-up if someone is using this plugin (version 0.2.0), it caused tracking issues on my website out-of-the-blue -- last 4 days tracking stopped and here's the report: Last 4 days one of my Matomo installation (shared hosting, cPanel, Linux, PHP 7.3.8) stopped tracking visitors. GA works fine. Here's what I found in system reports:
I haven't changed anything in the past few months besides updating Matomo. Contrary to what above reports might indicate, the issue is within this plugin, simply deactivating it resolved tracking and other problems right away. I know there was a work on a new plugin, but I didn't switch to it. Maybe that was the problem... edit: switched to newer plugin, problem fixed. |
Hi, it would be very useful if we could tell Matomo to either:
1. only track visits from specific target countries
2. or completely exclude visitors from specific countries
By exclude or track, I mean log into database or completely ignore / drop them. This would save so much resources, streamline our analytics work and keep database resources low.
Yes, I know we have post-filters, but that's not it. It is not good enough.
Also, yeah, we can use 3rd party scripts, but problem with custom country blockers is that it adds additional layer of complexity on top of Matomo, we have to maintain a script that depends on 3rd party updates etc. and Matomo already has all that built-in. Once it determines a country from a visitor's IP, it can simply decide whether it should store it or drop it.
Is there a way to actually build a simple plugin that can do this for us? It should be a simple hard-coded array of countries, I don't mind. It does not have to work in real-time, say, a cron-based script that can clean-up all entries in database every 24 hours that match certain criteria (e.g. visitor's country field). Similar to what we have in GDPR tools now.
Thanks!
The text was updated successfully, but these errors were encountered: