Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user properties convenience methods #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Amplitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,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->addUserProperties($this->userProperties);
$this->resetUserProperties();
}

Expand Down Expand Up @@ -348,15 +346,21 @@ public function setDeviceId($deviceId)
}

/**
* Set the user properties, will be sent with the next event sent to Amplitude
* Add user properties, will be sent with the next event sent to Amplitude
*
* If no events are logged, it will not get sent to Amplitude
* If user properties are added to the event directly, these will be added on top, so the properties set directly
* on the Event object would take precedence.
*
* If this is called multiple times before an event is sent, later calls will only add to the array, it will not
* overwrite values already set if no events have been sent yet.
*
* Note that if no events are logged after this point, it will not get sent to Amplitude
*
* @param array $userProperties
*/
public function addUserProperties(array $userProperties)
{
$this->userProperties = array_merge($this->userProperties, $userProperties);
$this->userProperties = $this->userProperties + $userProperties;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonyo Shouldn't be the other way round? This is ignoring keys that are previously defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrbasso I figured if you are calling a method named addUserProperties you would expect it to add any new properties and may be surprised if existing properties get overwritten. Otherwise mergeUserProperties() might be a better name...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonyo Hmmmm, silently ignoring data could be a problem too, even on purpose. Maybe the merge name fits better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I originally used addUserProperties() name to begin with: in Amplitude, you are always adding to or changing existing properties, you are never clearing old properties.

The above changes were because I figured, well, what I said above, the name implies it would not overwrite, just add new properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrbasso When in doubt, I guess look at how the official version works...

Looking at the JS SDK, they use setUserProperties(). I could change to use that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonyo Sounds good to me. Using the same name of other SDKs is also a good thing. 👍

return $this;
}

Expand Down
16 changes: 16 additions & 0 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ public function __construct(array $data = [])
}
}

/**
* Add user properties
*
* If called multiple times, the first time a user property is set will take precedence.
*
* If need to overwrite a property already set, you can manipulate $event->userProperties array directly
*
* @param array $userProperties
* @return \Zumba\Amplitude\Event
*/
public function addUserProperties(array $userProperties)
{
$props = !empty($this->userProperties) ? $this->userProperties : [];
$this->userProperties = $props + $userProperties;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonyo Same here.

return $this;
}

/**
* Set a value in the event.
Expand Down
21 changes: 21 additions & 0 deletions test/AmplitudeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,25 @@ public function testOptOut()
->queueEvent('Another Queued Event');
$this->assertTrue($amplitude->getOptOut());
}

public function testAddUserProperties()
{
$userProps = ['dob' => 'tomorrow', 'gender' => 'f'];
$amplitude = new Amplitude();
$amplitude->addUserProperties($userProps);
$this->assertSame($userProps, $amplitude->getUserProperties());

$userProps2 = ['dob' => 'yesterday', 'name' => 'Baby'];
$expected = [
'dob' => 'tomorrow',
'gender' => 'f',
'name' => 'Baby',
];
$amplitude->addUserProperties($userProps2);
$this->assertSame(
$expected,
$amplitude->getUserProperties(),
'Second call to addUserProperties should only add new properties, not overwrite existing'
);
}
}
25 changes: 25 additions & 0 deletions test/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,29 @@ public function testUnsetProperty()
unset($event->userId);
$this->assertEmpty($event->userId, 'Should unset built-in properties with magic unset');
}

public function testAddUserProperties()
{
$userProps = ['dob' => 'tomorrow', 'gender' => 'f'];
$event = new Event();
$event->addUserProperties($userProps);
$this->assertSame(
['user_properties' => $userProps],
$event->toArray(),
'Should set user properties in user_properties'
);

$userProps2 = ['dob' => 'yesterday', 'name' => 'Baby'];
$expected = [
'dob' => 'tomorrow',
'gender' => 'f',
'name' => 'Baby',
];
$event->addUserProperties($userProps2);
$this->assertSame(
['user_properties' => $expected],
$event->toArray(),
'Second call to addUserProperties should only add new properties, not overwrite existing'
);
}
}