From b5eca30279e3b4f69d71d848a5b9400c7ce8e1c9 Mon Sep 17 00:00:00 2001 From: Guido Walter Pettinari Date: Tue, 9 May 2017 18:47:45 +0200 Subject: [PATCH 1/3] Send client's IP to GA The IP is added to the payload data in getTrackingPayloadData(). The function 'getClientIp' is introduced to obtain the client's IP. Note that GA will anonymize the IP, so make sure that your IP filters in GA do not rely on the last three digits of the IP. For details, see issue https://github.com/ins0/google-measurement-php-client/issues/33 --- src/Racecore/GATracking/GATracking.php | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/Racecore/GATracking/GATracking.php b/src/Racecore/GATracking/GATracking.php index ad5f100..c653836 100755 --- a/src/Racecore/GATracking/GATracking.php +++ b/src/Racecore/GATracking/GATracking.php @@ -277,6 +277,52 @@ final private function generateUuid() ); } + /** + * Return the client's IP address. + * + * The algorithm uses the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR + * globals to infer the IP address; if they are not available (as + * it is the case in most cases), it suse the REMOTE_ADDR global. + * + * Do not use this function to grant access or privileges to + * IP addresses, because the HTTP_CLIENT_IP and + * HTTP_X_FORWARDED_FOR globals can be easily spoofed. + * + * On the other hand, the REMOTE_ADDR global is very difficult + * to spoof. However, when the client is beyond a proxy, it isn't + * necessarily the correct IP. + * + * Returns the IP address if found, false if not. The output is + * santized via FILTER_VALIDATE_IP. + * + * See here for more details: http://stackoverflow.com/questions/ + * 3003145/how-to-get-the-client-ip-address-in-php + * + * Created by Guido W. Pettinari on 05.09.2016. + * Latest version here: + * https://gist.github.com/coccoinomane/4c420776dc16d80ea772aff06d3e1ef4 + */ + final private function getClientIp () + { + + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + + $ip = $_SERVER['HTTP_CLIENT_IP']; + + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + + } else { + + $ip = $_SERVER['REMOTE_ADDR']; + + } + + return filter_var($ip, FILTER_VALIDATE_IP); + + } + /** * Build the Tracking Payload Data * @@ -291,6 +337,7 @@ protected function getTrackingPayloadData(Tracking\AbstractTracking $event) $payloadData['tid'] = $this->analyticsAccountUid; // account id $payloadData['uid'] = $this->getOption('user_id'); $payloadData['cid'] = $this->getClientId(); + $payloadData['uip'] = $this->getClientIp(); // client IP, will be anonymized $proxy = $this->getOption('proxy'); if ($proxy) { From fc5454805b453727491b9089edbacacb397f6de0 Mon Sep 17 00:00:00 2001 From: Guido Walter Pettinari Date: Tue, 23 May 2017 15:05:36 +0200 Subject: [PATCH 2/3] Fix bug in getClientIp() --- src/Racecore/GATracking/GATracking.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Racecore/GATracking/GATracking.php b/src/Racecore/GATracking/GATracking.php index c653836..c14f1a6 100755 --- a/src/Racecore/GATracking/GATracking.php +++ b/src/Racecore/GATracking/GATracking.php @@ -280,9 +280,12 @@ final private function generateUuid() /** * Return the client's IP address. * + * Returns the IP address if found, an empty string if not. The + * output is santized via FILTER_VALIDATE_IP. + * * The algorithm uses the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR * globals to infer the IP address; if they are not available (as - * it is the case in most cases), it suse the REMOTE_ADDR global. + * it is the case in most cases), it uses the REMOTE_ADDR global. * * Do not use this function to grant access or privileges to * IP addresses, because the HTTP_CLIENT_IP and @@ -292,9 +295,6 @@ final private function generateUuid() * to spoof. However, when the client is beyond a proxy, it isn't * necessarily the correct IP. * - * Returns the IP address if found, false if not. The output is - * santized via FILTER_VALIDATE_IP. - * * See here for more details: http://stackoverflow.com/questions/ * 3003145/how-to-get-the-client-ip-address-in-php * @@ -305,18 +305,23 @@ final private function generateUuid() final private function getClientIp () { - if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; - } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else { + } elseif (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; + } + else { + + return ''; + } return filter_var($ip, FILTER_VALIDATE_IP); From 1abbd1ab39927064ed292ec250a6c1ca641b1e3f Mon Sep 17 00:00:00 2001 From: Marco Rieger Date: Tue, 23 May 2017 15:59:00 +0200 Subject: [PATCH 3/3] Fix failed composer https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode --- composer.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/composer.json b/composer.json index 4c98077..38b4896 100755 --- a/composer.json +++ b/composer.json @@ -29,6 +29,13 @@ "Racecore\\GATracking": "src/" } }, + "repositories": [ + { + "type": "composer", + "url": "https://packagist.org" + }, + { "packagist": false } + ], "extra": { "branch-alias": { "dev-master": "2.1.x-dev"