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.cjs.js +49 -39
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.esm.js +49 -39
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +2 -2
- package/lib/posthog-core/src/types.d.ts +1 -1
- package/lib/posthog-core/src/utils.d.ts +0 -1
- package/package.json +1 -1
- package/src/posthog-node.ts +0 -1
package/lib/index.cjs.js
CHANGED
|
@@ -139,10 +139,11 @@ function __generator(thisArg, body) {
|
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
var version = "2.0.0-
|
|
142
|
+
var version = "2.0.0-alpha9";
|
|
143
143
|
|
|
144
144
|
var PostHogPersistedProperty;
|
|
145
145
|
(function (PostHogPersistedProperty) {
|
|
146
|
+
PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
|
|
146
147
|
PostHogPersistedProperty["DistinctId"] = "distinct_id";
|
|
147
148
|
PostHogPersistedProperty["Props"] = "props";
|
|
148
149
|
PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
|
|
@@ -697,7 +698,7 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
697
698
|
var PostHogCore = /** @class */ (function () {
|
|
698
699
|
function PostHogCore(apiKey, options) {
|
|
699
700
|
var _this = this;
|
|
700
|
-
var _a, _b, _c, _d, _e
|
|
701
|
+
var _a, _b, _c, _d, _e;
|
|
701
702
|
this.flagCallReported = {};
|
|
702
703
|
// internal
|
|
703
704
|
this._events = new SimpleEventEmitter();
|
|
@@ -708,14 +709,13 @@ var PostHogCore = /** @class */ (function () {
|
|
|
708
709
|
this.flushInterval = (_a = options === null || options === void 0 ? void 0 : options.flushInterval) !== null && _a !== void 0 ? _a : 10000;
|
|
709
710
|
this.captureMode = (options === null || options === void 0 ? void 0 : options.captureMode) || 'form';
|
|
710
711
|
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
711
|
-
this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000);
|
|
712
712
|
// If enable is explicitly set to false we override the optout
|
|
713
713
|
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false;
|
|
714
714
|
this._retryOptions = {
|
|
715
|
-
retryCount: (
|
|
716
|
-
retryDelay: (
|
|
715
|
+
retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3,
|
|
716
|
+
retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000,
|
|
717
717
|
};
|
|
718
|
-
this._sessionExpirationTimeSeconds = (
|
|
718
|
+
this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes
|
|
719
719
|
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
720
720
|
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
721
721
|
safeSetTimeout(function () {
|
|
@@ -767,7 +767,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
767
767
|
for (var key in PostHogPersistedProperty) {
|
|
768
768
|
this.setPersistedProperty(PostHogPersistedProperty[key], null);
|
|
769
769
|
}
|
|
770
|
-
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis));
|
|
771
770
|
};
|
|
772
771
|
PostHogCore.prototype.debug = function (enabled) {
|
|
773
772
|
var _a;
|
|
@@ -797,13 +796,16 @@ var PostHogCore = /** @class */ (function () {
|
|
|
797
796
|
PostHogCore.prototype.resetSessionId = function () {
|
|
798
797
|
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
|
|
799
798
|
};
|
|
800
|
-
PostHogCore.prototype.
|
|
801
|
-
var
|
|
802
|
-
if (!
|
|
803
|
-
|
|
804
|
-
this.setPersistedProperty(PostHogPersistedProperty.
|
|
799
|
+
PostHogCore.prototype.getAnonymousId = function () {
|
|
800
|
+
var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
|
|
801
|
+
if (!anonId) {
|
|
802
|
+
anonId = generateUUID(globalThis);
|
|
803
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
|
|
805
804
|
}
|
|
806
|
-
return
|
|
805
|
+
return anonId;
|
|
806
|
+
};
|
|
807
|
+
PostHogCore.prototype.getDistinctId = function () {
|
|
808
|
+
return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
|
|
807
809
|
};
|
|
808
810
|
PostHogCore.prototype.register = function (properties) {
|
|
809
811
|
this.props = __assign(__assign({}, this.props), properties);
|
|
@@ -817,27 +819,28 @@ var PostHogCore = /** @class */ (function () {
|
|
|
817
819
|
*** TRACKING
|
|
818
820
|
***/
|
|
819
821
|
PostHogCore.prototype.identify = function (distinctId, properties) {
|
|
820
|
-
|
|
822
|
+
var previousDistinctId = this.getDistinctId();
|
|
823
|
+
distinctId = distinctId || previousDistinctId;
|
|
821
824
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
822
825
|
this.groups(properties.$groups);
|
|
823
826
|
}
|
|
824
827
|
var payload = __assign(__assign({}, this.buildPayload({
|
|
825
828
|
distinct_id: distinctId,
|
|
826
829
|
event: '$identify',
|
|
827
|
-
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.
|
|
830
|
+
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }),
|
|
828
831
|
})), { $set: properties });
|
|
829
|
-
if (distinctId !==
|
|
832
|
+
if (distinctId !== previousDistinctId) {
|
|
833
|
+
// We keep the AnonymousId to be used by decide calls and identify to link the previousId
|
|
834
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
830
835
|
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
836
|
+
if (this.getFeatureFlags()) {
|
|
837
|
+
void this.reloadFeatureFlagsAsync();
|
|
838
|
+
}
|
|
831
839
|
}
|
|
832
840
|
this.enqueue('identify', payload);
|
|
833
841
|
return this;
|
|
834
842
|
};
|
|
835
843
|
PostHogCore.prototype.capture = function (event, properties) {
|
|
836
|
-
// NOTE: Legacy nodejs implementation uses groups
|
|
837
|
-
if (properties && properties['groups']) {
|
|
838
|
-
properties.$groups = properties.groups;
|
|
839
|
-
delete properties.groups;
|
|
840
|
-
}
|
|
841
844
|
if (properties === null || properties === void 0 ? void 0 : properties.$groups) {
|
|
842
845
|
this.groups(properties.$groups);
|
|
843
846
|
}
|
|
@@ -876,7 +879,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
876
879
|
this.register({
|
|
877
880
|
$groups: __assign(__assign({}, existingGroups), groups),
|
|
878
881
|
});
|
|
879
|
-
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.
|
|
882
|
+
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
|
|
880
883
|
void this.reloadFeatureFlagsAsync();
|
|
881
884
|
}
|
|
882
885
|
return this;
|
|
@@ -920,16 +923,24 @@ var PostHogCore = /** @class */ (function () {
|
|
|
920
923
|
fetchOptions = {
|
|
921
924
|
method: 'POST',
|
|
922
925
|
headers: { 'Content-Type': 'application/json' },
|
|
923
|
-
body: JSON.stringify({
|
|
926
|
+
body: JSON.stringify({
|
|
927
|
+
token: this.apiKey,
|
|
928
|
+
distinct_id: distinctId,
|
|
929
|
+
$anon_distinct_id: this.getAnonymousId(),
|
|
930
|
+
groups: groups,
|
|
931
|
+
}),
|
|
924
932
|
};
|
|
925
933
|
this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions)
|
|
926
934
|
.then(function (r) { return r.json(); })
|
|
927
935
|
.then(function (res) {
|
|
928
936
|
if (res.featureFlags) {
|
|
929
937
|
_this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
|
|
938
|
+
_this._events.emit('featureflags', res.featureFlags);
|
|
930
939
|
}
|
|
931
|
-
_this._events.emit('featureflags', res.featureFlags);
|
|
932
940
|
return res;
|
|
941
|
+
})
|
|
942
|
+
.finally(function () {
|
|
943
|
+
_this._decideResponsePromise = undefined;
|
|
933
944
|
});
|
|
934
945
|
return [2 /*return*/, this._decideResponsePromise];
|
|
935
946
|
});
|
|
@@ -978,27 +989,16 @@ var PostHogCore = /** @class */ (function () {
|
|
|
978
989
|
};
|
|
979
990
|
PostHogCore.prototype.reloadFeatureFlagsAsync = function () {
|
|
980
991
|
return __awaiter(this, void 0, void 0, function () {
|
|
981
|
-
var _this = this;
|
|
982
992
|
return __generator(this, function (_a) {
|
|
983
993
|
switch (_a.label) {
|
|
984
|
-
case 0:
|
|
985
|
-
clearTimeout(this._decideTimer);
|
|
986
|
-
if (this._decidePollInterval) {
|
|
987
|
-
this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval);
|
|
988
|
-
}
|
|
989
|
-
this._decideResponsePromise = undefined;
|
|
990
|
-
return [4 /*yield*/, this.decideAsync()];
|
|
994
|
+
case 0: return [4 /*yield*/, this.decideAsync()];
|
|
991
995
|
case 1: return [2 /*return*/, (_a.sent()).featureFlags];
|
|
992
996
|
}
|
|
993
997
|
});
|
|
994
998
|
});
|
|
995
999
|
};
|
|
996
|
-
// When listening to feature flags polling is active
|
|
997
1000
|
PostHogCore.prototype.onFeatureFlags = function (cb) {
|
|
998
1001
|
var _this = this;
|
|
999
|
-
if (!this._decideTimer) {
|
|
1000
|
-
void this.reloadFeatureFlagsAsync();
|
|
1001
|
-
}
|
|
1002
1002
|
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1003
1003
|
var flags;
|
|
1004
1004
|
return __generator(this, function (_a) {
|
|
@@ -1010,6 +1010,19 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1010
1010
|
});
|
|
1011
1011
|
}); });
|
|
1012
1012
|
};
|
|
1013
|
+
PostHogCore.prototype.onFeatureFlag = function (key, cb) {
|
|
1014
|
+
var _this = this;
|
|
1015
|
+
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1016
|
+
var flagResponse;
|
|
1017
|
+
return __generator(this, function (_a) {
|
|
1018
|
+
flagResponse = this.getFeatureFlag(key);
|
|
1019
|
+
if (flagResponse !== undefined) {
|
|
1020
|
+
cb(flagResponse);
|
|
1021
|
+
}
|
|
1022
|
+
return [2 /*return*/];
|
|
1023
|
+
});
|
|
1024
|
+
}); });
|
|
1025
|
+
};
|
|
1013
1026
|
PostHogCore.prototype.overrideFeatureFlag = function (flags) {
|
|
1014
1027
|
if (flags === null) {
|
|
1015
1028
|
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
|
|
@@ -1117,7 +1130,6 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1117
1130
|
return __generator(this, function (_a) {
|
|
1118
1131
|
switch (_a.label) {
|
|
1119
1132
|
case 0:
|
|
1120
|
-
clearTimeout(this._decideTimer);
|
|
1121
1133
|
clearTimeout(this._flushTimer);
|
|
1122
1134
|
return [4 /*yield*/, this.flushAsync()];
|
|
1123
1135
|
case 1:
|
|
@@ -1207,8 +1219,6 @@ function () {
|
|
|
1207
1219
|
options = {};
|
|
1208
1220
|
}
|
|
1209
1221
|
|
|
1210
|
-
options.decidePollInterval = 0; // Forcefully set to 0 so we don't auto-reload
|
|
1211
|
-
|
|
1212
1222
|
this._sharedClient = new PostHog(apiKey, options);
|
|
1213
1223
|
}
|
|
1214
1224
|
|