Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-groups-identify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-php": minor
---

Add an optional `distinctId`/`distinct_id` override for `groupIdentify()` events.
16 changes: 9 additions & 7 deletions bin/posthog
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ switch ($options['type']) {
break;

case 'groupIdentify':
PostHog::groupIdentify(
array(
'groupType' => $options['groupType'],
'groupKey' => $options['groupKey'],
'properties' => parse_json($options['properties'])
)
$message = array(
'groupType' => $options['groupType'],
'groupKey' => $options['groupKey'],
'properties' => parse_json($options['properties'])
);
if (!empty($options['distinctId'])) {
$message['distinctId'] = $options['distinctId'];
}
PostHog::groupIdentify($message);
break;

case 'getFeatureFlag':
Expand Down Expand Up @@ -121,7 +123,7 @@ function usage(): string
"\n posthog --type capture --distinctId \"id\" --event \"event name\" --properties '{ \"json\": \"object\" }'" .
"\n posthog --type identify --distinctId \"id\" --properties '{ \"json\": \"object\" }'" .
"\n posthog --type alias --distinctId \"id\" --alias \"alias\"" .
"\n posthog --type groupIdentify --groupType \"organization\" --groupKey \"id:5\" --properties '{ \"json\": \"object\" }'" .
"\n posthog --type groupIdentify --groupType \"organization\" --groupKey \"id:5\" --distinctId \"user-id\" --properties '{ \"json\": \"object\" }'" .
"\n posthog --type getFeatureFlag --flag some-flag --apiKey phc_... --host localhost:8010 --no-ssl" .
"\n posthog --type getFeatureFlag --flag some-flag --apiKey phc_... --host localhost:8010 --no-ssl" .
"\n\n\n";
Expand Down
20 changes: 18 additions & 2 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public static function identify(array $message)
/**
* Adds properties to a group.
*
* @param array $message Must contain keys `groupType`, `groupKey`, `properties`
* @param array $message Must contain keys `groupType`, `groupKey`; accepts optional `properties`
* and `distinctId`/`distinct_id` to override the default synthetic ID.
* @return boolean whether the groupIdentify call succeeded
* @throws Exception
*/
Expand All @@ -123,9 +124,24 @@ public static function groupIdentify(array $message)
$message["properties"] = array();
}

$distinctId = "\${$message['groupType']}_{$message['groupKey']}";
if (
array_key_exists("distinctId", $message)
&& is_scalar($message["distinctId"])
&& (string) $message["distinctId"] !== ""
) {
$distinctId = (string) $message["distinctId"];
} elseif (
array_key_exists("distinct_id", $message)
&& is_scalar($message["distinct_id"])
&& (string) $message["distinct_id"] !== ""
) {
$distinctId = (string) $message["distinct_id"];
}

$msg = array(
"event" => "\$groupidentify",
"distinctId" => "\${$message['groupType']}_{$message['groupKey']}",
"distinctId" => $distinctId,
"properties" => array(
"\$group_type" => $message["groupType"],
"\$group_key" => $message["groupKey"],
Expand Down
32 changes: 32 additions & 0 deletions test/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,38 @@ public function testContextDoesNotMutateGroupIdentifyProperties(): void
$this->assertArrayNotHasKey('$session_id', $event['properties']);
}

public function testGroupIdentifyAllowsDistinctIdOverride(): void
{
PostHog::groupIdentify([
'groupType' => 'organization',
'groupKey' => 'acme',
'distinctId' => 'user-123',
'properties' => ['name' => 'Acme Inc.'],
]);

$event = $this->flushAndGetEvents()[0];

$this->assertSame('user-123', $event['distinct_id']);
$this->assertSame('$groupidentify', $event['event']);
$this->assertSame('organization', $event['properties']['$group_type']);
$this->assertSame('acme', $event['properties']['$group_key']);
$this->assertSame(['name' => 'Acme Inc.'], $event['properties']['$group_set']);
}

public function testGroupIdentifyAllowsSnakeCaseDistinctIdOverride(): void
{
PostHog::groupIdentify([
'groupType' => 'organization',
'groupKey' => 'acme',
'distinct_id' => 'snake-user-123',
'properties' => ['name' => 'Acme Inc.'],
]);

$event = $this->flushAndGetEvents()[0];

$this->assertSame('snake-user-123', $event['distinct_id']);
}

public function testEvaluateFlagsUsesContextDistinctIdWhenOmitted(): void
{
PostHog::withContext(['distinctId' => 'context-user'], function (): void {
Expand Down
Loading