posthog-node 5.4.0 → 5.5.1

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.1";
919
919
 
920
920
  var PostHogPersistedProperty;
921
921
  (function (PostHogPersistedProperty) {
@@ -2210,7 +2210,7 @@ class FeatureFlagsPoller {
2210
2210
  }
2211
2211
  } catch (e) {
2212
2212
  if (e instanceof InconclusiveMatchError) {
2213
- this.onError?.(new Error(`Unable to compute flag locally: ${flag.key} - ${e.message}`));
2213
+ this.logMsgIfDebug(() => console.debug(`InconclusiveMatchError when computing flag locally: ${flag.key}: ${e}`));
2214
2214
  } else if (e instanceof Error) {
2215
2215
  this.onError?.(new Error(`Error computing flag locally: ${flag.key}: ${e}`));
2216
2216
  }
@@ -2307,6 +2307,9 @@ class FeatureFlagsPoller {
2307
2307
  let matches = false;
2308
2308
  if (propertyType === 'cohort') {
2309
2309
  matches = matchCohort(prop, properties, this.cohorts, this.debugMode);
2310
+ } else if (propertyType === 'flag') {
2311
+ this.logMsgIfDebug(() => console.warn(`[FEATURE FLAGS] Flag dependency filters are not supported in local evaluation. ` + `Skipping condition for flag '${flag.key}' with dependency on flag '${prop.key || 'unknown'}'`));
2312
+ continue;
2310
2313
  } else {
2311
2314
  matches = matchProperty(prop, properties, warnFunction);
2312
2315
  }
@@ -2644,6 +2647,11 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, deb
2644
2647
  let matches;
2645
2648
  if (prop.type === 'cohort') {
2646
2649
  matches = matchCohort(prop, propertyValues, cohortProperties, debugMode);
2650
+ } else if (prop.type === 'flag') {
2651
+ if (debugMode) {
2652
+ console.warn(`[FEATURE FLAGS] Flag dependency filters are not supported in local evaluation. ` + `Skipping condition with dependency on flag '${prop.key || 'unknown'}'`);
2653
+ }
2654
+ continue;
2647
2655
  } else {
2648
2656
  matches = matchProperty(prop, propertyValues);
2649
2657
  }
@@ -2839,7 +2847,8 @@ class PostHogBackendClient extends PostHogCoreStateless {
2839
2847
  const capturePromise = Promise.resolve().then(async () => {
2840
2848
  if (sendFeatureFlags) {
2841
2849
  // 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);
2850
+ const sendFeatureFlagsOptions = typeof sendFeatureFlags === 'object' ? sendFeatureFlags : undefined;
2851
+ return await this.getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions);
2843
2852
  }
2844
2853
  if (event === '$feature_flag_called') {
2845
2854
  // 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 +2905,8 @@ class PostHogBackendClient extends PostHogCoreStateless {
2896
2905
  const capturePromise = Promise.resolve().then(async () => {
2897
2906
  if (sendFeatureFlags) {
2898
2907
  // 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);
2908
+ const sendFeatureFlagsOptions = typeof sendFeatureFlags === 'object' ? sendFeatureFlags : undefined;
2909
+ return await this.getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions);
2900
2910
  }
2901
2911
  if (event === '$feature_flag_called') {
2902
2912
  // 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 +3273,32 @@ class PostHogBackendClient extends PostHogCoreStateless {
3263
3273
  groupProperties
3264
3274
  };
3265
3275
  }
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
3276
+ async getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions) {
3277
+ // Use properties directly from options if they exist
3278
+ const finalPersonProperties = sendFeatureFlagsOptions?.personProperties || {};
3279
+ const finalGroupProperties = sendFeatureFlagsOptions?.groupProperties || {};
3280
+ // Check if we should only evaluate locally
3281
+ const onlyEvaluateLocally = sendFeatureFlagsOptions?.onlyEvaluateLocally ?? false;
3282
+ // If onlyEvaluateLocally is true, only use local evaluation
3283
+ if (onlyEvaluateLocally) {
3284
+ if ((this.featureFlagsPoller?.featureFlags?.length || 0) > 0) {
3285
+ const groupsWithStringValues = {};
3286
+ for (const [key, value] of Object.entries(groups || {})) {
3287
+ groupsWithStringValues[key] = String(value);
3288
+ }
3289
+ return await this.getAllFlags(distinctId, {
3290
+ groups: groupsWithStringValues,
3291
+ personProperties: finalPersonProperties,
3292
+ groupProperties: finalGroupProperties,
3293
+ disableGeoip,
3294
+ onlyEvaluateLocally: true
3295
+ });
3296
+ } else {
3297
+ // If onlyEvaluateLocally is true but we don't have local flags, return empty
3298
+ return {};
3299
+ }
3300
+ }
3301
+ // 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
3302
  if ((this.featureFlagsPoller?.featureFlags?.length || 0) > 0) {
3274
3303
  const groupsWithStringValues = {};
3275
3304
  for (const [key, value] of Object.entries(groups || {})) {
@@ -3277,14 +3306,14 @@ class PostHogBackendClient extends PostHogCoreStateless {
3277
3306
  }
3278
3307
  return await this.getAllFlags(distinctId, {
3279
3308
  groups: groupsWithStringValues,
3280
- personProperties: cleanPersonProperties,
3281
- groupProperties: cleanGroupProperties,
3309
+ personProperties: finalPersonProperties,
3310
+ groupProperties: finalGroupProperties,
3282
3311
  disableGeoip,
3283
3312
  onlyEvaluateLocally: true
3284
3313
  });
3285
3314
  }
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;
3315
+ // Fall back to remote evaluation if local evaluation is not available
3316
+ return (await super.getFeatureFlagsStateless(distinctId, groups, finalPersonProperties, finalGroupProperties, disableGeoip)).flags;
3288
3317
  }
3289
3318
  addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties) {
3290
3319
  const allPersonProperties = {