posthog-js 1.29.3 → 1.30.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
@@ -188,6 +188,11 @@ interface PostHogConfig {
188
188
  _onCapture: (eventName: string, eventData: CaptureResult) => void;
189
189
  _capture_metrics: boolean;
190
190
  _capture_performance: boolean;
191
+ bootstrap: {
192
+ distinctID?: string;
193
+ isIdentifiedID?: boolean;
194
+ featureFlags?: Record<string, boolean | string>;
195
+ };
191
196
  }
192
197
  interface OptInOutCapturingOptions {
193
198
  capture: (event: string, properties: Properties, options: CaptureOptions) => void;
@@ -418,7 +423,7 @@ declare class PostHogFeatureFlags {
418
423
  send_event?: boolean;
419
424
  }): boolean;
420
425
  addFeatureFlagsHandler(handler: FeatureFlagsCallback): void;
421
- receivedFeatureFlags(response: DecideResponse): void;
426
+ receivedFeatureFlags(response: Partial<DecideResponse>): void;
422
427
  override(flags: boolean | string[] | Record<string, string | boolean>): void;
423
428
  onFeatureFlags(callback: FeatureFlagsCallback): void;
424
429
  }
@@ -925,6 +930,7 @@ declare class PostHog {
925
930
  * @param {Array} array
926
931
  */
927
932
  _execute_array(array: SnippetArrayItem[]): void;
933
+ _hasBootstrappedFeatureFlags(): boolean;
928
934
  /**
929
935
  * push() keeps the standard async-array-push
930
936
  * behavior around after the lib is loaded.
package/dist/module.js CHANGED
@@ -885,7 +885,7 @@ var LZString = {
885
885
  }
886
886
  };
887
887
 
888
- var version = "1.29.3";
888
+ var version = "1.30.0";
889
889
 
890
890
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
891
891
 
@@ -3294,6 +3294,7 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3294
3294
  }, {
3295
3295
  key: "receivedFeatureFlags",
3296
3296
  value: function receivedFeatureFlags(response) {
3297
+ this.instance.decideEndpointWasHit = true;
3297
3298
  parseFeatureFlagDecideResponse(response, this.instance.persistence);
3298
3299
  var flags = this.getFlags();
3299
3300
  var variants = this.getFlagVariants();
@@ -3971,8 +3972,9 @@ var Decide = /*#__PURE__*/function () {
3971
3972
  function Decide(instance) {
3972
3973
  _classCallCheck(this, Decide);
3973
3974
 
3974
- this.instance = instance;
3975
- this.instance.decideEndpointWasHit = false;
3975
+ this.instance = instance; // don't need to wait for `decide` to return if flags were provided on initialisation
3976
+
3977
+ this.instance.decideEndpointWasHit = this.instance._hasBootstrappedFeatureFlags();
3976
3978
  }
3977
3979
 
3978
3980
  _createClass(Decide, [{
@@ -5958,7 +5960,8 @@ var defaultConfig = function defaultConfig() {
5958
5960
  _capture_metrics: false,
5959
5961
  _capture_performance: false,
5960
5962
  name: 'posthog',
5961
- callback_fn: 'posthog._jsc'
5963
+ callback_fn: 'posthog._jsc',
5964
+ bootstrap: {}
5962
5965
  };
5963
5966
  };
5964
5967
  /**
@@ -6107,6 +6110,8 @@ var PostHog = /*#__PURE__*/function () {
6107
6110
  }, {
6108
6111
  key: "_init",
6109
6112
  value: function _init(token) {
6113
+ var _config$bootstrap;
6114
+
6110
6115
  var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6111
6116
  var name = arguments.length > 2 ? arguments[2] : undefined;
6112
6117
  this.__loaded = true;
@@ -6131,14 +6136,43 @@ var PostHog = /*#__PURE__*/function () {
6131
6136
 
6132
6137
  this._gdpr_init();
6133
6138
 
6139
+ if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6140
+ var _config$bootstrap2;
6141
+
6142
+ var uuid = this.get_config('get_device_id')(_UUID());
6143
+ var deviceID = (_config$bootstrap2 = config.bootstrap) !== null && _config$bootstrap2 !== void 0 && _config$bootstrap2.isIdentifiedID ? uuid : config.bootstrap.distinctID;
6144
+ this.register({
6145
+ distinct_id: config.bootstrap.distinctID,
6146
+ $device_id: deviceID
6147
+ });
6148
+ }
6149
+
6150
+ if (this._hasBootstrappedFeatureFlags()) {
6151
+ var _config$bootstrap3;
6152
+
6153
+ var activeFlags = Object.keys(((_config$bootstrap3 = config.bootstrap) === null || _config$bootstrap3 === void 0 ? void 0 : _config$bootstrap3.featureFlags) || {}).filter(function (flag) {
6154
+ var _config$bootstrap4, _config$bootstrap4$fe;
6155
+
6156
+ return !!((_config$bootstrap4 = config.bootstrap) !== null && _config$bootstrap4 !== void 0 && (_config$bootstrap4$fe = _config$bootstrap4.featureFlags) !== null && _config$bootstrap4$fe !== void 0 && _config$bootstrap4$fe[flag]);
6157
+ }).reduce(function (res, key) {
6158
+ var _config$bootstrap5, _config$bootstrap5$fe;
6159
+
6160
+ 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;
6161
+ }, {});
6162
+ this.featureFlags.receivedFeatureFlags({
6163
+ featureFlags: activeFlags
6164
+ });
6165
+ }
6166
+
6134
6167
  if (!this.get_distinct_id()) {
6135
6168
  // There is no need to set the distinct id
6136
6169
  // or the device id if something was already stored
6137
6170
  // in the persitence
6138
- var uuid = this.get_config('get_device_id')(_UUID());
6171
+ var _uuid = this.get_config('get_device_id')(_UUID());
6172
+
6139
6173
  this.register_once({
6140
- distinct_id: uuid,
6141
- $device_id: uuid
6174
+ distinct_id: _uuid,
6175
+ $device_id: _uuid
6142
6176
  }, '');
6143
6177
  } // Set up event handler for pageleave
6144
6178
  // Use `onpagehide` if available, see https://calendar.perfplanet.com/2020/beaconing-in-practice/#beaconing-reliability-avoiding-abandons
@@ -6419,6 +6453,13 @@ var PostHog = /*#__PURE__*/function () {
6419
6453
  execute(other_calls, this);
6420
6454
  execute(capturing_calls, this);
6421
6455
  }
6456
+ }, {
6457
+ key: "_hasBootstrappedFeatureFlags",
6458
+ value: function _hasBootstrappedFeatureFlags() {
6459
+ var _this$config$bootstra, _this$config$bootstra2;
6460
+
6461
+ return ((_this$config$bootstra = this.config.bootstrap) === null || _this$config$bootstra === void 0 ? void 0 : _this$config$bootstra.featureFlags) && Object.keys((_this$config$bootstra2 = this.config.bootstrap) === null || _this$config$bootstra2 === void 0 ? void 0 : _this$config$bootstra2.featureFlags).length > 0 || false;
6462
+ }
6422
6463
  /**
6423
6464
  * push() keeps the standard async-array-push
6424
6465
  * behavior around after the lib is loaded.