Skip to content

Commit

Permalink
allow $device:-prefixed anon_ids to identify (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
csiden authored Apr 11, 2023
1 parent 2e261e9 commit 4f1b621
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/Producers/MixpanelEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ public function getProperty($property) {


/**
* Identify the user you want to associate to tracked events. The $anon_id must be UUID v4 format and not already merged to an $identified_id.
* All identify calls with a new and valid $anon_id will trigger a track $identify event, and merge to the $identified_id.
* Identify the user you want to associate to tracked events. The $anon_id must be UUID v4 format (optionally with the
* prefix '$device:') and not already merged to an $identified_id. All identify calls with a new and valid $anon_id will
* trigger a track $identify event, and merge to the $identified_id.
* @param string|int $user_id
* @param string|int $anon_id [optional]
*/
public function identify($user_id, $anon_id = null) {
$this->register("distinct_id", $user_id);

$UUIDv4 = '/^[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*$/i';
$UUIDv4 = '/^(\$device:)?[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*$/i';
if (!empty($anon_id)) {
if (preg_match($UUIDv4, $anon_id) !== 1) {
/* not a valid uuid */
Expand Down
13 changes: 13 additions & 0 deletions test/Producers/MixpanelEventsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public function testIdentifyValidAnonIdLong() {
$this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']);
}

public function testIdentifyValidAnonIdDevice() {
$user_id = 1;
$anon_id = '$device:13bbf7943e584-0885c2531-5c793977-3e8000-13bbf7943e64cf';

$test = $this->_instance->identify($user_id, $anon_id);
$queue = $this->_instance->getQueue();

$this->assertEquals(1, count($queue));
$this->assertEquals('$identify', $queue[0]['event']);
$this->assertEquals($user_id, $queue[0]['properties']['$identified_id']);
$this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']);
}

public function testIdentifyNoAnonId() {
$user_id = 1;

Expand Down

0 comments on commit 4f1b621

Please sign in to comment.