Skip to content

Commit

Permalink
Merge pull request #26 from PostHog/group-analytics-support
Browse files Browse the repository at this point in the history
Add group analytics support
  • Loading branch information
macobo authored Oct 28, 2021
2 parents eeec32d + 2095a02 commit d1d9c14
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
29 changes: 26 additions & 3 deletions bin/posthog
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ $options = getopt(
'event:',
'properties:',
'alias:',
'groupType:',
'groupKey:',
'no-ssl',
'debug'
)
);
Expand All @@ -30,7 +33,14 @@ if (empty($options['apiKey'])) {
error('apiKey flag required');
}

PostHog::init($options['apiKey'], array('host' => $options['host'], 'debug' => array_key_exists('debug', $options)));
PostHog::init(
$options['apiKey'],
array(
'host' => $options['host'],
'debug' => array_key_exists('debug', $options),
'ssl' => !array_key_exists('no-ssl', $options)
)
);

switch ($options['type']) {
case 'capture':
Expand Down Expand Up @@ -61,6 +71,16 @@ switch ($options['type']) {
);
break;

case 'groupIdentify':
PostHog::groupIdentify(
array(
'groupType' => $options['groupType'],
'groupKey' => $options['groupKey'],
'properties' => parse_json($options['properties'])
)
);
break;

default:
error(usage());
break;
Expand All @@ -72,12 +92,15 @@ function usage(): string
{
return "\n Common keys:" .
"\n --apiKey KEY" .
"\n --host HOST" .
"\n --debug" .
"\n --no-ssl" .
"\n" .
"\n Usage:" .
"\n posthog --type capture --distinctId \"id\" --event \"event name\" --properties \"{ json: 'object' }\"" .
"\n posthog --type identify --distinctId \"id\" --properties \"{ json: 'object' }" .
"\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\n\n";
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
"bin": [
"bin/posthog"
]
}
}
5 changes: 2 additions & 3 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Client
* @var Consumer
*/
protected $consumer;

/**
* @var HttpClient
*/
Expand All @@ -56,7 +56,7 @@ public function __construct(string $apiKey, array $options = [], ?HttpClient $ht
10000,
false,
$options["debug"] ?? false
);
);
}

public function __destruct()
Expand Down Expand Up @@ -276,5 +276,4 @@ private function message($msg)

return $msg;
}

}
28 changes: 28 additions & 0 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ public static function identify(array $message)
return self::$client->identify($message);
}

/**
* Adds properties to a group.
*
* @param array $message Must contain keys `groupType`, `groupKey`, `properties`
* @return boolean whether the groupIdentify call succeeded
* @throws Exception
*/
public static function groupIdentify(array $message)
{
self::assert(!empty($message["groupType"]), "PostHog::groupIdentify() expects a groupType");
self::assert(!empty($message["groupKey"]), "PostHog::groupIdentify() expects a groupKey");

if (!isset($message["properties"])) {
$message["properties"] = array();
}

$msg = array(
"event" => "\$groupidentify",
"distinctId" => "\${$message['groupType']}_{$message['groupKey']}",
"properties" => array(
"\$group_type" => $message["groupType"],
"\$group_key" => $message["groupKey"],
"\$group_set" => $message["properties"],
)
);

return self::capture($msg);
}

/**
* decide if the feature flag is enabled for this distinct id.
Expand Down
33 changes: 33 additions & 0 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,37 @@ public function testTimestamps(): void
)
);
}

public function testGroupIdentify(): void
{
self::assertTrue(
PostHog::groupIdentify(
array(
"groupType" => "company",
"groupKey" => "id:5",
"properties" => array(
"foo" => "bar"
)
)
)
);

self::assertTrue(
PostHog::groupIdentify(
array(
"groupType" => "company",
"groupKey" => "id:5",
)
)
);
}

public function testGroupIdentifyValidation(): void
{
try {
Posthog::groupIdentify(array());
} catch (Exception $e) {
$this->assertEquals("PostHog::groupIdentify() expects a groupType", $e->getMessage());
}
}
}

0 comments on commit d1d9c14

Please sign in to comment.