posthog-js-lite 2.0.0-alpha2 → 2.0.0-alpha5

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
@@ -71,10 +71,10 @@ interface RetriableOptions {
71
71
 
72
72
  declare class SimpleEventEmitter {
73
73
  events: {
74
- [key: string]: ((e: any) => void)[];
74
+ [key: string]: ((...args: any[]) => void)[];
75
75
  };
76
76
  constructor();
77
- on(event: string, listener: (e: any) => void): () => void;
77
+ on(event: string, listener: (...args: any[]) => void): () => void;
78
78
  emit(event: string, payload: any): void;
79
79
  }
80
80
 
@@ -86,6 +86,7 @@ declare abstract class PostHogCore {
86
86
  private captureMode;
87
87
  private sendFeatureFlagEvent;
88
88
  private flagCallReported;
89
+ private removeDebugCallback?;
89
90
  protected _events: SimpleEventEmitter;
90
91
  protected _flushTimer?: any;
91
92
  protected _decideResponsePromise?: Promise<PostHogDecideResponse>;
@@ -108,10 +109,12 @@ declare abstract class PostHogCore {
108
109
  get optedOut(): boolean;
109
110
  optIn(): void;
110
111
  optOut(): void;
111
- on(event: string, cb: (e: any) => void): () => void;
112
+ on(event: string, cb: (...args: any[]) => void): () => void;
112
113
  reset(): void;
114
+ debug(enabled?: boolean): void;
113
115
  private buildPayload;
114
116
  getSessionId(): string | undefined;
117
+ resetSessionId(): void;
115
118
  getDistinctId(): string;
116
119
  register(properties: {
117
120
  [key: string]: any;
@@ -156,10 +159,12 @@ declare abstract class PostHogCore {
156
159
  shutdown(): void;
157
160
  }
158
161
 
159
- interface PostHogOptions extends PosthogCoreOptions {
162
+ declare type PostHogOptions = {
160
163
  autocapture?: boolean;
164
+ persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory';
161
165
  persistence_name?: string;
162
- }
166
+ } & PosthogCoreOptions;
167
+
163
168
  declare class PostHog extends PostHogCore {
164
169
  private _storage;
165
170
  private _storageCache;
@@ -174,4 +179,4 @@ declare class PostHog extends PostHogCore {
174
179
  getCommonEventProperties(): any;
175
180
  }
176
181
 
177
- export { PostHog, PostHogOptions, PostHog as default };
182
+ export { PostHog, PostHog as default };
package/lib/index.esm.js CHANGED
@@ -156,7 +156,7 @@ function retriable(fn, props) {
156
156
  return __generator(this, function (_d) {
157
157
  switch (_d.label) {
158
158
  case 0:
159
- _a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ? 1000 : _b, _c = props.retryCheck, retryCheck = _c === void 0 ? function () { return true; } : _c;
159
+ _a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ? 5000 : _b, _c = props.retryCheck, retryCheck = _c === void 0 ? function () { return true; } : _c;
160
160
  lastError = null;
161
161
  i = 0;
162
162
  _d.label = 1;
@@ -670,13 +670,14 @@ var SimpleEventEmitter = /** @class */ (function () {
670
670
  };
671
671
  };
672
672
  SimpleEventEmitter.prototype.emit = function (event, payload) {
673
- if (!this.events[event]) {
674
- return;
675
- }
676
- for (var _i = 0, _a = this.events[event]; _i < _a.length; _i++) {
673
+ for (var _i = 0, _a = this.events[event] || []; _i < _a.length; _i++) {
677
674
  var listener = _a[_i];
678
675
  listener(payload);
679
676
  }
677
+ for (var _b = 0, _c = this.events['*'] || []; _b < _c.length; _b++) {
678
+ var listener = _c[_b];
679
+ listener(event, payload);
680
+ }
680
681
  };
681
682
  return SimpleEventEmitter;
682
683
  }());
@@ -756,6 +757,14 @@ var PostHogCore = /** @class */ (function () {
756
757
  }
757
758
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
758
759
  };
760
+ PostHogCore.prototype.debug = function (enabled) {
761
+ var _a;
762
+ if (enabled === void 0) { enabled = true; }
763
+ (_a = this.removeDebugCallback) === null || _a === void 0 ? void 0 : _a.call(this);
764
+ if (enabled) {
765
+ this.removeDebugCallback = this.on('*', function (event, payload) { return console.log('PostHog Debug', event, payload); });
766
+ }
767
+ };
759
768
  PostHogCore.prototype.buildPayload = function (payload) {
760
769
  return {
761
770
  distinct_id: payload.distinct_id || this.getDistinctId(),
@@ -773,6 +782,9 @@ var PostHogCore = /** @class */ (function () {
773
782
  this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, Date.now());
774
783
  return sessionId;
775
784
  };
785
+ PostHogCore.prototype.resetSessionId = function () {
786
+ this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
787
+ };
776
788
  PostHogCore.prototype.getDistinctId = function () {
777
789
  var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
778
790
  if (!distinctId) {
@@ -1109,9 +1121,8 @@ var PostHogCore = /** @class */ (function () {
1109
1121
  return PostHogCore;
1110
1122
  }());
1111
1123
 
1112
- // TODO: Get this from package.json
1124
+ var version = "2.0.0-alpha5";
1113
1125
 
1114
- var version$1 = '2.0.0-alpha';
1115
1126
  function getContext(window) {
1116
1127
  var context = {};
1117
1128
 
@@ -1135,7 +1146,7 @@ function getContext(window) {
1135
1146
 
1136
1147
  context = __assign(__assign({}, context), {
1137
1148
  $lib: 'js',
1138
- $lib_version: version$1,
1149
+ $lib_version: version,
1139
1150
  $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
1140
1151
  $time: currentTimestamp() / 1000
1141
1152
  });
@@ -1311,7 +1322,7 @@ var cookieStore = {
1311
1322
  var cdomain = '',
1312
1323
  expires = '',
1313
1324
  secure = '';
1314
- var new_cookie_val = key + '=' + encodeURIComponent(JSON.stringify(value)) + expires + '; path=/' + cdomain + secure;
1325
+ var new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure;
1315
1326
  document.cookie = new_cookie_val;
1316
1327
  } catch (err) {
1317
1328
  return;
@@ -1351,7 +1362,7 @@ var createStorageLike = function (store) {
1351
1362
  return store.getItem(key);
1352
1363
  },
1353
1364
  setItem: function (key, value) {
1354
- store.setItem(key, JSON.stringify(value));
1365
+ store.setItem(key, value);
1355
1366
  },
1356
1367
  removeItem: function (key) {
1357
1368
  store.removeItem(key);
@@ -1401,7 +1412,54 @@ var checkStoreIsSupported = function (storage, key) {
1401
1412
  var localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined;
1402
1413
  var sessionStorage = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined;
1403
1414
 
1404
- var version = "2.0.0-alpha2";
1415
+ var createMemoryStorage = function () {
1416
+ var _cache = {};
1417
+ var store = {
1418
+ getItem: function (key) {
1419
+ return _cache[key];
1420
+ },
1421
+ setItem: function (key, value) {
1422
+ _cache[key] = value !== null ? value : undefined;
1423
+ },
1424
+ removeItem: function (key) {
1425
+ delete _cache[key];
1426
+ },
1427
+ clear: function () {
1428
+ for (var key in _cache) {
1429
+ delete _cache[key];
1430
+ }
1431
+ },
1432
+ getAllKeys: function () {
1433
+ var keys = [];
1434
+
1435
+ for (var key in _cache) {
1436
+ keys.push(key);
1437
+ }
1438
+
1439
+ return keys;
1440
+ }
1441
+ };
1442
+ return store;
1443
+ };
1444
+
1445
+ var getStorage = function (type) {
1446
+ switch (type) {
1447
+ case 'cookie':
1448
+ return cookieStore || localStore || sessionStorage || createMemoryStorage();
1449
+
1450
+ case 'localStorage':
1451
+ return localStore || sessionStorage || createMemoryStorage();
1452
+
1453
+ case 'sessionStorage':
1454
+ return sessionStorage || createMemoryStorage();
1455
+
1456
+ case 'memory':
1457
+ return createMemoryStorage();
1458
+
1459
+ default:
1460
+ return createMemoryStorage();
1461
+ }
1462
+ };
1405
1463
 
1406
1464
  var PostHog =
1407
1465
  /** @class */
@@ -1409,11 +1467,11 @@ function (_super) {
1409
1467
  __extends(PostHog, _super);
1410
1468
 
1411
1469
  function PostHog(apiKey, options) {
1412
- var _this = _super.call(this, apiKey, options) || this;
1470
+ var _this = _super.call(this, apiKey, options) || this; // posthog-js stores options in one object on
1413
1471
 
1414
- _this._storage = localStore || sessionStorage || cookieStore; // posthog-js stores options in one object on
1415
1472
 
1416
1473
  _this._storageKey = (options === null || options === void 0 ? void 0 : options.persistence_name) ? "ph_".concat(options.persistence_name) : "ph_".concat(apiKey, "_posthog");
1474
+ _this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage');
1417
1475
  return _this;
1418
1476
  }
1419
1477
 
@@ -1432,13 +1490,11 @@ function (_super) {
1432
1490
 
1433
1491
  if (value === null) {
1434
1492
  delete this._storageCache[key];
1435
-
1436
- this._storage.removeItem(this._storageKey);
1437
1493
  } else {
1438
1494
  this._storageCache[key] = value;
1439
-
1440
- this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
1441
1495
  }
1496
+
1497
+ this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
1442
1498
  };
1443
1499
 
1444
1500
  PostHog.prototype.fetch = function (url, options) {
@@ -1446,7 +1502,7 @@ function (_super) {
1446
1502
  };
1447
1503
 
1448
1504
  PostHog.prototype.getLibraryId = function () {
1449
- return 'posthog-web';
1505
+ return 'posthog-js-lite';
1450
1506
  };
1451
1507
 
1452
1508
  PostHog.prototype.getLibraryVersion = function () {