posthog-node 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -915,7 +915,7 @@ function setupExpressErrorHandler(_posthog, app) {
915
915
  });
916
916
  }
917
917
 
918
- var version = "5.4.0";
918
+ var version = "5.5.0";
919
919
 
920
920
  var PostHogPersistedProperty;
921
921
  (function (PostHogPersistedProperty) {
@@ -2839,7 +2839,8 @@ class PostHogBackendClient extends PostHogCoreStateless {
2839
2839
  const capturePromise = Promise.resolve().then(async () => {
2840
2840
  if (sendFeatureFlags) {
2841
2841
  // If we are sending feature flags, we evaluate them locally if the user prefers it, otherwise we fall back to remote evaluation
2842
- return await this.getFeatureFlagsForEvent(distinctId, groups, properties, disableGeoip);
2842
+ const sendFeatureFlagsOptions = typeof sendFeatureFlags === 'object' ? sendFeatureFlags : undefined;
2843
+ return await this.getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions);
2843
2844
  }
2844
2845
  if (event === '$feature_flag_called') {
2845
2846
  // If we're capturing a $feature_flag_called event, we don't want to enrich the event with cached flags that may be out of date.
@@ -2896,7 +2897,8 @@ class PostHogBackendClient extends PostHogCoreStateless {
2896
2897
  const capturePromise = Promise.resolve().then(async () => {
2897
2898
  if (sendFeatureFlags) {
2898
2899
  // If we are sending feature flags, we evaluate them locally if the user prefers it, otherwise we fall back to remote evaluation
2899
- return await this.getFeatureFlagsForEvent(distinctId, groups, properties, disableGeoip);
2900
+ const sendFeatureFlagsOptions = typeof sendFeatureFlags === 'object' ? sendFeatureFlags : undefined;
2901
+ return await this.getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions);
2900
2902
  }
2901
2903
  if (event === '$feature_flag_called') {
2902
2904
  // If we're capturing a $feature_flag_called event, we don't want to enrich the event with cached flags that may be out of date.
@@ -3263,13 +3265,32 @@ class PostHogBackendClient extends PostHogCoreStateless {
3263
3265
  groupProperties
3264
3266
  };
3265
3267
  }
3266
- async getFeatureFlagsForEvent(distinctId, groups, eventProperties, disableGeoip) {
3267
- // Extract person and group properties from the event properties
3268
- const {
3269
- personProperties: cleanPersonProperties,
3270
- groupProperties: cleanGroupProperties
3271
- } = this.extractPropertiesFromEvent(eventProperties, groups);
3272
- // Prefer local evaluation if available
3268
+ async getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions) {
3269
+ // Use properties directly from options if they exist
3270
+ const finalPersonProperties = sendFeatureFlagsOptions?.personProperties || {};
3271
+ const finalGroupProperties = sendFeatureFlagsOptions?.groupProperties || {};
3272
+ // Check if we should only evaluate locally
3273
+ const onlyEvaluateLocally = sendFeatureFlagsOptions?.onlyEvaluateLocally ?? false;
3274
+ // If onlyEvaluateLocally is true, only use local evaluation
3275
+ if (onlyEvaluateLocally) {
3276
+ if ((this.featureFlagsPoller?.featureFlags?.length || 0) > 0) {
3277
+ const groupsWithStringValues = {};
3278
+ for (const [key, value] of Object.entries(groups || {})) {
3279
+ groupsWithStringValues[key] = String(value);
3280
+ }
3281
+ return await this.getAllFlags(distinctId, {
3282
+ groups: groupsWithStringValues,
3283
+ personProperties: finalPersonProperties,
3284
+ groupProperties: finalGroupProperties,
3285
+ disableGeoip,
3286
+ onlyEvaluateLocally: true
3287
+ });
3288
+ } else {
3289
+ // If onlyEvaluateLocally is true but we don't have local flags, return empty
3290
+ return {};
3291
+ }
3292
+ }
3293
+ // Prefer local evaluation if available (default behavior; I'd rather not penalize users who haven't updated to the new API but still want to use local evaluation)
3273
3294
  if ((this.featureFlagsPoller?.featureFlags?.length || 0) > 0) {
3274
3295
  const groupsWithStringValues = {};
3275
3296
  for (const [key, value] of Object.entries(groups || {})) {
@@ -3277,14 +3298,14 @@ class PostHogBackendClient extends PostHogCoreStateless {
3277
3298
  }
3278
3299
  return await this.getAllFlags(distinctId, {
3279
3300
  groups: groupsWithStringValues,
3280
- personProperties: cleanPersonProperties,
3281
- groupProperties: cleanGroupProperties,
3301
+ personProperties: finalPersonProperties,
3302
+ groupProperties: finalGroupProperties,
3282
3303
  disableGeoip,
3283
3304
  onlyEvaluateLocally: true
3284
3305
  });
3285
3306
  }
3286
- // Fall back to remote evaluation if local evaluation is not available/is not being used
3287
- return (await super.getFeatureFlagsStateless(distinctId, groups, cleanPersonProperties, cleanGroupProperties, disableGeoip)).flags;
3307
+ // Fall back to remote evaluation if local evaluation is not available
3308
+ return (await super.getFeatureFlagsStateless(distinctId, groups, finalPersonProperties, finalGroupProperties, disableGeoip)).flags;
3288
3309
  }
3289
3310
  addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties) {
3290
3311
  const allPersonProperties = {