posthog-node
Advanced tools
Comparing version 2.0.0-alpha8 to 2.0.0-alpha9
@@ -142,6 +142,7 @@ 'use strict'; | ||
var version = "2.0.0-alpha8"; | ||
var version = "2.0.0-alpha9"; | ||
var PostHogPersistedProperty; | ||
(function (PostHogPersistedProperty) { | ||
PostHogPersistedProperty["AnonymousId"] = "anonymous_id"; | ||
PostHogPersistedProperty["DistinctId"] = "distinct_id"; | ||
@@ -701,3 +702,3 @@ PostHogPersistedProperty["Props"] = "props"; | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f; | ||
var _a, _b, _c, _d, _e; | ||
this.flagCallReported = {}; | ||
@@ -713,10 +714,9 @@ // internal | ||
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true; | ||
this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000); | ||
// If enable is explicitly set to false we override the optout | ||
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false; | ||
this._retryOptions = { | ||
retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3, | ||
retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000, | ||
retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3, | ||
retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000, | ||
}; | ||
this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes | ||
this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes | ||
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent | ||
@@ -773,3 +773,2 @@ if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) { | ||
} | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis)); | ||
}; | ||
@@ -804,10 +803,13 @@ PostHogCore.prototype.debug = function (enabled) { | ||
}; | ||
PostHogCore.prototype.getDistinctId = function () { | ||
var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId); | ||
if (!distinctId) { | ||
distinctId = generateUUID(globalThis); | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId); | ||
PostHogCore.prototype.getAnonymousId = function () { | ||
var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId); | ||
if (!anonId) { | ||
anonId = generateUUID(globalThis); | ||
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId); | ||
} | ||
return distinctId; | ||
return anonId; | ||
}; | ||
PostHogCore.prototype.getDistinctId = function () { | ||
return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId(); | ||
}; | ||
PostHogCore.prototype.register = function (properties) { | ||
@@ -825,3 +827,4 @@ this.props = __assign(__assign({}, this.props), properties); | ||
PostHogCore.prototype.identify = function (distinctId, properties) { | ||
distinctId = distinctId || this.getDistinctId(); | ||
var previousDistinctId = this.getDistinctId(); | ||
distinctId = distinctId || previousDistinctId; | ||
if (properties === null || properties === void 0 ? void 0 : properties.$groups) { | ||
@@ -833,6 +836,11 @@ this.groups(properties.$groups); | ||
event: '$identify', | ||
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }), | ||
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }), | ||
})), { $set: properties }); | ||
if (distinctId !== this.getDistinctId()) { | ||
if (distinctId !== previousDistinctId) { | ||
// We keep the AnonymousId to be used by decide calls and identify to link the previousId | ||
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId); | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId); | ||
if (this.getFeatureFlags()) { | ||
void this.reloadFeatureFlagsAsync(); | ||
} | ||
} | ||
@@ -843,7 +851,2 @@ this.enqueue('identify', payload); | ||
PostHogCore.prototype.capture = function (event, properties) { | ||
// NOTE: Legacy nodejs implementation uses groups | ||
if (properties && properties['groups']) { | ||
properties.$groups = properties.groups; | ||
delete properties.groups; | ||
} | ||
if (properties === null || properties === void 0 ? void 0 : properties.$groups) { | ||
@@ -887,3 +890,3 @@ this.groups(properties.$groups); | ||
}); | ||
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) { | ||
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) { | ||
void this.reloadFeatureFlagsAsync(); | ||
@@ -932,3 +935,8 @@ } | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }), | ||
body: JSON.stringify({ | ||
token: this.apiKey, | ||
distinct_id: distinctId, | ||
$anon_distinct_id: this.getAnonymousId(), | ||
groups: groups, | ||
}), | ||
}; | ||
@@ -940,5 +948,8 @@ this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions) | ||
_this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags); | ||
_this._events.emit('featureflags', res.featureFlags); | ||
} | ||
_this._events.emit('featureflags', res.featureFlags); | ||
return res; | ||
}) | ||
.finally(function () { | ||
_this._decideResponsePromise = undefined; | ||
}); | ||
@@ -992,12 +1003,5 @@ return [2 /*return*/, this._decideResponsePromise]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
clearTimeout(this._decideTimer); | ||
if (this._decidePollInterval) { | ||
this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval); | ||
} | ||
this._decideResponsePromise = undefined; | ||
return [4 /*yield*/, this.decideAsync()]; | ||
case 0: return [4 /*yield*/, this.decideAsync()]; | ||
case 1: return [2 /*return*/, (_a.sent()).featureFlags]; | ||
@@ -1008,8 +1012,4 @@ } | ||
}; | ||
// When listening to feature flags polling is active | ||
PostHogCore.prototype.onFeatureFlags = function (cb) { | ||
var _this = this; | ||
if (!this._decideTimer) { | ||
void this.reloadFeatureFlagsAsync(); | ||
} | ||
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -1026,2 +1026,15 @@ var flags; | ||
}; | ||
PostHogCore.prototype.onFeatureFlag = function (key, cb) { | ||
var _this = this; | ||
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () { | ||
var flagResponse; | ||
return __generator(this, function (_a) { | ||
flagResponse = this.getFeatureFlag(key); | ||
if (flagResponse !== undefined) { | ||
cb(flagResponse); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
}; | ||
PostHogCore.prototype.overrideFeatureFlag = function (flags) { | ||
@@ -1134,3 +1147,2 @@ if (flags === null) { | ||
case 0: | ||
clearTimeout(this._decideTimer); | ||
clearTimeout(this._flushTimer); | ||
@@ -1225,4 +1237,2 @@ return [4 /*yield*/, this.flushAsync()]; | ||
options.decidePollInterval = 0; // Forcefully set to 0 so we don't auto-reload | ||
this._sharedClient = new PostHog(apiKey, options); | ||
@@ -1229,0 +1239,0 @@ } |
@@ -8,3 +8,2 @@ declare type PosthogCoreOptions = { | ||
preloadFeatureFlags?: boolean; | ||
decidePollInterval?: number; | ||
fetchRetryCount?: number; | ||
@@ -11,0 +10,0 @@ fetchRetryDelay?: number; |
@@ -134,6 +134,7 @@ import undici from 'undici'; | ||
var version = "2.0.0-alpha8"; | ||
var version = "2.0.0-alpha9"; | ||
var PostHogPersistedProperty; | ||
(function (PostHogPersistedProperty) { | ||
PostHogPersistedProperty["AnonymousId"] = "anonymous_id"; | ||
PostHogPersistedProperty["DistinctId"] = "distinct_id"; | ||
@@ -693,3 +694,3 @@ PostHogPersistedProperty["Props"] = "props"; | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f; | ||
var _a, _b, _c, _d, _e; | ||
this.flagCallReported = {}; | ||
@@ -705,10 +706,9 @@ // internal | ||
this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true; | ||
this._decidePollInterval = Math.max(0, (_c = options === null || options === void 0 ? void 0 : options.decidePollInterval) !== null && _c !== void 0 ? _c : 30000); | ||
// If enable is explicitly set to false we override the optout | ||
this._optoutOverride = (options === null || options === void 0 ? void 0 : options.enable) === false; | ||
this._retryOptions = { | ||
retryCount: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _d !== void 0 ? _d : 3, | ||
retryDelay: (_e = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _e !== void 0 ? _e : 3000, | ||
retryCount: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _c !== void 0 ? _c : 3, | ||
retryDelay: (_d = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _d !== void 0 ? _d : 3000, | ||
}; | ||
this._sessionExpirationTimeSeconds = (_f = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _f !== void 0 ? _f : 1800; // 30 minutes | ||
this._sessionExpirationTimeSeconds = (_e = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _e !== void 0 ? _e : 1800; // 30 minutes | ||
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent | ||
@@ -765,3 +765,2 @@ if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) { | ||
} | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, generateUUID(globalThis)); | ||
}; | ||
@@ -796,10 +795,13 @@ PostHogCore.prototype.debug = function (enabled) { | ||
}; | ||
PostHogCore.prototype.getDistinctId = function () { | ||
var distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId); | ||
if (!distinctId) { | ||
distinctId = generateUUID(globalThis); | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId); | ||
PostHogCore.prototype.getAnonymousId = function () { | ||
var anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId); | ||
if (!anonId) { | ||
anonId = generateUUID(globalThis); | ||
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId); | ||
} | ||
return distinctId; | ||
return anonId; | ||
}; | ||
PostHogCore.prototype.getDistinctId = function () { | ||
return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId(); | ||
}; | ||
PostHogCore.prototype.register = function (properties) { | ||
@@ -817,3 +819,4 @@ this.props = __assign(__assign({}, this.props), properties); | ||
PostHogCore.prototype.identify = function (distinctId, properties) { | ||
distinctId = distinctId || this.getDistinctId(); | ||
var previousDistinctId = this.getDistinctId(); | ||
distinctId = distinctId || previousDistinctId; | ||
if (properties === null || properties === void 0 ? void 0 : properties.$groups) { | ||
@@ -825,6 +828,11 @@ this.groups(properties.$groups); | ||
event: '$identify', | ||
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getDistinctId() }), | ||
properties: __assign(__assign({}, (properties || {})), { $anon_distinct_id: this.getAnonymousId() }), | ||
})), { $set: properties }); | ||
if (distinctId !== this.getDistinctId()) { | ||
if (distinctId !== previousDistinctId) { | ||
// We keep the AnonymousId to be used by decide calls and identify to link the previousId | ||
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId); | ||
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId); | ||
if (this.getFeatureFlags()) { | ||
void this.reloadFeatureFlagsAsync(); | ||
} | ||
} | ||
@@ -835,7 +843,2 @@ this.enqueue('identify', payload); | ||
PostHogCore.prototype.capture = function (event, properties) { | ||
// NOTE: Legacy nodejs implementation uses groups | ||
if (properties && properties['groups']) { | ||
properties.$groups = properties.groups; | ||
delete properties.groups; | ||
} | ||
if (properties === null || properties === void 0 ? void 0 : properties.$groups) { | ||
@@ -879,3 +882,3 @@ this.groups(properties.$groups); | ||
}); | ||
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this._decideResponsePromise) { | ||
if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) { | ||
void this.reloadFeatureFlagsAsync(); | ||
@@ -924,3 +927,8 @@ } | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ groups: groups, distinct_id: distinctId, token: this.apiKey }), | ||
body: JSON.stringify({ | ||
token: this.apiKey, | ||
distinct_id: distinctId, | ||
$anon_distinct_id: this.getAnonymousId(), | ||
groups: groups, | ||
}), | ||
}; | ||
@@ -932,5 +940,8 @@ this._decideResponsePromise = this.fetchWithRetry(url, fetchOptions) | ||
_this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags); | ||
_this._events.emit('featureflags', res.featureFlags); | ||
} | ||
_this._events.emit('featureflags', res.featureFlags); | ||
return res; | ||
}) | ||
.finally(function () { | ||
_this._decideResponsePromise = undefined; | ||
}); | ||
@@ -984,12 +995,5 @@ return [2 /*return*/, this._decideResponsePromise]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
clearTimeout(this._decideTimer); | ||
if (this._decidePollInterval) { | ||
this._decideTimer = safeSetTimeout(function () { return _this.reloadFeatureFlagsAsync(); }, this._decidePollInterval); | ||
} | ||
this._decideResponsePromise = undefined; | ||
return [4 /*yield*/, this.decideAsync()]; | ||
case 0: return [4 /*yield*/, this.decideAsync()]; | ||
case 1: return [2 /*return*/, (_a.sent()).featureFlags]; | ||
@@ -1000,8 +1004,4 @@ } | ||
}; | ||
// When listening to feature flags polling is active | ||
PostHogCore.prototype.onFeatureFlags = function (cb) { | ||
var _this = this; | ||
if (!this._decideTimer) { | ||
void this.reloadFeatureFlagsAsync(); | ||
} | ||
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -1018,2 +1018,15 @@ var flags; | ||
}; | ||
PostHogCore.prototype.onFeatureFlag = function (key, cb) { | ||
var _this = this; | ||
return this.on('featureflags', function () { return __awaiter(_this, void 0, void 0, function () { | ||
var flagResponse; | ||
return __generator(this, function (_a) { | ||
flagResponse = this.getFeatureFlag(key); | ||
if (flagResponse !== undefined) { | ||
cb(flagResponse); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
}; | ||
PostHogCore.prototype.overrideFeatureFlag = function (flags) { | ||
@@ -1126,3 +1139,2 @@ if (flags === null) { | ||
case 0: | ||
clearTimeout(this._decideTimer); | ||
clearTimeout(this._flushTimer); | ||
@@ -1217,4 +1229,2 @@ return [4 /*yield*/, this.flushAsync()]; | ||
options.decidePollInterval = 0; // Forcefully set to 0 so we don't auto-reload | ||
this._sharedClient = new PostHog(apiKey, options); | ||
@@ -1221,0 +1231,0 @@ } |
@@ -18,4 +18,2 @@ import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty } from './types'; | ||
protected _decideResponsePromise?: Promise<PostHogDecideResponse>; | ||
protected _decideTimer?: any; | ||
protected _decidePollInterval: number; | ||
protected _retryOptions: RetriableOptions; | ||
@@ -44,2 +42,3 @@ protected _sessionExpirationTimeSeconds: number; | ||
resetSessionId(): void; | ||
getAnonymousId(): string; | ||
getDistinctId(): string; | ||
@@ -77,2 +76,3 @@ register(properties: { | ||
onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void; | ||
onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void; | ||
overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): void; | ||
@@ -79,0 +79,0 @@ /*** |
@@ -8,3 +8,2 @@ export declare type PosthogCoreOptions = { | ||
preloadFeatureFlags?: boolean; | ||
decidePollInterval?: number; | ||
fetchRetryCount?: number; | ||
@@ -16,2 +15,3 @@ fetchRetryDelay?: number; | ||
export declare enum PostHogPersistedProperty { | ||
AnonymousId = "anonymous_id", | ||
DistinctId = "distinct_id", | ||
@@ -18,0 +18,0 @@ Props = "props", |
@@ -12,3 +12,2 @@ export declare function assert(truthyValue: any, message: string): void; | ||
export declare function currentISOTime(): string; | ||
export declare function isUndefined(obj: any): boolean; | ||
export declare function safeSetTimeout(fn: () => void, timeout: number): any; |
{ | ||
"name": "posthog-node", | ||
"version": "2.0.0-alpha8", | ||
"version": "2.0.0-alpha9", | ||
"description": "PostHog Node.js integration", | ||
@@ -5,0 +5,0 @@ "repository": "PostHog/posthog-node", |
@@ -60,3 +60,2 @@ import { version } from '../package.json' | ||
constructor(apiKey: string, options: PostHogOptions = {}) { | ||
options.decidePollInterval = 0 // Forcefully set to 0 so we don't auto-reload | ||
this._sharedClient = new PostHog(apiKey, options) | ||
@@ -63,0 +62,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3371
352409