From fe04f4df3358548a67842327fb833e8a6f3dcf17 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 01:55:40 +0200 Subject: [PATCH 01/15] wip: Add sha256_email_address --- src/Helper/UserDataHelper.php | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/Helper/UserDataHelper.php diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php new file mode 100644 index 0000000..4908242 --- /dev/null +++ b/src/Helper/UserDataHelper.php @@ -0,0 +1,43 @@ +sha256_email_address, $email); + $this->sha256_email_address = array_unique($this->sha256_email_address); + return array_search($email, $this->sha256_email_address, true); + } +} From 213596eb8112324f75864005294333e77ed03a25 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 01:56:04 +0200 Subject: [PATCH 02/15] wip: prepare userdata handler in Analytics (Not implemented in body) --- src/Analytics.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Analytics.php b/src/Analytics.php index 9a0082b..6f0cc79 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -6,7 +6,6 @@ use AlexWestergaard\PhpGa4\Helper; use AlexWestergaard\PhpGa4\Facade; use AlexWestergaard\PhpGa4\Exception\Ga4Exception; -use AlexWestergaard\PhpGa4\Helper\ConsentHelper; /** * Analytics wrapper to contain UserProperties and Events to post on Google Analytics @@ -15,7 +14,8 @@ class Analytics extends Helper\IOHelper implements Facade\Type\AnalyticsType { private Guzzle $guzzle; - private ConsentHelper $consent; + private Helper\ConsentHelper $consent; + private Helper\UserDataHelper $userdata; protected null|bool $non_personalized_ads = false; protected null|int $timestamp_micros; @@ -31,7 +31,8 @@ public function __construct( ) { parent::__construct(); $this->guzzle = new Guzzle(); - $this->consent = new ConsentHelper(); + $this->consent = new Helper\ConsentHelper(); + $this->userdata = new Helper\UserDataHelper(); } public function getParams(): array @@ -106,11 +107,16 @@ public function addEvent(Facade\Type\EventType ...$events) return $this; } - public function consent(): ConsentHelper + public function consent(): Helper\ConsentHelper { return $this->consent; } + public function userdata(): Helper\UserDataHelper + { + return $this->userdata; + } + public function post(): void { if (empty($this->measurement_id)) { From fd2eeed303be15bd636ff90579c45b9275c903e9 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 01:56:16 +0200 Subject: [PATCH 03/15] docs: Add reference to UserData documentation in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b55741f..93b6466 100644 --- a/README.md +++ b/README.md @@ -372,3 +372,4 @@ Two important points: - [Measurement Protocol: Events](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events) - [Reserved Event Names](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#reserved_event_names) - [Measurement Protocol: Validation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=gtag) +- [Measurement Protocol: User Data](https://developers.google.com/analytics/devguides/collection/ga4/uid-data) From 094c49886ff004e552be7218de5f037d1248803d Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 02:09:57 +0200 Subject: [PATCH 04/15] fix: Properly reformat gmails --- src/Helper/UserDataHelper.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 4908242..1672653 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -13,7 +13,7 @@ class UserDataHelper public function addEmail(string $email): int { // Sanitize & Validate email - $email = str_replace(" ", "", $email); + $email = str_replace(" ", "", mb_strtolower($email)); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return -1; // Invalid email format // Google Mail Sanitizer @@ -28,16 +28,15 @@ public function addEmail(string $email): int $x[1] = "gmail.com"; // } $x[0] = explode("+", $x[0], 2)[0]; // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html - $x[0] = str_replace($x[0], ".", ""); + $x[0] = str_replace(".", "", $x[0]); $email = implode("@", $x); } // Sha256 encode - $email = hash("sha256", mb_strtolower($email)); + $email = hash("sha256", $email); - // Append email to user - array_push($this->sha256_email_address, $email); - $this->sha256_email_address = array_unique($this->sha256_email_address); - return array_search($email, $this->sha256_email_address, true); + // Skip duplicated and append email + $this->sha256_email_address = array_filter($this->sha256_email_address, fn ($v) => $v != $email); + return array_push($this->sha256_email_address, $email) - 1; } } From 9ba663fef0314014ec92e81a4f8070ebed218c95 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 02:42:59 +0200 Subject: [PATCH 05/15] wip: Add sha256_phone_number --- src/Helper/UserDataHelper.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 1672653..3965a14 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -5,6 +5,7 @@ class UserDataHelper { private array $sha256_email_address = []; + private array $sha256_phone_number = []; /** * @param string $email Valid email format @@ -25,7 +26,7 @@ public function addEmail(string $email): int $x = explode("@", $email, 2); if (substr($x[1], -mb_strlen("googlemail.com")) == "googlemail.com") { // https://support.google.com/mail/thread/125577450/gmail-and-googlemail?hl=en - $x[1] = "gmail.com"; // + $x[1] = "gmail.com"; } $x[0] = explode("+", $x[0], 2)[0]; // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html $x[0] = str_replace(".", "", $x[0]); @@ -39,4 +40,26 @@ public function addEmail(string $email): int $this->sha256_email_address = array_filter($this->sha256_email_address, fn ($v) => $v != $email); return array_push($this->sha256_email_address, $email) - 1; } + + /** + * This converst prefix and number into international approved numbers + * @param int $number fully international number (without dashes or plus) eg. \ + * "+1-123-4567890" for USA or\ + * "+44-1234-5678900" for UK or\ + * "+45-12345678" for DK + * @return int Cursor of array or -1 for error + */ + public function addPhone(int $number): int + { + $sNumber = (string)$number; + if (strlen($sNumber) < 1 || strlen($sNumber) > 15) { + return -1; + } + + $sNumber = hash("sha256", "+{$sNumber}"); + + // Skip duplicated and append phone number + $this->sha256_phone_number = array_filter($this->sha256_phone_number, fn ($v) => $v != $sNumber); + return array_push($this->sha256_phone_number, $sNumber) - 1; + } } From 194df898aef4bd8c0a305cd84803278f1e8d8f3a Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 04:47:03 +0200 Subject: [PATCH 06/15] feat: Introduce Country-ISO list validator --- src/Helper/CountryIsoHelper.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/Helper/CountryIsoHelper.php diff --git a/src/Helper/CountryIsoHelper.php b/src/Helper/CountryIsoHelper.php new file mode 100644 index 0000000..f2e6d0d --- /dev/null +++ b/src/Helper/CountryIsoHelper.php @@ -0,0 +1,31 @@ + Date: Tue, 2 Jul 2024 04:47:34 +0200 Subject: [PATCH 07/15] fix: wrapping up UserData as object --- src/Helper/UserDataHelper.php | 158 +++++++++++++++++++++++++++++----- 1 file changed, 137 insertions(+), 21 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 3965a14..cec48cf 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -4,18 +4,26 @@ class UserDataHelper { - private array $sha256_email_address = []; - private array $sha256_phone_number = []; + private ?string $sha256_email_address = null; + private ?string $sha256_phone_number = null; + + private ?string $sha256_first_name = null; + private ?string $sha256_last_name = null; + private ?string $sha256_street = null; + private ?string $city = null; + private ?string $region = null; + private ?string $postal_code = null; + private ?string $country = null; /** * @param string $email Valid email format - * @return int Cursor of array or -1 for error + * @return int Cursor of array or -1 for invalid */ - public function addEmail(string $email): int + public function setEmail(string $email): bool { // Sanitize & Validate email $email = str_replace(" ", "", mb_strtolower($email)); - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return -1; // Invalid email format + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; // Invalid email format // Google Mail Sanitizer // https://support.google.com/mail/answer/7436150 @@ -28,38 +36,146 @@ public function addEmail(string $email): int // https://support.google.com/mail/thread/125577450/gmail-and-googlemail?hl=en $x[1] = "gmail.com"; } - $x[0] = explode("+", $x[0], 2)[0]; // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html + // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html + $x[0] = explode("+", $x[0], 2)[0]; $x[0] = str_replace(".", "", $x[0]); $email = implode("@", $x); } // Sha256 encode - $email = hash("sha256", $email); - - // Skip duplicated and append email - $this->sha256_email_address = array_filter($this->sha256_email_address, fn ($v) => $v != $email); - return array_push($this->sha256_email_address, $email) - 1; + $this->sha256_email_address = hash("sha256", $email); + return true; } /** - * This converst prefix and number into international approved numbers * @param int $number fully international number (without dashes or plus) eg. \ * "+1-123-4567890" for USA or\ * "+44-1234-5678900" for UK or\ * "+45-12345678" for DK - * @return int Cursor of array or -1 for error + * @return boolean */ - public function addPhone(int $number): int + public function setPhone(int $number): bool + { + $sNumber = strval($number); + if (strlen($sNumber) < 3 || strlen($sNumber) > 15) { + return false; + } + + $this->sha256_phone_number = hash("sha256", "+{$sNumber}"); + return true; + } + + public function setFirstName(string $firstName): bool + { + if (empty($firstName)) return false; + $this->sha256_first_name = hash("sha256", $this->strip($firstName, true)); + return true; + } + + public function setLastName(string $lastName): bool + { + if (empty($lastName)) return false; + $this->sha256_last_name = hash("sha256", $this->strip($lastName, true)); + return true; + } + + public function setStreet(string $street): bool + { + if (empty($street)) return false; + $this->sha256_street = hash("sha256", $this->strip($street)); + return true; + } + + public function setCity(string $city): bool + { + if (empty($city)) return false; + $this->city = $this->strip($city, true); + return true; + } + + public function setRegion(string $region): bool + { + if (empty($region)) return false; + $this->region = $this->strip($region, true); + return true; + } + + public function setPostalCode(string $postalCode): bool + { + if (empty($postalCode)) return false; + $this->postal_code = $this->strip($postalCode); + return true; + } + + public function setCountry(string $iso): bool { - $sNumber = (string)$number; - if (strlen($sNumber) < 1 || strlen($sNumber) > 15) { - return -1; + if (!CountryIsoHelper::valid($iso)) { + return false; } - $sNumber = hash("sha256", "+{$sNumber}"); + $this->country = mb_strtoupper(trim($iso)); + return false; + } + + public function toArray(): array + { + $res = []; + + if (!empty($this->sha256_email_address)) { + $res["sha256_email_address"] = $this->sha256_email_address; + } + + if (!empty($this->sha256_phone_number)) { + $res["sha256_phone_number"] = $this->sha256_phone_number; + } + + $addr = []; + + if (!empty($this->sha256_first_name)) { + $addr["sha256_first_name"] = $this->sha256_first_name; + } + + if (!empty($this->sha256_last_name)) { + $addr["sha256_last_name"] = $this->sha256_last_name; + } + + if (!empty($this->sha256_street)) { + $addr["sha256_street"] = $this->sha256_street; + } + + if (!empty($this->city)) { + $addr["city"] = $this->city; + } + + if (!empty($this->region)) { + $addr["region"] = $this->region; + } + + if (!empty($this->postal_code)) { + $addr["postal_code"] = $this->postal_code; + } + + if (!empty($this->country)) { + $addr["country"] = $this->country; + } + + if (!empty($this->sha256_phone_number)) { + $res["sha256_phone_number"] = $this->sha256_phone_number; + } + + if (count($addr) > 0) { + $res["address"] = $addr; + } + + return $res; + } + + private function strip(string $s, bool $removeDigits = false): string + { + $d = $removeDigits ? '0-9' : ''; - // Skip duplicated and append phone number - $this->sha256_phone_number = array_filter($this->sha256_phone_number, fn ($v) => $v != $sNumber); - return array_push($this->sha256_phone_number, $sNumber) - 1; + $s = preg_replace("[^a-zA-Z{$d}\-\_\.\,\s]", "", $s); + $s = mb_strtolower($s); + return trim($s); } } From 316e6430bdaa493c41b1396e3b73bba53df0d21c Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 04:51:18 +0200 Subject: [PATCH 08/15] fix: Update PHPDocs --- src/Helper/UserDataHelper.php | 39 ++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index cec48cf..e8ce201 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -16,16 +16,14 @@ class UserDataHelper private ?string $country = null; /** - * @param string $email Valid email format - * @return int Cursor of array or -1 for invalid + * @param string $email + * @return bool */ public function setEmail(string $email): bool { - // Sanitize & Validate email $email = str_replace(" ", "", mb_strtolower($email)); - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; // Invalid email format + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; - // Google Mail Sanitizer // https://support.google.com/mail/answer/7436150 if ( substr($email, -mb_strlen("@gmail.com")) == "@gmail.com" || @@ -42,13 +40,12 @@ public function setEmail(string $email): bool $email = implode("@", $x); } - // Sha256 encode $this->sha256_email_address = hash("sha256", $email); return true; } /** - * @param int $number fully international number (without dashes or plus) eg. \ + * @param int $number International number (without dashes and plus) eg. \ * "+1-123-4567890" for USA or\ * "+44-1234-5678900" for UK or\ * "+45-12345678" for DK @@ -65,6 +62,10 @@ public function setPhone(int $number): bool return true; } + /** + * @param string $firstName Users first name + * @return bool + */ public function setFirstName(string $firstName): bool { if (empty($firstName)) return false; @@ -72,6 +73,10 @@ public function setFirstName(string $firstName): bool return true; } + /** + * @param string $lastName Users last name + * @return bool + */ public function setLastName(string $lastName): bool { if (empty($lastName)) return false; @@ -79,6 +84,10 @@ public function setLastName(string $lastName): bool return true; } + /** + * @param string $street Users street name + * @return bool + */ public function setStreet(string $street): bool { if (empty($street)) return false; @@ -86,6 +95,10 @@ public function setStreet(string $street): bool return true; } + /** + * @param string $city Users city name + * @return bool + */ public function setCity(string $city): bool { if (empty($city)) return false; @@ -93,6 +106,10 @@ public function setCity(string $city): bool return true; } + /** + * @param string $region Users region name + * @return bool + */ public function setRegion(string $region): bool { if (empty($region)) return false; @@ -100,6 +117,10 @@ public function setRegion(string $region): bool return true; } + /** + * @param string $postalCode Users postal code + * @return bool + */ public function setPostalCode(string $postalCode): bool { if (empty($postalCode)) return false; @@ -107,6 +128,10 @@ public function setPostalCode(string $postalCode): bool return true; } + /** + * @param string $iso Users country (ISO) + * @return bool + */ public function setCountry(string $iso): bool { if (!CountryIsoHelper::valid($iso)) { From d28e7d64100a1c23486784365241ce93869d0a3a Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 04:55:08 +0200 Subject: [PATCH 09/15] fix: Update grammar and links --- src/Helper/UserDataHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index e8ce201..48c82fa 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -30,8 +30,8 @@ public function setEmail(string $email): bool substr($email, -mb_strlen("@googlemail.com")) == "@googlemail.com" ) { $x = explode("@", $email, 2); + // https://support.google.com/mail/thread/125577450/gmail-and-googlemail if (substr($x[1], -mb_strlen("googlemail.com")) == "googlemail.com") { - // https://support.google.com/mail/thread/125577450/gmail-and-googlemail?hl=en $x[1] = "gmail.com"; } // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html @@ -45,7 +45,7 @@ public function setEmail(string $email): bool } /** - * @param int $number International number (without dashes and plus) eg. \ + * @param int $number International number (without prefix "+" and dashes) eg. \ * "+1-123-4567890" for USA or\ * "+44-1234-5678900" for UK or\ * "+45-12345678" for DK From 1372edd1d835b35d050ef4441309998900314724 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 04:56:01 +0200 Subject: [PATCH 10/15] fix: lookup email domain directly after split --- src/Helper/UserDataHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 48c82fa..46f78ca 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -31,7 +31,7 @@ public function setEmail(string $email): bool ) { $x = explode("@", $email, 2); // https://support.google.com/mail/thread/125577450/gmail-and-googlemail - if (substr($x[1], -mb_strlen("googlemail.com")) == "googlemail.com") { + if ($x[1] == "googlemail.com") { $x[1] = "gmail.com"; } // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html From c6301632e0e74615a6b91f4b2950d936af904335 Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 22:07:18 +0200 Subject: [PATCH 11/15] fix: shorten "boolean" to "bool" as type --- src/Helper/UserDataHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 46f78ca..0c86e55 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -49,7 +49,7 @@ public function setEmail(string $email): bool * "+1-123-4567890" for USA or\ * "+44-1234-5678900" for UK or\ * "+45-12345678" for DK - * @return boolean + * @return bool */ public function setPhone(int $number): bool { @@ -139,7 +139,7 @@ public function setCountry(string $iso): bool } $this->country = mb_strtoupper(trim($iso)); - return false; + return true; } public function toArray(): array From 97c7994c32224360102f27fd4dd9cdd697edb5df Mon Sep 17 00:00:00 2001 From: AAW Date: Tue, 2 Jul 2024 22:07:30 +0200 Subject: [PATCH 12/15] test: Add test for all fields in UserData --- test/Unit/UserDataTest.php | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/Unit/UserDataTest.php diff --git a/test/Unit/UserDataTest.php b/test/Unit/UserDataTest.php new file mode 100644 index 0000000..fd88bec --- /dev/null +++ b/test/Unit/UserDataTest.php @@ -0,0 +1,39 @@ +assertTrue($uda->setEmail($setEmail = "test@gmail.com")); + $this->assertTrue($uda->setPhone($setPhone = 4500000000)); + $this->assertTrue($uda->setFirstName($setFirstName = "test")); + $this->assertTrue($uda->setLastName($setLastName = "person")); + $this->assertTrue($uda->setStreet($setStreet = "some street 11")); + $this->assertTrue($uda->setCity($setCity = "somewhere")); + $this->assertTrue($uda->setRegion($setRegion = "inthere")); + $this->assertTrue($uda->setPostalCode($setPostalCode = "1234")); + $this->assertTrue($uda->setCountry($setCountry = "DK")); + + $export = $uda->toArray(); + $this->assertIsArray($export); + $this->assertEquals(hash("sha256", $setEmail), $export["sha256_email_address"], $setEmail); + $this->assertEquals(hash("sha256", '+' . $setPhone), $export["sha256_phone_number"], $setPhone); + + $this->assertArrayHasKey("address", $export); + $this->assertIsArray($export["address"]); + $this->assertEquals(hash("sha256", $setFirstName), $export["address"]["sha256_first_name"], $setFirstName); + $this->assertEquals(hash("sha256", $setLastName), $export["address"]["sha256_last_name"], $setLastName); + $this->assertEquals(hash("sha256", $setStreet), $export["address"]["sha256_street"], $setStreet); + $this->assertEquals($setCity, $export["address"]["city"], $setCity); + $this->assertEquals($setRegion, $export["address"]["region"], $setRegion); + $this->assertEquals($setPostalCode, $export["address"]["postal_code"], $setPostalCode); + $this->assertEquals($setCountry, $export["address"]["country"], $setCountry); + } +} From 16fdae6046483ee4f19273cb1e7e097d8d00b7ec Mon Sep 17 00:00:00 2001 From: AAW Date: Wed, 3 Jul 2024 00:21:52 +0200 Subject: [PATCH 13/15] test: Add test to verify Analytics.php is not blocked --- test/Unit/UserDataTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/Unit/UserDataTest.php b/test/Unit/UserDataTest.php index fd88bec..2055d81 100644 --- a/test/Unit/UserDataTest.php +++ b/test/Unit/UserDataTest.php @@ -36,4 +36,20 @@ public function test_user_data_is_fillable() $this->assertEquals($setPostalCode, $export["address"]["postal_code"], $setPostalCode); $this->assertEquals($setCountry, $export["address"]["country"], $setCountry); } + public function test_user_data_is_sendable() + { + $uad = $this->analytics->userdata(); + $uad->setEmail("test@gmail.com"); + $uad->setPhone(4500000000); + $uad->setFirstName("test"); + $uad->setLastName("person"); + $uad->setStreet("some street 11"); + $uad->setCity("somewhere"); + $uad->setRegion("inthere"); + $uad->setPostalCode("1234"); + $uad->setCountry("DK"); + + $this->analytics->addEvent(Login::new()); + $this->assertNull($this->analytics->post()); + } } From 2e5f201add676912b79e5d088f1146f7b55e135d Mon Sep 17 00:00:00 2001 From: AAW Date: Wed, 3 Jul 2024 00:22:11 +0200 Subject: [PATCH 14/15] fix: add UserData to Analytics.php with Resetter --- src/Analytics.php | 5 +++++ src/Helper/UserDataHelper.php | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Analytics.php b/src/Analytics.php index 6f0cc79..20daca2 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -132,16 +132,21 @@ public function post(): void $body = array_replace_recursive( $this->toArray(), + ["user_data" => $this->user_id != null ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too ["user_properties" => $this->user_properties], ["consent" => $this->consent->toArray()], ); + if (count($body["user_data"]) < 1) unset($body["user_data"]); + if (count($body["user_properties"]) < 1) unset($body["user_properties"]); + $chunkEvents = array_chunk($this->events, 25); if (count($chunkEvents) < 1) { throw Ga4Exception::throwMissingEvents(); } + $this->userdata->reset(); $this->user_properties = []; $this->events = []; diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index 0c86e55..c3d8644 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -15,6 +15,19 @@ class UserDataHelper private ?string $postal_code = null; private ?string $country = null; + public function reset(): void + { + $this->sha256_email_address = null; + $this->sha256_phone_number = null; + $this->sha256_first_name = null; + $this->sha256_last_name = null; + $this->sha256_street = null; + $this->city = null; + $this->region = null; + $this->postal_code = null; + $this->country = null; + } + /** * @param string $email * @return bool From 4dcb1d250c65cfca6b7418a837d67933aad8ad0f Mon Sep 17 00:00:00 2001 From: AAW Date: Wed, 3 Jul 2024 00:27:21 +0200 Subject: [PATCH 15/15] fix: Use named variables on sanitizing gmails --- src/Helper/UserDataHelper.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Helper/UserDataHelper.php b/src/Helper/UserDataHelper.php index c3d8644..d3d656a 100644 --- a/src/Helper/UserDataHelper.php +++ b/src/Helper/UserDataHelper.php @@ -42,15 +42,15 @@ public function setEmail(string $email): bool substr($email, -mb_strlen("@gmail.com")) == "@gmail.com" || substr($email, -mb_strlen("@googlemail.com")) == "@googlemail.com" ) { - $x = explode("@", $email, 2); + [$addr, $host] = explode("@", $email, 2); // https://support.google.com/mail/thread/125577450/gmail-and-googlemail - if ($x[1] == "googlemail.com") { - $x[1] = "gmail.com"; + if ($host == "googlemail.com") { + $host = "gmail.com"; } // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html - $x[0] = explode("+", $x[0], 2)[0]; - $x[0] = str_replace(".", "", $x[0]); - $email = implode("@", $x); + $addr = explode("+", $addr, 2)[0]; + $addr = str_replace(".", "", $addr); + $email = implode("@", [trim($addr), trim($host)]); } $this->sha256_email_address = hash("sha256", $email);