posthog-js 1.40.2 → 1.41.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.
package/dist/module.d.ts CHANGED
@@ -223,6 +223,7 @@ interface PostHogConfig {
223
223
  distinctID?: string;
224
224
  isIdentifiedID?: boolean;
225
225
  featureFlags?: Record<string, boolean | string>;
226
+ featureFlagPayloads?: Record<string, JsonType>;
226
227
  };
227
228
  segment?: any;
228
229
  }
@@ -293,6 +294,7 @@ interface XHRParams extends QueuedRequestData {
293
294
  captureMetrics: CaptureMetrics;
294
295
  retryQueue: RetryQueue;
295
296
  onXHRError: (req: XMLHttpRequest) => void;
297
+ timeout?: number;
296
298
  }
297
299
  interface DecideResponse {
298
300
  status: number;
@@ -302,6 +304,8 @@ interface DecideResponse {
302
304
  };
303
305
  custom_properties: AutoCaptureCustomProperty[];
304
306
  featureFlags: Record<string, string | boolean>;
307
+ featureFlagPayloads: Record<string, JsonType>;
308
+ errorsWhileComputingFlags: boolean;
305
309
  capturePerformance?: boolean;
306
310
  sessionRecording?: {
307
311
  endpoint?: string;
@@ -377,6 +381,9 @@ interface JSC {
377
381
  [key: string]: (response: any) => void;
378
382
  }
379
383
  declare type SnippetArrayItem = [method: string, ...args: any[]];
384
+ declare type JsonType = string | number | boolean | null | {
385
+ [key: string]: JsonType;
386
+ } | Array<JsonType>;
380
387
 
381
388
  /**
382
389
  * PostHog People Object
@@ -458,6 +465,7 @@ declare class PostHogFeatureFlags {
458
465
  constructor(instance: PostHog);
459
466
  getFlags(): string[];
460
467
  getFlagVariants(): Record<string, string | boolean>;
468
+ getFlagPayloads(): Record<string, JsonType>;
461
469
  /**
462
470
  * Reloads feature flags asynchronously.
463
471
  *
@@ -476,6 +484,7 @@ declare class PostHogFeatureFlags {
476
484
  getFeatureFlag(key: string, options?: {
477
485
  send_event?: boolean;
478
486
  }): boolean | string;
487
+ getFeatureFlagPayload(key: string): JsonType;
479
488
  isFeatureEnabled(key: string, options?: {
480
489
  send_event?: boolean;
481
490
  }): boolean;
@@ -1108,6 +1117,7 @@ declare class PostHog {
1108
1117
  getFeatureFlag(key: string, options?: {
1109
1118
  send_event?: boolean;
1110
1119
  }): boolean | string | undefined;
1120
+ getFeatureFlagPayload(key: string): JsonType;
1111
1121
  isFeatureEnabled(key: string, options?: isFeatureEnabledOptions): boolean;
1112
1122
  reloadFeatureFlags(): void;
1113
1123
  onFeatureFlags(callback: (flags: string[], variants: Record<string, boolean | string>) => void): void;
@@ -1505,4 +1515,4 @@ declare class PostHog {
1505
1515
 
1506
1516
  declare const posthog: PostHog;
1507
1517
 
1508
- export { AutoCaptureCustomProperty, AutocaptureCompatibleElement, AutocaptureConfig, Breaker, CaptureCallback, CaptureOptions, CaptureResult, Compression, CompressionData, DecideResponse, DomAutocaptureEvents, EventHandler, FeatureFlagsCallback, GDPROptions, JSC, OptInOutCapturingOptions, PersistentStore, PostData, PostHog, PostHogConfig, Properties, Property, QueuedRequestData, RequestCallback, RetryQueueElement, SessionRecordingOptions, SnippetArrayItem, ToolbarParams, ToolbarSource, ToolbarUserIntent, ToolbarVersion, XHROptions, XHRParams, posthog as default, isFeatureEnabledOptions, posthog };
1518
+ export { AutoCaptureCustomProperty, AutocaptureCompatibleElement, AutocaptureConfig, Breaker, CaptureCallback, CaptureOptions, CaptureResult, Compression, CompressionData, DecideResponse, DomAutocaptureEvents, EventHandler, FeatureFlagsCallback, GDPROptions, JSC, JsonType, OptInOutCapturingOptions, PersistentStore, PostData, PostHog, PostHogConfig, Properties, Property, QueuedRequestData, RequestCallback, RetryQueueElement, SessionRecordingOptions, SnippetArrayItem, ToolbarParams, ToolbarSource, ToolbarUserIntent, ToolbarVersion, XHROptions, XHRParams, posthog as default, isFeatureEnabledOptions, posthog };
package/dist/module.js CHANGED
@@ -921,7 +921,7 @@ var LZString = {
921
921
  }
922
922
  };
923
923
 
924
- var version = "1.40.2";
924
+ var version = "1.41.0";
925
925
 
926
926
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
927
927
 
@@ -3172,12 +3172,21 @@ var PostHogPeople = /*#__PURE__*/function () {
3172
3172
  return PostHogPeople;
3173
3173
  }();
3174
3174
 
3175
+ var PERSISTENCE_ACTIVE_FEATURE_FLAGS = '$active_feature_flags';
3176
+ var PERSISTENCE_ENABLED_FEATURE_FLAGS = '$enabled_feature_flags';
3177
+ var PERSISTENCE_OVERRIDE_FEATURE_FLAGS = '$override_feature_flags';
3178
+ var PERSISTENCE_FEATURE_FLAG_PAYLOADS = '$feature_flag_payloads';
3175
3179
  var parseFeatureFlagDecideResponse = function parseFeatureFlagDecideResponse(response, persistence) {
3180
+ var currentFlags = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
3181
+ var currentFlagPayloads = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3176
3182
  var flags = response['featureFlags'];
3183
+ var flagPayloads = response['featureFlagPayloads'];
3177
3184
 
3178
3185
  if (flags) {
3179
3186
  // using the v1 api
3180
3187
  if (Array.isArray(flags)) {
3188
+ var _persistence$register;
3189
+
3181
3190
  var $enabled_feature_flags = {};
3182
3191
 
3183
3192
  if (flags) {
@@ -3186,21 +3195,27 @@ var parseFeatureFlagDecideResponse = function parseFeatureFlagDecideResponse(res
3186
3195
  }
3187
3196
  }
3188
3197
 
3189
- persistence && persistence.register({
3190
- $active_feature_flags: flags,
3191
- $enabled_feature_flags: $enabled_feature_flags
3192
- });
3198
+ persistence && persistence.register((_persistence$register = {}, _defineProperty(_persistence$register, PERSISTENCE_ACTIVE_FEATURE_FLAGS, flags), _defineProperty(_persistence$register, PERSISTENCE_ENABLED_FEATURE_FLAGS, $enabled_feature_flags), _persistence$register));
3193
3199
  } else {
3194
- // using the v2 api
3195
- persistence && persistence.register({
3196
- $active_feature_flags: Object.keys(flags || {}),
3197
- $enabled_feature_flags: flags || {}
3198
- });
3200
+ var _persistence$register2;
3201
+
3202
+ // using the v2+ api
3203
+ var newFeatureFlags = flags;
3204
+ var newFeatureFlagPayloads = flagPayloads;
3205
+
3206
+ if (response.errorsWhileComputingFlags) {
3207
+ // if not all flags were computed, we upsert flags instead of replacing them
3208
+ newFeatureFlags = _objectSpread2(_objectSpread2({}, currentFlags), newFeatureFlags);
3209
+ newFeatureFlagPayloads = _objectSpread2(_objectSpread2({}, currentFlagPayloads), newFeatureFlagPayloads);
3210
+ }
3211
+
3212
+ persistence && persistence.register((_persistence$register2 = {}, _defineProperty(_persistence$register2, PERSISTENCE_ACTIVE_FEATURE_FLAGS, Object.keys(newFeatureFlags || {})), _defineProperty(_persistence$register2, PERSISTENCE_ENABLED_FEATURE_FLAGS, newFeatureFlags || {}), _defineProperty(_persistence$register2, PERSISTENCE_FEATURE_FLAG_PAYLOADS, newFeatureFlagPayloads || {}), _persistence$register2));
3199
3213
  }
3200
3214
  } else {
3201
3215
  if (persistence) {
3202
- persistence.unregister('$active_feature_flags');
3203
- persistence.unregister('$enabled_feature_flags');
3216
+ persistence.unregister(PERSISTENCE_ACTIVE_FEATURE_FLAGS);
3217
+ persistence.unregister(PERSISTENCE_ENABLED_FEATURE_FLAGS);
3218
+ persistence.unregister(PERSISTENCE_FEATURE_FLAG_PAYLOADS);
3204
3219
  }
3205
3220
  }
3206
3221
  };
@@ -3224,8 +3239,8 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3224
3239
  }, {
3225
3240
  key: "getFlagVariants",
3226
3241
  value: function getFlagVariants() {
3227
- var enabledFlags = this.instance.get_property('$enabled_feature_flags');
3228
- var overriddenFlags = this.instance.get_property('$override_feature_flags');
3242
+ var enabledFlags = this.instance.get_property(PERSISTENCE_ENABLED_FEATURE_FLAGS);
3243
+ var overriddenFlags = this.instance.get_property(PERSISTENCE_OVERRIDE_FEATURE_FLAGS);
3229
3244
 
3230
3245
  if (!overriddenFlags) {
3231
3246
  return enabledFlags || {};
@@ -3254,6 +3269,12 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3254
3269
 
3255
3270
  return finalFlags;
3256
3271
  }
3272
+ }, {
3273
+ key: "getFlagPayloads",
3274
+ value: function getFlagPayloads() {
3275
+ var flagPayloads = this.instance.get_property(PERSISTENCE_FEATURE_FLAG_PAYLOADS);
3276
+ return flagPayloads || {};
3277
+ }
3257
3278
  /**
3258
3279
  * Reloads feature flags asynchronously.
3259
3280
  *
@@ -3319,7 +3340,7 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3319
3340
 
3320
3341
  var encoded_data = _base64Encode(json_data);
3321
3342
 
3322
- this.instance._send_request(this.instance.get_config('api_host') + '/decide/?v=2', {
3343
+ this.instance._send_request(this.instance.get_config('api_host') + '/decide/?v=3', {
3323
3344
  data: encoded_data
3324
3345
  }, {
3325
3346
  method: 'POST'
@@ -3328,7 +3349,9 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3328
3349
  // makes it through
3329
3350
  _this2.$anon_distinct_id = undefined;
3330
3351
 
3331
- _this2.receivedFeatureFlags(response); // :TRICKY: Reload - start another request if queued!
3352
+ if (response && response.status === 200) {
3353
+ _this2.receivedFeatureFlags(response);
3354
+ } // :TRICKY: Reload - start another request if queued!
3332
3355
 
3333
3356
 
3334
3357
  _this2.setReloadingPaused(false);
@@ -3369,6 +3392,12 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3369
3392
 
3370
3393
  return flagValue;
3371
3394
  }
3395
+ }, {
3396
+ key: "getFeatureFlagPayload",
3397
+ value: function getFeatureFlagPayload(key) {
3398
+ var payloads = this.getFlagPayloads();
3399
+ return payloads[key];
3400
+ }
3372
3401
  /*
3373
3402
  * See if feature flag is enabled for user.
3374
3403
  *
@@ -3401,7 +3430,9 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3401
3430
  key: "receivedFeatureFlags",
3402
3431
  value: function receivedFeatureFlags(response) {
3403
3432
  this.instance.decideEndpointWasHit = true;
3404
- parseFeatureFlagDecideResponse(response, this.instance.persistence);
3433
+ var currentFlags = this.getFlagVariants();
3434
+ var currentFlagPayloads = this.getFlagPayloads();
3435
+ parseFeatureFlagDecideResponse(response, this.instance.persistence, currentFlags, currentFlagPayloads);
3405
3436
  var flags = this.getFlags();
3406
3437
  var variants = this.getFlagVariants();
3407
3438
  this.featureFlagEventHandlers.forEach(function (handler) {
@@ -3426,7 +3457,7 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3426
3457
  this._override_warning = false;
3427
3458
 
3428
3459
  if (flags === false) {
3429
- this.instance.persistence.unregister('$override_feature_flags');
3460
+ this.instance.persistence.unregister(PERSISTENCE_OVERRIDE_FEATURE_FLAGS);
3430
3461
  } else if (Array.isArray(flags)) {
3431
3462
  var flagsObj = {};
3432
3463
 
@@ -3434,13 +3465,9 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3434
3465
  flagsObj[flags[i]] = true;
3435
3466
  }
3436
3467
 
3437
- this.instance.persistence.register({
3438
- $override_feature_flags: flagsObj
3439
- });
3468
+ this.instance.persistence.register(_defineProperty({}, PERSISTENCE_OVERRIDE_FEATURE_FLAGS, flagsObj));
3440
3469
  } else {
3441
- this.instance.persistence.register({
3442
- $override_feature_flags: flags
3443
- });
3470
+ this.instance.persistence.register(_defineProperty({}, PERSISTENCE_OVERRIDE_FEATURE_FLAGS, flags));
3444
3471
  }
3445
3472
  }
3446
3473
  /*
@@ -4344,7 +4371,7 @@ var Decide = /*#__PURE__*/function () {
4344
4371
 
4345
4372
  var encoded_data = _base64Encode(json_data);
4346
4373
 
4347
- this.instance._send_request("".concat(this.instance.get_config('api_host'), "/decide/?v=2"), {
4374
+ this.instance._send_request("".concat(this.instance.get_config('api_host'), "/decide/?v=3"), {
4348
4375
  data: encoded_data,
4349
4376
  verbose: true
4350
4377
  }, {
@@ -4379,7 +4406,11 @@ var Decide = /*#__PURE__*/function () {
4379
4406
  (_this$instance$sessio = this.instance.sessionRecording) === null || _this$instance$sessio === void 0 ? void 0 : _this$instance$sessio.afterDecideResponse(response);
4380
4407
  autocapture.afterDecideResponse(response, this.instance);
4381
4408
  (_this$instance$webPer = this.instance.webPerformance) === null || _this$instance$webPer === void 0 ? void 0 : _this$instance$webPer.afterDecideResponse(response);
4382
- this.instance.featureFlags.receivedFeatureFlags(response);
4409
+
4410
+ if (response && response.status === 200) {
4411
+ this.instance.featureFlags.receivedFeatureFlags(response);
4412
+ }
4413
+
4383
4414
  this.instance['compression'] = {};
4384
4415
 
4385
4416
  if (response['supportedCompression'] && !this.instance.get_config('disable_compression')) {
@@ -5728,7 +5759,9 @@ var xhr = function xhr(_ref) {
5728
5759
  callback = _ref.callback,
5729
5760
  retriesPerformedSoFar = _ref.retriesPerformedSoFar,
5730
5761
  retryQueue = _ref.retryQueue,
5731
- onXHRError = _ref.onXHRError;
5762
+ onXHRError = _ref.onXHRError,
5763
+ _ref$timeout = _ref.timeout,
5764
+ timeout = _ref$timeout === void 0 ? 10000 : _ref$timeout;
5732
5765
  var req = new XMLHttpRequest();
5733
5766
  req.open(options.method || 'GET', url, true);
5734
5767
  var body = encodePostData(data, options);
@@ -5741,9 +5774,10 @@ var xhr = function xhr(_ref) {
5741
5774
 
5742
5775
  if (options.method === 'POST' && !options.blob) {
5743
5776
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
5744
- } // send the ph_optout cookie
5745
- // withCredentials cannot be modified until after calling .open on Android and Mobile Safari
5777
+ }
5746
5778
 
5779
+ req.timeout = timeout; // send the ph_optout cookie
5780
+ // withCredentials cannot be modified until after calling .open on Android and Mobile Safari
5747
5781
 
5748
5782
  req.withCredentials = true;
5749
5783
 
@@ -6625,7 +6659,7 @@ var PostHog = /*#__PURE__*/function () {
6625
6659
  }
6626
6660
 
6627
6661
  if (this._hasBootstrappedFeatureFlags()) {
6628
- var _config$bootstrap3;
6662
+ var _config$bootstrap3, _config$bootstrap6;
6629
6663
 
6630
6664
  var activeFlags = Object.keys(((_config$bootstrap3 = config.bootstrap) === null || _config$bootstrap3 === void 0 ? void 0 : _config$bootstrap3.featureFlags) || {}).filter(function (flag) {
6631
6665
  var _config$bootstrap4, _config$bootstrap4$fe;
@@ -6636,8 +6670,22 @@ var PostHog = /*#__PURE__*/function () {
6636
6670
 
6637
6671
  return res[key] = ((_config$bootstrap5 = config.bootstrap) === null || _config$bootstrap5 === void 0 ? void 0 : (_config$bootstrap5$fe = _config$bootstrap5.featureFlags) === null || _config$bootstrap5$fe === void 0 ? void 0 : _config$bootstrap5$fe[key]) || false, res;
6638
6672
  }, {});
6673
+ var featureFlagPayloads = Object.keys(((_config$bootstrap6 = config.bootstrap) === null || _config$bootstrap6 === void 0 ? void 0 : _config$bootstrap6.featureFlagPayloads) || {}).filter(function (key) {
6674
+ return activeFlags[key];
6675
+ }).reduce(function (res, key) {
6676
+ var _config$bootstrap7, _config$bootstrap7$fe;
6677
+
6678
+ if ((_config$bootstrap7 = config.bootstrap) !== null && _config$bootstrap7 !== void 0 && (_config$bootstrap7$fe = _config$bootstrap7.featureFlagPayloads) !== null && _config$bootstrap7$fe !== void 0 && _config$bootstrap7$fe[key]) {
6679
+ var _config$bootstrap8, _config$bootstrap8$fe;
6680
+
6681
+ res[key] = (_config$bootstrap8 = config.bootstrap) === null || _config$bootstrap8 === void 0 ? void 0 : (_config$bootstrap8$fe = _config$bootstrap8.featureFlagPayloads) === null || _config$bootstrap8$fe === void 0 ? void 0 : _config$bootstrap8$fe[key];
6682
+ }
6683
+
6684
+ return res;
6685
+ }, {});
6639
6686
  this.featureFlags.receivedFeatureFlags({
6640
- featureFlags: activeFlags
6687
+ featureFlags: activeFlags,
6688
+ featureFlagPayloads: featureFlagPayloads
6641
6689
  });
6642
6690
  }
6643
6691
 
@@ -7226,6 +7274,30 @@ var PostHog = /*#__PURE__*/function () {
7226
7274
  value: function getFeatureFlag(key, options) {
7227
7275
  return this.featureFlags.getFeatureFlag(key, options);
7228
7276
  }
7277
+ /*
7278
+ * Get feature flag payload value matching key for user (supports multivariate flags).
7279
+ *
7280
+ * ### Usage:
7281
+ *
7282
+ * if(posthog.getFeatureFlag('beta-feature') === 'some-value') {
7283
+ * const someValue = posthog.getFeatureFlagPayload('beta-feature')
7284
+ * // do something
7285
+ * }
7286
+ *
7287
+ * @param {Object|String} prop Key of the feature flag.
7288
+ */
7289
+
7290
+ }, {
7291
+ key: "getFeatureFlagPayload",
7292
+ value: function getFeatureFlagPayload(key) {
7293
+ var payload = this.featureFlags.getFeatureFlagPayload(key);
7294
+
7295
+ try {
7296
+ return JSON.parse(payload);
7297
+ } catch (_unused) {
7298
+ return payload;
7299
+ }
7300
+ }
7229
7301
  /*
7230
7302
  * See if feature flag is enabled for user.
7231
7303
  *