posthog-js 1.42.3 → 1.43.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.
package/dist/module.d.ts CHANGED
@@ -417,6 +417,7 @@ declare class PostHogPersistence {
417
417
  expire_days: number | undefined;
418
418
  default_expiry: number | undefined;
419
419
  cross_subdomain: boolean | undefined;
420
+ user_state: 'anonymous' | 'identified' | undefined;
420
421
  constructor(config: PostHogConfig);
421
422
  properties(): Properties;
422
423
  load(): void;
@@ -452,6 +453,8 @@ declare class PostHogPersistence {
452
453
  set_secure(secure: boolean): void;
453
454
  set_event_timer(event_name: string, timestamp: number): void;
454
455
  remove_event_timer(event_name: string): number;
456
+ get_user_state(): 'anonymous' | 'identified' | undefined;
457
+ set_user_state(state: 'anonymous' | 'identified'): void;
455
458
  }
456
459
 
457
460
  declare class PostHogFeatureFlags {
package/dist/module.js CHANGED
@@ -921,7 +921,7 @@ var LZString = {
921
921
  }
922
922
  };
923
923
 
924
- var version = "1.42.3";
924
+ var version = "1.43.1";
925
925
 
926
926
  // e.g. Config.DEBUG = Config.DEBUG || instance.get_config('debug')
927
927
 
@@ -3540,7 +3540,8 @@ var SESSION_RECORDING_ENABLED_SERVER_SIDE = '$session_recording_enabled_server_s
3540
3540
  var CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE = '$console_log_recording_enabled_server_side';
3541
3541
  var SESSION_ID = '$sesid';
3542
3542
  var ENABLED_FEATURE_FLAGS = '$enabled_feature_flags';
3543
- var RESERVED_PROPERTIES = [SET_QUEUE_KEY, SET_ONCE_QUEUE_KEY, UNSET_QUEUE_KEY, ADD_QUEUE_KEY, APPEND_QUEUE_KEY, REMOVE_QUEUE_KEY, UNION_QUEUE_KEY, PEOPLE_DISTINCT_ID_KEY, ALIAS_ID_KEY, CAMPAIGN_IDS_KEY, EVENT_TIMERS_KEY, SESSION_RECORDING_ENABLED_SERVER_SIDE, SESSION_ID, ENABLED_FEATURE_FLAGS];
3543
+ var USER_STATE = '$user_state';
3544
+ var RESERVED_PROPERTIES = [SET_QUEUE_KEY, SET_ONCE_QUEUE_KEY, UNSET_QUEUE_KEY, ADD_QUEUE_KEY, APPEND_QUEUE_KEY, REMOVE_QUEUE_KEY, UNION_QUEUE_KEY, PEOPLE_DISTINCT_ID_KEY, ALIAS_ID_KEY, CAMPAIGN_IDS_KEY, EVENT_TIMERS_KEY, SESSION_RECORDING_ENABLED_SERVER_SIDE, SESSION_ID, ENABLED_FEATURE_FLAGS, USER_STATE];
3544
3545
  /**
3545
3546
  * PostHog Persistence Object
3546
3547
  * @constructor
@@ -3584,6 +3585,7 @@ var PostHogPersistence = /*#__PURE__*/function () {
3584
3585
  this.storage = cookieStore;
3585
3586
  }
3586
3587
 
3588
+ this.user_state = undefined;
3587
3589
  this.load();
3588
3590
  this.update_config(config);
3589
3591
  this.save();
@@ -3829,6 +3831,17 @@ var PostHogPersistence = /*#__PURE__*/function () {
3829
3831
 
3830
3832
  return timestamp;
3831
3833
  }
3834
+ }, {
3835
+ key: "get_user_state",
3836
+ value: function get_user_state() {
3837
+ return this.props[USER_STATE];
3838
+ }
3839
+ }, {
3840
+ key: "set_user_state",
3841
+ value: function set_user_state(state) {
3842
+ this.props[USER_STATE] = state;
3843
+ this.save();
3844
+ }
3832
3845
  }]);
3833
3846
 
3834
3847
  return PostHogPersistence;
@@ -6661,16 +6674,18 @@ var PostHog = /*#__PURE__*/function () {
6661
6674
  this.register({
6662
6675
  distinct_id: config.segment.user().id()
6663
6676
  });
6677
+ this.persistence.set_user_state('identified');
6664
6678
  }
6665
6679
 
6666
6680
  config.segment.register(this.segmentIntegration());
6667
6681
  }
6668
6682
 
6669
6683
  if (((_config$bootstrap = config.bootstrap) === null || _config$bootstrap === void 0 ? void 0 : _config$bootstrap.distinctID) !== undefined) {
6670
- var _config$bootstrap2;
6684
+ var _config$bootstrap2, _config$bootstrap3;
6671
6685
 
6672
6686
  var uuid = this.get_config('get_device_id')(_UUID());
6673
6687
  var deviceID = (_config$bootstrap2 = config.bootstrap) !== null && _config$bootstrap2 !== void 0 && _config$bootstrap2.isIdentifiedID ? uuid : config.bootstrap.distinctID;
6688
+ this.persistence.set_user_state((_config$bootstrap3 = config.bootstrap) !== null && _config$bootstrap3 !== void 0 && _config$bootstrap3.isIdentifiedID ? 'identified' : 'anonymous');
6674
6689
  this.register({
6675
6690
  distinct_id: config.bootstrap.distinctID,
6676
6691
  $device_id: deviceID
@@ -6678,26 +6693,26 @@ var PostHog = /*#__PURE__*/function () {
6678
6693
  }
6679
6694
 
6680
6695
  if (this._hasBootstrappedFeatureFlags()) {
6681
- var _config$bootstrap3, _config$bootstrap6;
6696
+ var _config$bootstrap4, _config$bootstrap7;
6682
6697
 
6683
- var activeFlags = Object.keys(((_config$bootstrap3 = config.bootstrap) === null || _config$bootstrap3 === void 0 ? void 0 : _config$bootstrap3.featureFlags) || {}).filter(function (flag) {
6684
- var _config$bootstrap4, _config$bootstrap4$fe;
6698
+ var activeFlags = Object.keys(((_config$bootstrap4 = config.bootstrap) === null || _config$bootstrap4 === void 0 ? void 0 : _config$bootstrap4.featureFlags) || {}).filter(function (flag) {
6699
+ var _config$bootstrap5, _config$bootstrap5$fe;
6685
6700
 
6686
- 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]);
6701
+ return !!((_config$bootstrap5 = config.bootstrap) !== null && _config$bootstrap5 !== void 0 && (_config$bootstrap5$fe = _config$bootstrap5.featureFlags) !== null && _config$bootstrap5$fe !== void 0 && _config$bootstrap5$fe[flag]);
6687
6702
  }).reduce(function (res, key) {
6688
- var _config$bootstrap5, _config$bootstrap5$fe;
6703
+ var _config$bootstrap6, _config$bootstrap6$fe;
6689
6704
 
6690
- 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;
6705
+ return res[key] = ((_config$bootstrap6 = config.bootstrap) === null || _config$bootstrap6 === void 0 ? void 0 : (_config$bootstrap6$fe = _config$bootstrap6.featureFlags) === null || _config$bootstrap6$fe === void 0 ? void 0 : _config$bootstrap6$fe[key]) || false, res;
6691
6706
  }, {});
6692
- var featureFlagPayloads = Object.keys(((_config$bootstrap6 = config.bootstrap) === null || _config$bootstrap6 === void 0 ? void 0 : _config$bootstrap6.featureFlagPayloads) || {}).filter(function (key) {
6707
+ var featureFlagPayloads = Object.keys(((_config$bootstrap7 = config.bootstrap) === null || _config$bootstrap7 === void 0 ? void 0 : _config$bootstrap7.featureFlagPayloads) || {}).filter(function (key) {
6693
6708
  return activeFlags[key];
6694
6709
  }).reduce(function (res, key) {
6695
- var _config$bootstrap7, _config$bootstrap7$fe;
6710
+ var _config$bootstrap8, _config$bootstrap8$fe;
6696
6711
 
6697
- 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]) {
6698
- var _config$bootstrap8, _config$bootstrap8$fe;
6712
+ if ((_config$bootstrap8 = config.bootstrap) !== null && _config$bootstrap8 !== void 0 && (_config$bootstrap8$fe = _config$bootstrap8.featureFlagPayloads) !== null && _config$bootstrap8$fe !== void 0 && _config$bootstrap8$fe[key]) {
6713
+ var _config$bootstrap9, _config$bootstrap9$fe;
6699
6714
 
6700
- 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];
6715
+ res[key] = (_config$bootstrap9 = config.bootstrap) === null || _config$bootstrap9 === void 0 ? void 0 : (_config$bootstrap9$fe = _config$bootstrap9.featureFlagPayloads) === null || _config$bootstrap9$fe === void 0 ? void 0 : _config$bootstrap9$fe[key];
6701
6716
  }
6702
6717
 
6703
6718
  return res;
@@ -6717,7 +6732,9 @@ var PostHog = /*#__PURE__*/function () {
6717
6732
  this.register_once({
6718
6733
  distinct_id: _uuid,
6719
6734
  $device_id: _uuid
6720
- }, '');
6735
+ }, ''); // distinct id == $device_id is a proxy for anonymous user
6736
+
6737
+ this.persistence.set_user_state('anonymous');
6721
6738
  } // Set up event handler for pageleave
6722
6739
  // Use `onpagehide` if available, see https://calendar.perfplanet.com/2020/beaconing-in-practice/#beaconing-reliability-avoiding-abandons
6723
6740
 
@@ -7426,11 +7443,14 @@ var PostHog = /*#__PURE__*/function () {
7426
7443
  this.register({
7427
7444
  distinct_id: new_distinct_id
7428
7445
  });
7429
- } // send an $identify event any time the distinct_id is changing and the old ID is an anoymous ID
7430
- // - logic on the server will determine whether or not to do anything with it.
7446
+ }
7431
7447
 
7448
+ var deviceIdMarksForIdentify = !this.get_property('$device_id');
7449
+ var isKnownAnonymous = this.persistence.get_user_state() === 'anonymous'; // send an $identify event any time the distinct_id is changing and the old ID is an anoymous ID
7450
+ // - logic on the server will determine whether or not to do anything with it.
7432
7451
 
7433
- if (new_distinct_id !== previous_distinct_id && (!this.get_property('$device_id') || previous_distinct_id === this.get_property('$device_id'))) {
7452
+ if (new_distinct_id !== previous_distinct_id && (isKnownAnonymous || deviceIdMarksForIdentify)) {
7453
+ this.persistence.set_user_state('identified');
7434
7454
  this.capture('$identify', {
7435
7455
  distinct_id: new_distinct_id,
7436
7456
  $anon_distinct_id: previous_distinct_id
@@ -7518,6 +7538,7 @@ var PostHog = /*#__PURE__*/function () {
7518
7538
  value: function reset(reset_device_id) {
7519
7539
  var device_id = this.get_property('$device_id');
7520
7540
  this.persistence.clear();
7541
+ this.persistence.set_user_state('anonymous');
7521
7542
  this.sessionManager.resetSessionId();
7522
7543
  var uuid = this.get_config('get_device_id')(_UUID());
7523
7544
  this.register_once({