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.cjs.js +74 -18
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +11 -6
- package/lib/index.esm.js +74 -18
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/eventemitter.d.ts +2 -2
- package/lib/posthog-core/src/index.d.ts +4 -1
- package/lib/posthog-web/src/posthog-web.d.ts +2 -5
- package/lib/posthog-web/src/storage.d.ts +2 -0
- package/lib/posthog-web/src/types.d.ts +6 -0
- package/package.json +6 -2
- package/src/context.ts +1 -4
- package/src/posthog-web.ts +7 -11
- package/src/storage.ts +50 -3
- package/src/types.ts +7 -0
package/lib/index.cjs.js
CHANGED
|
@@ -160,7 +160,7 @@ function retriable(fn, props) {
|
|
|
160
160
|
return __generator(this, function (_d) {
|
|
161
161
|
switch (_d.label) {
|
|
162
162
|
case 0:
|
|
163
|
-
_a = props.retryCount, retryCount = _a === void 0 ? 3 : _a, _b = props.retryDelay, retryDelay = _b === void 0 ?
|
|
163
|
+
_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;
|
|
164
164
|
lastError = null;
|
|
165
165
|
i = 0;
|
|
166
166
|
_d.label = 1;
|
|
@@ -674,13 +674,14 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
674
674
|
};
|
|
675
675
|
};
|
|
676
676
|
SimpleEventEmitter.prototype.emit = function (event, payload) {
|
|
677
|
-
|
|
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,9 +1125,8 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1113
1125
|
return PostHogCore;
|
|
1114
1126
|
}());
|
|
1115
1127
|
|
|
1116
|
-
|
|
1128
|
+
var version = "2.0.0-alpha5";
|
|
1117
1129
|
|
|
1118
|
-
var version$1 = '2.0.0-alpha';
|
|
1119
1130
|
function getContext(window) {
|
|
1120
1131
|
var context = {};
|
|
1121
1132
|
|
|
@@ -1139,7 +1150,7 @@ function getContext(window) {
|
|
|
1139
1150
|
|
|
1140
1151
|
context = __assign(__assign({}, context), {
|
|
1141
1152
|
$lib: 'js',
|
|
1142
|
-
$lib_version: version
|
|
1153
|
+
$lib_version: version,
|
|
1143
1154
|
$insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
|
|
1144
1155
|
$time: currentTimestamp() / 1000
|
|
1145
1156
|
});
|
|
@@ -1315,7 +1326,7 @@ var cookieStore = {
|
|
|
1315
1326
|
var cdomain = '',
|
|
1316
1327
|
expires = '',
|
|
1317
1328
|
secure = '';
|
|
1318
|
-
var new_cookie_val = key + '=' + encodeURIComponent(
|
|
1329
|
+
var new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure;
|
|
1319
1330
|
document.cookie = new_cookie_val;
|
|
1320
1331
|
} catch (err) {
|
|
1321
1332
|
return;
|
|
@@ -1355,7 +1366,7 @@ var createStorageLike = function (store) {
|
|
|
1355
1366
|
return store.getItem(key);
|
|
1356
1367
|
},
|
|
1357
1368
|
setItem: function (key, value) {
|
|
1358
|
-
store.setItem(key,
|
|
1369
|
+
store.setItem(key, value);
|
|
1359
1370
|
},
|
|
1360
1371
|
removeItem: function (key) {
|
|
1361
1372
|
store.removeItem(key);
|
|
@@ -1405,7 +1416,54 @@ var checkStoreIsSupported = function (storage, key) {
|
|
|
1405
1416
|
var localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined;
|
|
1406
1417
|
var sessionStorage = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined;
|
|
1407
1418
|
|
|
1408
|
-
var
|
|
1419
|
+
var createMemoryStorage = function () {
|
|
1420
|
+
var _cache = {};
|
|
1421
|
+
var store = {
|
|
1422
|
+
getItem: function (key) {
|
|
1423
|
+
return _cache[key];
|
|
1424
|
+
},
|
|
1425
|
+
setItem: function (key, value) {
|
|
1426
|
+
_cache[key] = value !== null ? value : undefined;
|
|
1427
|
+
},
|
|
1428
|
+
removeItem: function (key) {
|
|
1429
|
+
delete _cache[key];
|
|
1430
|
+
},
|
|
1431
|
+
clear: function () {
|
|
1432
|
+
for (var key in _cache) {
|
|
1433
|
+
delete _cache[key];
|
|
1434
|
+
}
|
|
1435
|
+
},
|
|
1436
|
+
getAllKeys: function () {
|
|
1437
|
+
var keys = [];
|
|
1438
|
+
|
|
1439
|
+
for (var key in _cache) {
|
|
1440
|
+
keys.push(key);
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
return keys;
|
|
1444
|
+
}
|
|
1445
|
+
};
|
|
1446
|
+
return store;
|
|
1447
|
+
};
|
|
1448
|
+
|
|
1449
|
+
var getStorage = function (type) {
|
|
1450
|
+
switch (type) {
|
|
1451
|
+
case 'cookie':
|
|
1452
|
+
return cookieStore || localStore || sessionStorage || createMemoryStorage();
|
|
1453
|
+
|
|
1454
|
+
case 'localStorage':
|
|
1455
|
+
return localStore || sessionStorage || createMemoryStorage();
|
|
1456
|
+
|
|
1457
|
+
case 'sessionStorage':
|
|
1458
|
+
return sessionStorage || createMemoryStorage();
|
|
1459
|
+
|
|
1460
|
+
case 'memory':
|
|
1461
|
+
return createMemoryStorage();
|
|
1462
|
+
|
|
1463
|
+
default:
|
|
1464
|
+
return createMemoryStorage();
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1409
1467
|
|
|
1410
1468
|
var PostHog =
|
|
1411
1469
|
/** @class */
|
|
@@ -1413,11 +1471,11 @@ function (_super) {
|
|
|
1413
1471
|
__extends(PostHog, _super);
|
|
1414
1472
|
|
|
1415
1473
|
function PostHog(apiKey, options) {
|
|
1416
|
-
var _this = _super.call(this, apiKey, options) || this;
|
|
1474
|
+
var _this = _super.call(this, apiKey, options) || this; // posthog-js stores options in one object on
|
|
1417
1475
|
|
|
1418
|
-
_this._storage = localStore || sessionStorage || cookieStore; // posthog-js stores options in one object on
|
|
1419
1476
|
|
|
1420
1477
|
_this._storageKey = (options === null || options === void 0 ? void 0 : options.persistence_name) ? "ph_".concat(options.persistence_name) : "ph_".concat(apiKey, "_posthog");
|
|
1478
|
+
_this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage');
|
|
1421
1479
|
return _this;
|
|
1422
1480
|
}
|
|
1423
1481
|
|
|
@@ -1436,13 +1494,11 @@ function (_super) {
|
|
|
1436
1494
|
|
|
1437
1495
|
if (value === null) {
|
|
1438
1496
|
delete this._storageCache[key];
|
|
1439
|
-
|
|
1440
|
-
this._storage.removeItem(this._storageKey);
|
|
1441
1497
|
} else {
|
|
1442
1498
|
this._storageCache[key] = value;
|
|
1443
|
-
|
|
1444
|
-
this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
|
|
1445
1499
|
}
|
|
1500
|
+
|
|
1501
|
+
this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
|
|
1446
1502
|
};
|
|
1447
1503
|
|
|
1448
1504
|
PostHog.prototype.fetch = function (url, options) {
|
|
@@ -1450,7 +1506,7 @@ function (_super) {
|
|
|
1450
1506
|
};
|
|
1451
1507
|
|
|
1452
1508
|
PostHog.prototype.getLibraryId = function () {
|
|
1453
|
-
return 'posthog-
|
|
1509
|
+
return 'posthog-js-lite';
|
|
1454
1510
|
};
|
|
1455
1511
|
|
|
1456
1512
|
PostHog.prototype.getLibraryVersion = function () {
|