Skip to content

Commit 7b2be4a

Browse files
authored
Fix: Using user_id before initialization with tests (#94)
2 parents 31e37cd + 3db6adb commit 7b2be4a

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/Analytics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function post(): void
132132

133133
$body = array_replace_recursive(
134134
$this->toArray(),
135-
["user_data" => $this->user_id != null ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too
135+
["user_data" => !empty($this->user_id) ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too
136136
["user_properties" => $this->user_properties],
137137
["consent" => $this->consent->toArray()],
138138
);

test/Unit/AnalyticsTest.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,64 @@
1111

1212
final class AnalyticsTest extends TestCase
1313
{
14+
public function test_can_configure_only_client_id_and_export()
15+
{
16+
$analytics = Analytics::new(
17+
$this->prefill['measurement_id'],
18+
$this->prefill['api_secret'],
19+
$debug = true
20+
)
21+
->setClientId($this->prefill['client_id'])
22+
->setTimestampMicros($time = time())
23+
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
24+
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));
25+
26+
$asArray = $analytics->toArray();
27+
$this->assertIsArray($asArray);
28+
29+
$this->assertArrayHasKey('timestamp_micros', $asArray);
30+
$this->assertArrayHasKey('client_id', $asArray);
31+
$this->assertArrayNotHasKey('user_id', $asArray);
32+
$this->assertArrayHasKey('user_properties', $asArray);
33+
$this->assertArrayHasKey('events', $asArray);
34+
35+
$timeAsMicro = $time * 1_000_000;
36+
37+
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
38+
$this->assertEquals($this->prefill['client_id'], $asArray['client_id']);
39+
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
40+
$this->assertEquals([$event->toArray()], $asArray['events']);
41+
}
42+
43+
public function test_can_configure_only_user_id_and_export()
44+
{
45+
$analytics = Analytics::new(
46+
$this->prefill['measurement_id'],
47+
$this->prefill['api_secret'],
48+
$debug = true
49+
)
50+
->setUserId($this->prefill['user_id'])
51+
->setTimestampMicros($time = time())
52+
->addEvent($event = Event\JoinGroup::fromArray(['group_id' => 1]))
53+
->addUserProperty($userProperty = UserProperty::fromArray(['name' => 'test', 'value' => 'testvalue']));
54+
55+
$asArray = $analytics->toArray();
56+
$this->assertIsArray($asArray);
57+
58+
$this->assertArrayHasKey('timestamp_micros', $asArray);
59+
$this->assertArrayNotHasKey('client_id', $asArray);
60+
$this->assertArrayHasKey('user_id', $asArray);
61+
$this->assertArrayHasKey('user_properties', $asArray);
62+
$this->assertArrayHasKey('events', $asArray);
63+
64+
$timeAsMicro = $time * 1_000_000;
65+
66+
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
67+
$this->assertEquals($this->prefill['user_id'], $asArray['user_id']);
68+
$this->assertEquals($userProperty->toArray(), $asArray['user_properties']);
69+
$this->assertEquals([$event->toArray()], $asArray['events']);
70+
}
71+
1472
public function test_can_configure_and_export()
1573
{
1674
$analytics = Analytics::new(
@@ -42,6 +100,19 @@ public function test_can_configure_and_export()
42100
$this->assertEquals([$event->toArray()], $asArray['events']);
43101
}
44102

103+
public function test_can_post_only_client_id_to_google()
104+
{
105+
$this->assertNull(
106+
Analytics::new(
107+
$this->prefill['measurement_id'],
108+
$this->prefill['api_secret'],
109+
$debug = true
110+
)
111+
->setClientId($this->prefill['user_id'])
112+
->addEvent(Login::new())->post()
113+
);
114+
}
115+
45116
public function test_can_post_to_google()
46117
{
47118
$this->assertNull($this->analytics->addEvent(Login::new())->post());
@@ -67,7 +138,7 @@ public function test_throws_if_microtime_older_than_three_days()
67138
public function test_exports_userproperty_to_array()
68139
{
69140
$this->analytics->addEvent(Login::new());
70-
141+
71142
$userProperty = UserProperty::new()
72143
->setName('customer_tier')
73144
->setValue('premium');

0 commit comments

Comments
 (0)