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

feat(flags): Add version info to $feature_flag_called request #1811

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

haacked
Copy link
Contributor

@haacked haacked commented Mar 11, 2025

This PR adds /decide?v=4 support to posthog-js. What we get with this support are the following:

A new getFeatureFlagDetails(key) method

This returns the full details of a feature flag. For example:

{
    "beta-feature-flag": {
    "key": "beta-feature-flag",
    "enabled": true,
    "variant": "variant-3",
    "reason": {
        "code": "condition_match",
        "condition_index": 0,
        "description": null
    },
    "metadata": {
        "id": 8,
        "payload": "{\"paylod\": \"3\"}",
        "version": 19,
        "description": ""
    }
}

This includes an evaluation reason (description is currently null, but will be populated in a future PR).
It also includes metadata such as the db id of the flag and its version.

There's also the corresponding getFlagsWithDetails() method to get all the flag details.

Updates to the $feature_flag_called event

When capturing the $feature_flag_called event, additional information are now captured:

  1. $feature_flag_version: The version of the feature flag.
  2. $feature_flag_id: The database id of the flag. A flag can be deleted and recreated with the same key, so this helps us determine if that's the case.
  3. $feature_flag_reason: The reason the flag evaluated as it did. For now, it's just the evaluation code, but once [Flag reporting improvements] Add evaluation reason to decide response posthog#29445 is completed, this will include a human friendly description.
  4. $feature_flag_original_response: This contains the original response in the situation that the client overrode the flag via a call to overrideFeatureFlags().
  5. $feature_flag_original_payload: This contains the original payload in the situation that the client overrode the payload via a call to overrideFeatureFlags().

Sending the original values will help debug in the rare situation that a customer is confused by a flag result and forgot they were overriding it. 😆

Backwards compatibility:

The changes are all backwards compatible with /decide?v=3. Any requests for /decide?v=4 today will return a v=3 response. Once PostHog/posthog#29751 is deployed, then these new additions will "light up".

Copy link

vercel bot commented Mar 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Mar 14, 2025 10:12pm

Copy link

github-actions bot commented Mar 11, 2025

Size Change: +24.3 kB (+0.68%)

Total Size: 3.6 MB

Filename Size Change
dist/array.full.es5.js 277 kB +2.59 kB (+0.94%)
dist/array.full.js 380 kB +2.42 kB (+0.64%)
dist/array.full.no-external.js 379 kB +2.42 kB (+0.64%)
dist/array.js 188 kB +2.41 kB (+1.3%)
dist/array.no-external.js 187 kB +2.41 kB (+1.31%)
dist/main.js 189 kB +2.41 kB (+1.29%)
dist/module.full.js 380 kB +2.42 kB (+0.64%)
dist/module.full.no-external.js 379 kB +2.42 kB (+0.64%)
dist/module.js 188 kB +2.41 kB (+1.3%)
dist/module.no-external.js 187 kB +2.41 kB (+1.31%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 220 kB
dist/customizations.full.js 14 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.94 kB
dist/external-scripts-loader.js 2.75 kB
dist/posthog-recorder.js 212 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 71.3 kB
dist/surveys.js 76 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@haacked haacked force-pushed the haacked/add-version-info branch from 4aef13d to f6706fb Compare March 12, 2025 21:15
@haacked haacked force-pushed the haacked/add-version-info branch from f6706fb to e78b1bf Compare March 12, 2025 23:58
@haacked haacked changed the title chore(flags): Add version info to $feature_flag_called request feat(flags): Add version info to $feature_flag_called request Mar 13, 2025
@haacked haacked force-pushed the haacked/add-version-info branch from 7f19dc2 to aaf68fb Compare March 13, 2025 18:00
haacked added 3 commits March 13, 2025 11:01
This also includes the feature flag `version` and the evaluation `reason` in the `$feature_flag_called` event.

This is done in a backwards compatible manner so if it's pointing at `/decide?v=3`, it will still work fine, it'll just lack the version info.

This depends on:  PostHog/posthog#29751
@haacked haacked force-pushed the haacked/add-version-info branch from aaf68fb to a5df719 Compare March 13, 2025 18:01
@haacked haacked marked this pull request as ready for review March 13, 2025 18:02
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR adds support for /decide?v=4 endpoint in the PostHog JavaScript SDK, introducing significant enhancements to feature flag functionality. Here's a summary of the key changes:

  • Added new getFeatureFlagDetails() method returning comprehensive flag information including evaluation reasons, metadata, and version info
  • Enhanced $feature_flag_called event with additional fields like $feature_flag_version, $feature_flag_id, and $feature_flag_reason
  • Added tracking of original flag values when overridden through overrideFeatureFlags() for debugging purposes
  • Added proper handling of quota limiting scenarios for feature flag requests
  • Updated all relevant tests and types while maintaining backwards compatibility with /decide?v=3

The changes appear well-structured with comprehensive test coverage and proper type definitions. The new functionality will help with debugging and provide more detailed insights into feature flag behavior.

7 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
It's a lot of data to send with every single capture.
@haacked
Copy link
Contributor Author

haacked commented Mar 14, 2025

Here's an example showing the additional information:

Screenshot 2025-03-13 at 7 30 32 PM

@@ -71,6 +72,7 @@ export const PERSISTENCE_RESERVED_PROPERTIES = [
ENABLED_FEATURE_FLAGS,
USER_STATE,
PERSISTENCE_EARLY_ACCESS_FEATURES,
PERSISTENCE_FEATURE_FLAG_DETAILS,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you're wondering why I am excluding this, if we don't, it would get rendered like so:

Screenshot 2025-03-13 at 7 06 56 PM

Yikes! Useful info, but maybe too much and we'd need to figure out how to render it more nicely.

@haacked
Copy link
Contributor Author

haacked commented Mar 14, 2025

Made one last (hopefully) change to how we include the reason in the response:

image

I think the whole structure is useful. Later, when we implement a human friendly description, it can be included in there.

I need to dig into this, but by having structured data in there, I hope this'll make it possible to filter on $feature_flag_called events by reason.code so we can see all flags that matched a condition, etc.

Happy to roll that back if others don't like it and would rather it just be the human friendly description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants