posthog-node 2.0.0-alpha3 → 2.0.0-alpha6

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.d.ts CHANGED
@@ -96,7 +96,9 @@ declare type PostHogNodeV1 = {
96
96
  shutdown(): void;
97
97
  };
98
98
 
99
- declare type PostHogOptions = PosthogCoreOptions;
99
+ declare type PostHogOptions = PosthogCoreOptions & {
100
+ persistence?: 'memory';
101
+ };
100
102
  declare class PostHogGlobal implements PostHogNodeV1 {
101
103
  private _sharedClient;
102
104
  constructor(apiKey: string, options?: PostHogOptions);
package/lib/index.esm.js CHANGED
@@ -131,7 +131,7 @@ function __generator(thisArg, body) {
131
131
  }
132
132
  }
133
133
 
134
- var version = "2.0.0-alpha3";
134
+ var version = "2.0.0-alpha6";
135
135
 
136
136
  var PostHogPersistedProperty;
137
137
  (function (PostHogPersistedProperty) {
@@ -674,13 +674,14 @@ var SimpleEventEmitter = /** @class */ (function () {
674
674
  };
675
675
  };
676
676
  SimpleEventEmitter.prototype.emit = function (event, payload) {
677
- if (!this.events[event]) {
678
- return;
679
- }
680
- for (var _i = 0, _a = this.events[event]; _i < _a.length; _i++) {
677
+ for (var _i = 0, _a = this.events[event] || []; _i < _a.length; _i++) {
681
678
  var listener = _a[_i];
682
679
  listener(payload);
683
680
  }
681
+ for (var _b = 0, _c = this.events['*'] || []; _b < _c.length; _b++) {
682
+ var listener = _c[_b];
683
+ listener(event, payload);
684
+ }
684
685
  };
685
686
  return SimpleEventEmitter;
686
687
  }());
@@ -760,6 +761,14 @@ var PostHogCore = /** @class */ (function () {
760
761
  }
761
762
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
762
763
  };
764
+ PostHogCore.prototype.debug = function (enabled) {
765
+ var _a;
766
+ if (enabled === void 0) { enabled = true; }
767
+ (_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
768
+ if (enabled) {
769
+ this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
770
+ }
771
+ };
763
772
  PostHogCore.prototype.buildPayload = function (payload) {
764
773
  return {
765
774
  distinct_id: payload.distinct_id || this.getDistinctId(),
@@ -777,6 +786,9 @@ var PostHogCore = /** @class */ (function () {
777
786
  this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, Date.now());
778
787
  return sessionId;
779
788
  };
789
+ PostHogCore.prototype.resetSessionId = function () {
790
+ this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
791
+ };
780
792
  PostHogCore.prototype.getDistinctId = function () {
781
793
  var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
782
794
  if (!distinctId) {
@@ -1113,6 +1125,19 @@ var PostHogCore = /** @class */ (function () {
1113
1125
  return PostHogCore;
1114
1126
  }());
1115
1127
 
1128
+ var PostHogMemoryStorage = /** @class */ (function () {
1129
+ function PostHogMemoryStorage() {
1130
+ this._memoryStorage = {};
1131
+ }
1132
+ PostHogMemoryStorage.prototype.getProperty = function (key) {
1133
+ return this._memoryStorage[key];
1134
+ };
1135
+ PostHogMemoryStorage.prototype.setProperty = function (key, value) {
1136
+ this._memoryStorage[key] = value !== null ? value : undefined;
1137
+ };
1138
+ return PostHogMemoryStorage;
1139
+ }());
1140
+
1116
1141
  var PostHog =
1117
1142
  /** @class */
1118
1143
  function (_super) {
@@ -1129,16 +1154,16 @@ function (_super) {
1129
1154
  options.preloadFeatureFlags = false; // Don't preload as this makes no sense without a distinctId
1130
1155
 
1131
1156
  _this = _super.call(this, apiKey, options) || this;
1132
- _this._memoryStorage = {};
1157
+ _this._memoryStorage = new PostHogMemoryStorage();
1133
1158
  return _this;
1134
1159
  }
1135
1160
 
1136
1161
  PostHog.prototype.getPersistedProperty = function (key) {
1137
- return this._memoryStorage[key];
1162
+ return this._memoryStorage.getProperty(key);
1138
1163
  };
1139
1164
 
1140
1165
  PostHog.prototype.setPersistedProperty = function (key, value) {
1141
- this._memoryStorage[key] = value !== null ? value : undefined;
1166
+ return this._memoryStorage.setProperty(key, value);
1142
1167
  };
1143
1168
 
1144
1169
  PostHog.prototype.getSessionId = function () {
@@ -1180,12 +1205,14 @@ function () {
1180
1205
  }
1181
1206
 
1182
1207
  PostHogGlobal.prototype.reInit = function (distinctId) {
1183
- // Remove all state except the queue
1184
- var queue = this._sharedClient.getPersistedProperty(PostHogPersistedProperty.Queue);
1185
-
1186
- this._sharedClient.reset();
1208
+ // Certain properties we want to persist
1209
+ var propertiesToKeep = [PostHogPersistedProperty.Queue, PostHogPersistedProperty.OptedOut];
1187
1210
 
1188
- this._sharedClient.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
1211
+ for (var key in PostHogPersistedProperty) {
1212
+ if (!propertiesToKeep.includes(key)) {
1213
+ this._sharedClient.setPersistedProperty(PostHogPersistedProperty[key], null);
1214
+ }
1215
+ }
1189
1216
 
1190
1217
  this._sharedClient.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1191
1218
  };