From 3db6adb703daef70dc3cb5dbd0d759b36333d7b2 Mon Sep 17 00:00:00 2001 From: AAW Date: Wed, 31 Jul 2024 10:59:43 +0200 Subject: [PATCH] fix using user_id before initialization and add tests for future --- src/Analytics.php | 2 +- test/Unit/AnalyticsTest.php | 73 ++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/Analytics.php b/src/Analytics.php index 20daca2..c094bd9 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -132,7 +132,7 @@ 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_data" => !empty($this->user_id) ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too ["user_properties" => $this->user_properties], ["consent" => $this->consent->toArray()], ); diff --git a/test/Unit/AnalyticsTest.php b/test/Unit/AnalyticsTest.php index 5d43be2..bf4062c 100644 --- a/test/Unit/AnalyticsTest.php +++ b/test/Unit/AnalyticsTest.php @@ -11,6 +11,64 @@ final class AnalyticsTest extends TestCase { + public function test_can_configure_only_client_id_and_export() + { + $analytics = Analytics::new( + $this->prefill['measurement_id'], + $this->prefill['api_secret'], + $debug = true + ) + ->setClientId($this->prefill['client_id']) + ->setTimestampMicros($time = time()) + ->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1])) + ->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue'])); + + $asArray = $analytics->toArray(); + $this->assertIsArray($asArray); + + $this->assertArrayHasKey('timestamp_micros', $asArray); + $this->assertArrayHasKey('client_id', $asArray); + $this->assertArrayNotHasKey('user_id', $asArray); + $this->assertArrayHasKey('user_properties', $asArray); + $this->assertArrayHasKey('events', $asArray); + + $timeAsMicro = $time * 1_000_000; + + $this->assertEquals($timeAsMicro, $asArray['timestamp_micros']); + $this->assertEquals($this->prefill['client_id'], $asArray['client_id']); + $this->assertEquals($userProperty->toArray(), $asArray['user_properties']); + $this->assertEquals([$event->toArray()], $asArray['events']); + } + + public function test_can_configure_only_user_id_and_export() + { + $analytics = Analytics::new( + $this->prefill['measurement_id'], + $this->prefill['api_secret'], + $debug = true + ) + ->setUserId($this->prefill['user_id']) + ->setTimestampMicros($time = time()) + ->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1])) + ->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue'])); + + $asArray = $analytics->toArray(); + $this->assertIsArray($asArray); + + $this->assertArrayHasKey('timestamp_micros', $asArray); + $this->assertArrayNotHasKey('client_id', $asArray); + $this->assertArrayHasKey('user_id', $asArray); + $this->assertArrayHasKey('user_properties', $asArray); + $this->assertArrayHasKey('events', $asArray); + + $timeAsMicro = $time * 1_000_000; + + $this->assertEquals($timeAsMicro, $asArray['timestamp_micros']); + $this->assertEquals($this->prefill['user_id'], $asArray['user_id']); + $this->assertEquals($userProperty->toArray(), $asArray['user_properties']); + $this->assertEquals([$event->toArray()], $asArray['events']); + } + public function test_can_configure_and_export() { $analytics = Analytics::new( @@ -42,6 +100,19 @@ public function test_can_configure_and_export() $this->assertEquals([$event->toArray()], $asArray['events']); } + public function test_can_post_only_client_id_to_google() + { + $this->assertNull( + Analytics::new( + $this->prefill['measurement_id'], + $this->prefill['api_secret'], + $debug = true + ) + ->setClientId($this->prefill['user_id']) + ->addEvent(Login::new())->post() + ); + } + public function test_can_post_to_google() { $this->assertNull($this->analytics->addEvent(Login::new())->post()); @@ -67,7 +138,7 @@ public function test_throws_if_microtime_older_than_three_days() public function test_exports_userproperty_to_array() { $this->analytics->addEvent(Login::new()); - + $userProperty = UserProperty::new() ->setName('customer_tier') ->setValue('premium');