diff --git a/src/Amplitude.php b/src/Amplitude.php index 3aafefa..5d2f58a 100644 --- a/src/Amplitude.php +++ b/src/Amplitude.php @@ -257,9 +257,7 @@ public function logEvent($eventType = '', array $eventProperties = []) $event->deviceId = $this->deviceId; } if (!empty($this->userProperties)) { - $props = !empty($event->userProperties) ? $event->userProperties : []; - $props = array_merge($props, $this->userProperties); - $event->userProperties = $props; + $event->setUserProperties($this->userProperties); $this->resetUserProperties(); } @@ -355,6 +353,8 @@ public function setDeviceId($deviceId) /** * Set the user properties, will be sent with the next event sent to Amplitude * + * Any set with this will take precedence over any set on the Event object + * * If no events are logged, it will not get sent to Amplitude * * @param array $userProperties diff --git a/src/Event.php b/src/Event.php index 4c53f6c..dbc0f0c 100644 --- a/src/Event.php +++ b/src/Event.php @@ -100,6 +100,17 @@ public function __construct(array $data = []) } } + /** + * Set the user properties on the event + * + * @param array $userProperties + */ + public function setUserProperties(array $userProperties) + { + $props = $this->userProperties ?: []; + $this->userProperties = array_merge($props, $userProperties); + return $this; + } /** * Set a value in the event. diff --git a/test/AmplitudeTest.php b/test/AmplitudeTest.php index 34acf40..cc7710e 100644 --- a/test/AmplitudeTest.php +++ b/test/AmplitudeTest.php @@ -322,4 +322,24 @@ public function testOptOut() ->queueEvent('Another Queued Event'); $this->assertTrue($amplitude->getOptOut()); } + + public function testSetUserProperties() + { + $userProps = ['dob' => 'tomorrow', 'gender' => 'f']; + $amplitude = new Amplitude(); + $amplitude->setUserProperties($userProps); + $this->assertSame($userProps, $amplitude->getUserProperties()); + $userProps2 = ['dob' => 'yesterday', 'name' => 'Baby']; + $expected = [ + 'dob' => 'yesterday', + 'gender' => 'f', + 'name' => 'Baby', + ]; + $amplitude->setUserProperties($userProps2); + $this->assertSame( + $expected, + $amplitude->getUserProperties(), + 'Second call to setUserProperties should set properties, without removing existing' + ); + } } diff --git a/test/EventTest.php b/test/EventTest.php index 1967100..1315658 100644 --- a/test/EventTest.php +++ b/test/EventTest.php @@ -201,4 +201,28 @@ public function testUnsetProperty() unset($event->userId); $this->assertEmpty($event->userId, 'Should unset built-in properties with magic unset'); } + + public function testSetUserProperties() + { + $userProps = ['dob' => 'tomorrow', 'gender' => 'f']; + $event = new Event(); + $event->setUserProperties($userProps); + $this->assertSame( + ['user_properties' => $userProps], + $event->toArray(), + 'Should set user properties in user_properties' + ); + $userProps2 = ['dob' => 'yesterday', 'name' => 'Baby']; + $expected = [ + 'dob' => 'yesterday', + 'gender' => 'f', + 'name' => 'Baby', + ]; + $event->setUserProperties($userProps2); + $this->assertSame( + ['user_properties' => $expected], + $event->toArray(), + 'Second call to setUserProperties should update properties, not remove existing' + ); + } }