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 group analytics support #26

Merged
merged 4 commits into from
Oct 28, 2021
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
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\" }'" .
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually for just getting rid of the CLIs altogether - I think a lot of them are broken too.

Copy link
Contributor

Choose a reason for hiding this comment

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

not relevant here though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the CLI was the only way I could test this though. Fixed what I found broken though

"\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());
}
}
}