Skip to content

Commit

Permalink
Adding setUserProperties to event object
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyo committed Apr 29, 2016
1 parent 6a2f816 commit 15b4848
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Amplitude.php
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Event.php
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions test/AmplitudeTest.php
Expand Up @@ -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'
);
}
}
24 changes: 24 additions & 0 deletions test/EventTest.php
Expand Up @@ -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'
);
}
}

0 comments on commit 15b4848

Please sign in to comment.