posthog-node 2.0.0-alpha8 → 2.0.0-alpha9

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/lib/index.esm.js CHANGED
@@ -131,10 +131,11 @@ function __generator(thisArg, body) {
131
131
  }
132
132
  }
133
133
 
134
- var version = "2.0.0-alpha8";
134
+ var version = "2.0.0-alpha9";
135
135
 
136
136
  var PostHogPersistedProperty;
137
137
  (function (PostHogPersistedProperty) {
138
+ PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
138
139
  PostHogPersistedProperty["DistinctId"] = "distinct_id";
139
140
  PostHogPersistedProperty["Props"] = "props";
140
141
  PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
@@ -689,7 +690,7 @@ var SimpleEventEmitter = /** @class */ (function () {
689
690
  var PostHogCore = /** @class */ (function () {
690
691
  function PostHogCore(apiKey, options) {
691
692
  var _this = this;
692
- var _a, _b, _c, _d, _e, _f;
693
+ var _a, _b, _c, _d, _e;
693
694
  this.flagCallReported = {};
694
695
  // internal
695
696
  this._events = new SimpleEventEmitter();
@@ -700,14 +701,13 @@ var PostHogCore = /** @class */ (function () {
700
701
  this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
701
702
  this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
702
703
  this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
703
- this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
704
704
  // If enable is explicitly set to false we override the optout
705
705
  this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
706
706
  this._retryOptions = {
707
- retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3,
708
- retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000,
707
+ retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
708
+ retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
709
709
  };
710
- this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes
710
+ this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes
711
711
  // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
712
712
  if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
713
713
  safeSetTimeout(function () {
@@ -759,7 +759,6 @@ var PostHogCore = /** @class */ (function () {
759
759
  for (var key in PostHogPersistedProperty) {
760
760
  this.setPersistedProperty(PostHogPersistedProperty[key], null);
761
761
  }
762
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
763
762
  };
764
763
  PostHogCore.prototype.debug = function (enabled) {
765
764
  var _a;
@@ -789,13 +788,16 @@ var PostHogCore = /** @class */ (function () {
789
788
  PostHogCore.prototype.resetSessionId = function () {
790
789
  this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
791
790
  };
792
- PostHogCore.prototype.getDistinctId = function () {
793
- var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
794
- if (!distinctId) {
795
- distinctId = generateUUID(globalThis);
796
- this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
791
+ PostHogCore.prototype.getAnonymousId = function () {
792
+ var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
793
+ if (!anonId) {
794
+ anonId = generateUUID(globalThis);
795
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
797
796
  }
798
- return distinctId;
797
+ return anonId;
798
+ };
799
+ PostHogCore.prototype.getDistinctId = function () {
800
+ return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
799
801
  };
800
802
  PostHogCore.prototype.register = function (properties) {
801
803
  this.props = __assign(__assign({}, this.props), properties);
@@ -809,27 +811,28 @@ var PostHogCore = /** @class */ (function () {
809
811
  *** TRACKING
810
812
  ***/
811
813
  PostHogCore.prototype.identify = function (distinctId, properties) {
812
- distinctId = distinctId || this.getDistinctId();
814
+ var previousDistinctId = this.getDistinctId();
815
+ distinctId = distinctId || previousDistinctId;
813
816
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
814
817
  this.groups(properties.$groups);
815
818
  }
816
819
  var payload = __assign(__assign({}, this.buildPayload({
817
820
  distinct_id: distinctId,
818
821
  event: '$identify',
819
- properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }),
822
+ properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
820
823
  })), { $set: properties });
821
- if (distinctId !== this.getDistinctId()) {
824
+ if (distinctId !== previousDistinctId) {
825
+ // We keep the AnonymousId to be used by decide calls and identify to link the previousId
826
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
822
827
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
828
+ if (this.getFeatureFlags()) {
829
+ void this.reloadFeatureFlagsAsync();
830
+ }
823
831
  }
824
832
  this.enqueue('identify', payload);
825
833
  return this;
826
834
  };
827
835
  PostHogCore.prototype.capture = function (event, properties) {
828
- // NOTE: Legacy nodejs implementation uses groups
829
- if (properties && properties['groups']) {
830
- properties.$groups = properties.groups;
831
- delete properties.groups;
832
- }
833
836
  if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
834
837
  this.groups(properties.$groups);
835
838
  }
@@ -868,7 +871,7 @@ var PostHogCore = /** @class */ (function () {
868
871
  this.register({
869
872
  $groups: __assign(__assign({}, existingGroups), groups),
870
873
  });
871
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) {
874
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
872
875
  void this.reloadFeatureFlagsAsync();
873
876
  }
874
877
  return this;
@@ -912,16 +915,24 @@ var PostHogCore = /** @class */ (function () {
912
915
  fetchOptions = {
913
916
  method: 'POST',
914
917
  headers: { 'Content-Type': 'application/json' },
915
- body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }),
918
+ body: JSON.stringify({
919
+ token: this.apiKey,
920
+ distinct_id: distinctId,
921
+ $anon_distinct_id: this.getAnonymousId(),
922
+ groups: groups,
923
+ }),
916
924
  };
917
925
  this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
918
926
  .then(function (r) { return r.json(); })
919
927
  .then(function (res) {
920
928
  if (res.featureFlags) {
921
929
  _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
930
+ _this._events.emit('featureflags', res.featureFlags);
922
931
  }
923
- _this._events.emit('featureflags', res.featureFlags);
924
932
  return res;
933
+ })
934
+ .finally(function () {
935
+ _this._decideResponsePromise = undefined;
925
936
  });
926
937
  return [2 /*return*/, this._decideResponsePromise];
927
938
  });
@@ -970,27 +981,16 @@ var PostHogCore = /** @class */ (function () {
970
981
  };
971
982
  PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
972
983
  return __awaiter(this, void 0, void 0, function () {
973
- var _this = this;
974
984
  return __generator(this, function (_a) {
975
985
  switch (_a.label) {
976
- case 0:
977
- clearTimeout(this._decideTimer);
978
- if (this._decidePollInterval) {
979
- this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
980
- }
981
- this._decideResponsePromise = undefined;
982
- return [4 /*yield*/, this.decideAsync()];
986
+ case 0: return [4 /*yield*/, this.decideAsync()];
983
987
  case 1: return [2 /*return*/, (_a.sent()).featureFlags];
984
988
  }
985
989
  });
986
990
  });
987
991
  };
988
- // When listening to feature flags polling is active
989
992
  PostHogCore.prototype.onFeatureFlags = function (cb) {
990
993
  var _this = this;
991
- if (!this._decideTimer) {
992
- void this.reloadFeatureFlagsAsync();
993
- }
994
994
  return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
995
995
  var flags;
996
996
  return __generator(this, function (_a) {
@@ -1002,6 +1002,19 @@ var PostHogCore = /** @class */ (function () {
1002
1002
  });
1003
1003
  }); });
1004
1004
  };
1005
+ PostHogCore.prototype.onFeatureFlag = function (key, cb) {
1006
+ var _this = this;
1007
+ return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
1008
+ var flagResponse;
1009
+ return __generator(this, function (_a) {
1010
+ flagResponse = this.getFeatureFlag(key);
1011
+ if (flagResponse !== undefined) {
1012
+ cb(flagResponse);
1013
+ }
1014
+ return [2 /*return*/];
1015
+ });
1016
+ }); });
1017
+ };
1005
1018
  PostHogCore.prototype.overrideFeatureFlag = function (flags) {
1006
1019
  if (flags === null) {
1007
1020
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
@@ -1109,7 +1122,6 @@ var PostHogCore = /** @class */ (function () {
1109
1122
  return __generator(this, function (_a) {
1110
1123
  switch (_a.label) {
1111
1124
  case 0:
1112
- clearTimeout(this._decideTimer);
1113
1125
  clearTimeout(this._flushTimer);
1114
1126
  return [4 /*yield*/, this.flushAsync()];
1115
1127
  case 1:
@@ -1199,8 +1211,6 @@ function () {
1199
1211
  options = {};
1200
1212
  }
1201
1213
 
1202
- options.decidePollInterval = 0; // Forcefully set to 0 so we don't auto-reload
1203
-
1204
1214
  this._sharedClient = new PostHog(apiKey, options);
1205
1215
  }
1206
1216