posthog-js 1.29.1 → 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/es.js CHANGED
@@ -881,7 +881,7 @@ var LZString = {
881
881
  }
882
882
  };
883
883
 
884
- var version = "1.29.1";
884
+ var version = "1.30.0";
885
885
 
886
886
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
887
887
 
@@ -3290,6 +3290,7 @@ var PostHogFeatureFlags = /*#__PURE__*/function () {
3290
3290
  }, {
3291
3291
  key: "receivedFeatureFlags",
3292
3292
  value: function receivedFeatureFlags(response) {
3293
+ this.instance.decideEndpointWasHit = true;
3293
3294
  parseFeatureFlagDecideResponse(response, this.instance.persistence);
3294
3295
  var flags = this.getFlags();
3295
3296
  var variants = this.getFlagVariants();
@@ -3967,8 +3968,9 @@ var Decide = /*#__PURE__*/function () {
3967
3968
  function Decide(instance) {
3968
3969
  _classCallCheck(this, Decide);
3969
3970
 
3970
- this.instance = instance;
3971
- this.instance.decideEndpointWasHit = false;
3971
+ this.instance = instance; // don't need to wait for `decide` to return if flags were provided on initialisation
3972
+
3973
+ this.instance.decideEndpointWasHit = this.instance._hasBootstrappedFeatureFlags();
3972
3974
  }
3973
3975
 
3974
3976
  _createClass(Decide, [{
@@ -4341,19 +4343,27 @@ var RequestQueue = /*#__PURE__*/function (_RequestQueueScaffold) {
4341
4343
  }, {
4342
4344
  key: "unload",
4343
4345
  value: function unload() {
4346
+ var _this3 = this;
4347
+
4344
4348
  clearTimeout(this._poller);
4345
4349
  var requests = this._event_queue.length > 0 ? this.formatQueue() : {};
4346
4350
  this._event_queue.length = 0;
4347
-
4348
- for (var key in requests) {
4349
- var _requests$key3 = requests[key],
4350
- _url2 = _requests$key3.url,
4351
- _data2 = _requests$key3.data,
4352
- _options = _requests$key3.options;
4353
- this.handlePollRequest(_url2, _data2, _objectSpread2(_objectSpread2({}, _options), {}, {
4351
+ var requestValues = Object.values(requests); // Always force events to be sent before recordings, as events are more important, and recordings are bigger and thus less likely to arrive
4352
+
4353
+ var sortedRequests = [].concat(_toConsumableArray(requestValues.filter(function (r) {
4354
+ return r.url.indexOf('/e') === 0;
4355
+ })), _toConsumableArray(requestValues.filter(function (r) {
4356
+ return r.url.indexOf('/e') !== 0;
4357
+ })));
4358
+ sortedRequests.map(function (_ref) {
4359
+ var url = _ref.url,
4360
+ data = _ref.data,
4361
+ options = _ref.options;
4362
+
4363
+ _this3.handlePollRequest(url, data, _objectSpread2(_objectSpread2({}, options), {}, {
4354
4364
  transport: 'sendBeacon'
4355
4365
  }));
4356
- }
4366
+ });
4357
4367
  }
4358
4368
  }, {
4359
4369
  key: "formatQueue",
@@ -5946,7 +5956,8 @@ var defaultConfig = function defaultConfig() {
5946
5956
  _capture_metrics: false,
5947
5957
  _capture_performance: false,
5948
5958
  name: 'posthog',
5949
- callback_fn: 'posthog._jsc'
5959
+ callback_fn: 'posthog._jsc',
5960
+ bootstrap: {}
5950
5961
  };
5951
5962
  };
5952
5963
  /**
@@ -6095,6 +6106,8 @@ var PostHog = /*#__PURE__*/function () {
6095
6106
  }, {
6096
6107
  key: "_init",
6097
6108
  value: function _init(token) {
6109
+ var _config$bootstrap;
6110
+
6098
6111
  var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6099
6112
  var name = arguments.length > 2 ? arguments[2] : undefined;
6100
6113
  this.__loaded = true;
@@ -6119,19 +6132,49 @@ var PostHog = /*#__PURE__*/function () {
6119
6132
 
6120
6133
  this._gdpr_init();
6121
6134
 
6135
+ if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6136
+ var _config$bootstrap2;
6137
+
6138
+ var uuid = this.get_config('get_device_id')(_UUID());
6139
+ var deviceID = (_config$bootstrap2 = config.bootstrap) !== null && _config$bootstrap2 !== void 0 && _config$bootstrap2.isIdentifiedID ? uuid : config.bootstrap.distinctID;
6140
+ this.register({
6141
+ distinct_id: config.bootstrap.distinctID,
6142
+ $device_id: deviceID
6143
+ });
6144
+ }
6145
+
6146
+ if (this._hasBootstrappedFeatureFlags()) {
6147
+ var _config$bootstrap3;
6148
+
6149
+ var activeFlags = Object.keys(((_config$bootstrap3 = config.bootstrap) === null || _config$bootstrap3 === void 0 ? void 0 : _config$bootstrap3.featureFlags) || {}).filter(function (flag) {
6150
+ var _config$bootstrap4, _config$bootstrap4$fe;
6151
+
6152
+ 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]);
6153
+ }).reduce(function (res, key) {
6154
+ var _config$bootstrap5, _config$bootstrap5$fe;
6155
+
6156
+ 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;
6157
+ }, {});
6158
+ this.featureFlags.receivedFeatureFlags({
6159
+ featureFlags: activeFlags
6160
+ });
6161
+ }
6162
+
6122
6163
  if (!this.get_distinct_id()) {
6123
6164
  // There is no need to set the distinct id
6124
6165
  // or the device id if something was already stored
6125
6166
  // in the persitence
6126
- var uuid = this.get_config('get_device_id')(_UUID());
6167
+ var _uuid = this.get_config('get_device_id')(_UUID());
6168
+
6127
6169
  this.register_once({
6128
- distinct_id: uuid,
6129
- $device_id: uuid
6170
+ distinct_id: _uuid,
6171
+ $device_id: _uuid
6130
6172
  }, '');
6131
- } // Set up the window close event handler "unload"
6173
+ } // Set up event handler for pageleave
6174
+ // Use `onpagehide` if available, see https://calendar.perfplanet.com/2020/beaconing-in-practice/#beaconing-reliability-avoiding-abandons
6132
6175
 
6133
6176
 
6134
- win.addEventListener && win.addEventListener('unload', this._handle_unload.bind(this));
6177
+ win.addEventListener && win.addEventListener('onpagehide' in self ? 'pagehide' : 'unload', this._handle_unload.bind(this));
6135
6178
  } // Private methods
6136
6179
 
6137
6180
  }, {
@@ -6406,6 +6449,13 @@ var PostHog = /*#__PURE__*/function () {
6406
6449
  execute(other_calls, this);
6407
6450
  execute(capturing_calls, this);
6408
6451
  }
6452
+ }, {
6453
+ key: "_hasBootstrappedFeatureFlags",
6454
+ value: function _hasBootstrappedFeatureFlags() {
6455
+ var _this$config$bootstra, _this$config$bootstra2;
6456
+
6457
+ 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;
6458
+ }
6409
6459
  /**
6410
6460
  * push() keeps the standard async-array-push
6411
6461
  * behavior around after the lib is loaded.