Skip to content

Commit

Permalink
feat(flags): Add local props and flags to all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Jan 4, 2024
1 parent 3c76a85 commit b498848
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
54 changes: 52 additions & 2 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,22 @@ public function capture(array $message)
$message["properties"][sprintf('$feature/%s', $flagKey)] = $flagValue;
}

// Add all feature flag keys to $active_feature_flags key
$message["properties"]['$active_feature_flags'] = array_keys($flags);
// Add all feature flag keys that aren't false to $active_feature_flags
// decide v2 does this automatically, but we need it for when we upgrade to v3
$message["properties"]['$active_feature_flags'] = array_keys(array_filter($flags, function ($flagValue) {
return $flagValue !== false;
}));
} elseif (count($this->featureFlags) != 0) {
# Local evaluation is enabled, flags are loaded, so try and get all flags we can without going to the server
$flags = $this->getAllFlags($message["distinct_id"], $message["groups"], [], [], true);

// Add all feature variants to event
foreach ($flags as $flagKey => $flagValue) {
$message["properties"][sprintf('$feature/%s', $flagKey)] = $flagValue;
}
$message["properties"]['$active_feature_flags'] = array_keys(array_filter($flags, function ($flagValue) {
return $flagValue !== false;
}));
}

return $this->consumer->capture($message);
Expand Down Expand Up @@ -205,6 +219,12 @@ public function getFeatureFlag(
bool $onlyEvaluateLocally = false,
bool $sendFeatureFlagEvents = true
): null | bool | string {
[$personProperties, $groupProperties] = $this->addLocalPersonAndGroupProperties(
$distinctId,
$groups,
$personProperties,
$groupProperties
);
$result = null;

foreach ($this->featureFlags as $flag) {
Expand Down Expand Up @@ -278,6 +298,12 @@ public function getAllFlags(
array $groupProperties = array(),
bool $onlyEvaluateLocally = false
): array {
[$personProperties, $groupProperties] = $this->addLocalPersonAndGroupProperties(
$distinctId,
$groups,
$personProperties,
$groupProperties
);
$response = [];
$fallbackToDecide = false;

Expand Down Expand Up @@ -567,4 +593,28 @@ private function message($msg)

return $msg;
}

private function addLocalPersonAndGroupProperties(
string $distinctId,
array $groups,
array $personProperties,
array $groupProperties
): array {
$allPersonProperties = array_merge(
["\$current_distinct_id" => $distinctId],
$personProperties
);

$allGroupProperties = [];
if (count($groups) > 0) {
foreach ($groups as $groupName => $groupValue) {
$allGroupProperties[$groupName] = array_merge(
["\$group_key" => $groupValue],
$groupProperties[$groupName] ?? []
);
}
}

return [$allPersonProperties, $allGroupProperties];
}
}
74 changes: 70 additions & 4 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testIsFeatureEnabled()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id"}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"$current_distinct_id":"user-id"}}', self::FAKE_API_KEY),
),
)
);
Expand All @@ -151,7 +151,7 @@ public function testIsFeatureEnabledGroups()
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
self::FAKE_API_KEY
),
),
Expand All @@ -171,7 +171,7 @@ public function testGetFeatureFlag()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id"}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"$current_distinct_id":"user-id"}}', self::FAKE_API_KEY),
),
)
);
Expand Down Expand Up @@ -201,7 +201,7 @@ public function testGetFeatureFlagGroups()
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
self::FAKE_API_KEY
),
),
Expand Down Expand Up @@ -353,4 +353,70 @@ public function testGroupIdentifyValidation(): void
$this->assertEquals("PostHog::groupIdentify() expects a groupType", $e->getMessage());
}
}

public function testDefaultPropertiesGetAddedProperly(): void
{
PostHog::getFeatureFlag('random_key', 'some_id', array("company" => "id:5", "instance" => "app.posthog.com"), array("x1" => "y1"), array("company" => array("x" => "y")));
$this->assertEquals(
$this->http_client->calls,
array(
0 => array(
"path" => "/api/feature_flag/local_evaluation?token=random_key",
"payload" => null,
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);

// reset calls
$this->http_client->calls = array();

PostHog::getFeatureFlag(
'random_key',
'some_id',
array("company" => "id:5", "instance" => "app.posthog.com"),
array("\$current_distinct_id" => "override"),
array("company" => array("\$group_key" => "group_override"), "instance" => array("\$group_key" => "app.posthog.com"))
);
$this->assertEquals(
$this->http_client->calls,
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"override"},"group_properties":{"company":{"$group_key":"group_override"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);
// reset calls
$this->http_client->calls = array();

# test empty
PostHog::getFeatureFlag('random_key', 'some_id', array("company" => "id:5"), [], []);
$this->assertEquals(
$this->http_client->calls,
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"some_id"},"group_properties":{"company":{"$group_key":"id:5"}}}', self::FAKE_API_KEY),
),
)
);

// reset calls
$this->http_client->calls = array();

PostHog::isFeatureEnabled('random_key', 'some_id', array("company" => "id:5", "instance" => "app.posthog.com"), array("x1" => "y1"), array("company" => array("x" => "y")));
$this->assertEquals(
$this->http_client->calls,
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);
}
}

0 comments on commit b498848

Please sign in to comment.