IPv6 support for GeoLocation


#1

Hi all,

We’re using “GeoIP (Apache)” to track visitor locations, but we noticed it wasn’t tracking IPv6 traffic. That’s a hefty part of our client base, so we wrote the following patch to correct it. I can’t find how to submit a patch to a plugin - would someone be able to point me in the right direction?

Thanks.

piwik/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php


@@ -95,6 +95,8 @@
         foreach (self::$geoIpServerVars as $resultKey => $geoipVarName) {
             if (!empty($_SERVER[$geoipVarName])) {
                 $result[$resultKey] = $_SERVER[$geoipVarName];
+            }elseif (!empty($_SERVER[$geoipVarName.'_V6'])) {
+                $result[$resultKey] = $_SERVER[$geoipVarName.'_V6'];
             }
         }
         foreach (self::$geoIpUtfServerVars as $resultKey => $geoipVarName) {
@@ -150,7 +152,8 @@
         }
 
         $available = !empty($_SERVER[self::TEST_SERVER_VAR])
-            || !empty($_SERVER[self::TEST_SERVER_VAR_ALT]);
+            || !empty($_SERVER[self::TEST_SERVER_VAR_ALT])
+            || !empty($_SERVER[self::TEST_SERVER_VAR_ALT.'+_V6']);
 
         if ($available) {
             return true;
@@ -180,6 +183,7 @@
     {
         if (empty($_SERVER[self::TEST_SERVER_VAR])
             && empty($_SERVER[self::TEST_SERVER_VAR_ALT])
+            && empty($_SERVER[self::TEST_SERVER_VAR_ALT.'_V6'])
         ) {
             return Piwik::translate("UserCountry_CannotFindGeoIPServerVar", self::TEST_SERVER_VAR . ' $_SERVER');
         }

IPv4 tracking depends on:

  • GEOIP_ADDR
  • GEOIP_CONTINENT_CODE
  • GEOIP_COUNTRY_CODE
  • GEOIP_COUNTRY_NAME

And IPv6 depends on:
GEOIP_ADDR
GEOIP_CONTINENT_CODE_V6
GEOIP_COUNTRY_CODE_V6
GEOIP_COUNTRY_NAME_V6

The code above in the latter two snippets, checks for existence of the corresponding V6 server variables.

In the first, if it has not found IPv4 information, it will instead append _V6 to the variable names, and store any of those in the result hash if they exist.


(Matthieu Aubry) #2

Thanks for the feedback! It would be nice to include this in Piwik.

Here is the ticket that discusses this: Geo location should detect country, region and city from IPv6 addresses · Issue #3581 · matomo-org/matomo · GitHub

The best way to contribute to Piwik is to issue a Pull request on github: GitHub - matomo-org/matomo: Liberating Web Analytics. Star us on Github? +1. Matomo is the leading open alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites, apps & the IoT and visualise this data and extract insights. Privacy is built-in. We love Pull Requests!

To learn more about how to make a pull request, see: http://developer.piwik.org/guides/contributing-to-piwik-core


#3

Thanks Matt, couldn’t find the right page at all. I’ll start up Git so!