Skip to content

Commit

Permalink
Changing addUserProperties to setUserProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyo committed Apr 29, 2016
1 parent 922c185 commit 6a2f816
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
15 changes: 8 additions & 7 deletions README.md
Expand Up @@ -17,7 +17,7 @@ This is a moderately thin PHP API for [Amplitude](https://amplitude.com/), power
// the user identifier or device identifier, and of course your Amplitude App API key)
$amplitude = \Zumba\Amplitude\Amplitude::getInstance();
$amplitude->init('APIKEY', 'johnny@example.com')
->addUserProperties([
->setUserProperties([
'dob' => '1980-11-04',
'name' => 'Johnny 5'
])
Expand Down Expand Up @@ -93,7 +93,7 @@ if ($canLogEvents) {
There is one main way to set user properties, and this will send the user properties with the next Amplitude event sent to Amplitude:
```php
\Zumba\Amplitude\Amplitude::getInstance()
->addUserProperties(
->setUserProperties(
[
'name' => 'Jane',
'dob' => $dob,
Expand All @@ -105,27 +105,28 @@ You would typically call this right before calling `logQueuedEvents()` to make s

Using this method, it only sends the user information with one event, since once a user property is set in Amplitude it persists for all events that match the user ID or event ID.

Also note that if there happens to be no events sent after `addUserProperties()` are sent, those properties will not get sent to Amplitude.
Also note that if there happens to be no events sent after `setUserProperties()` are sent, those properties will not get sent to Amplitude.

One option, is to use a login event that adds the user info when the user has logged in, and sends it in a login event. That way you only send user properties for the page load that the user logs in.

Alternatively, just add the user properties with every page load when initializing the Amplitude object. This is the option used in the examples.

## Adding User Properties on Event Object
Another option for setting the user properties, is setting them on the Event object itself. You can do this by setting `userProperties`, or by using the `addUserProperties()` method on the `Event` object.
Another option for setting the user properties, is setting them on the Event object itself. You can do this by setting/changing `userProperties`, or by using the `setUserProperties()` method on the `Event` object.

You would typically use this in situations similar to the one in the next section, for times you may be sending events for different users in the same page load.

```php
$event = new \Zumba\Amplitude\Event();
// Method 1 - add user properties method:
$event->addUserProperties(
// Method 1 - set user properties method:
$event->setUserProperties(
[
'name' => 'Rambo',
// ...
]
);
// If you called addUserProperties() a second time, it would add any new properties but not affect ones already set
// If you called setUserProperties() a second time, it would overwrite any properties with the same name but leave
// others intact

// Method 2 - just set the userProperties directly:
$event->userProperties = [
Expand Down
8 changes: 4 additions & 4 deletions src/Amplitude.php
Expand Up @@ -78,7 +78,7 @@ class Amplitude
* <code>
* // In user initialization section of app...
* Zumba\Amplitude\Amplitude::getInstance()->init('APIKEY','johnny@example.com')
* ->addUserProperties(['name' => 'Johnny 5'])
* ->setUserProperties(['name' => 'Johnny 5'])
* ->logQueuedEvents();
*
* // Elsewhere in your app, this could happen before OR after the above initialization...
Expand Down Expand Up @@ -359,14 +359,14 @@ public function setDeviceId($deviceId)
*
* @param array $userProperties
*/
public function addUserProperties(array $userProperties)
public function setUserProperties(array $userProperties)
{
$this->userProperties = array_merge($this->userProperties, $userProperties);
return $this;
}

/**
* Resets user properties added with addUserProperties() if they have not already been sent in an event to Amplitude
* Resets user properties added with setUserProperties() if they have not already been sent in an event to Amplitude
*
* @return \Zumba\Amplitude\Amplitude
*/
Expand All @@ -391,7 +391,7 @@ public function hasQueuedEvents()
*
* This resets the user ID, device ID previously set using setUserId or setDeviceId.
*
* If additional information was previously set using addUserProperties() method, and the event has not already
* If additional information was previously set using setUserProperties() method, and the event has not already
* been sent to Amplitude, it will reset that information as well.
*
* Does not reset user information if set manually on an individual event in the queue.
Expand Down
4 changes: 2 additions & 2 deletions test/AmplitudeTest.php
Expand Up @@ -154,7 +154,7 @@ public function testLogEventUserPropertiesMerged()
$event = $amplitude->event();
$event->userProperties = $props;
$result = $amplitude->init('APIKEY', $userId)
->addUserProperties($props2)
->setUserProperties($props2)
->logEvent($eventType);

$eventData = $event->toArray();
Expand Down Expand Up @@ -291,7 +291,7 @@ public function testResetUser()
$amplitude = new Amplitude();
$amplitude->setUserId('User')
->setDeviceId('device')
->addUserProperties(['user props']);
->setUserProperties(['user props']);
$this->assertNotEmpty($amplitude->getUserId(), 'Initialization check');
$this->assertNotEmpty($amplitude->getDeviceId(), 'Initialization check');
$this->assertNotEmpty($amplitude->getUserProperties(), 'Initialization check');
Expand Down

0 comments on commit 6a2f816

Please sign in to comment.