@posthog/core 1.11.0 → 1.13.0
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/dist/posthog-core-stateless.d.ts +11 -3
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +39 -5
- package/dist/posthog-core-stateless.mjs +39 -5
- package/dist/posthog-core.d.ts +23 -4
- package/dist/posthog-core.d.ts.map +1 -1
- package/dist/posthog-core.js +95 -16
- package/dist/posthog-core.mjs +96 -17
- package/dist/testing/PostHogCoreTestClient.d.ts +2 -2
- package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
- package/dist/types.d.ts +76 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +12 -0
- package/dist/types.mjs +10 -1
- package/package.json +1 -1
- package/src/posthog-core-stateless.ts +57 -11
- package/src/posthog-core.ts +168 -32
- package/src/testing/PostHogCoreTestClient.ts +2 -8
- package/src/types.ts +82 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SimpleEventEmitter } from './eventemitter';
|
|
2
|
-
import { PostHogFlagsResponse, PostHogCoreOptions, PostHogEventProperties, PostHogCaptureOptions, JsonType, PostHogRemoteConfig, FeatureFlagValue, PostHogFeatureFlagDetails, FeatureFlagDetail, SurveyResponse, PostHogFetchResponse, PostHogFetchOptions, PostHogPersistedProperty, Logger } from './types';
|
|
2
|
+
import { PostHogFlagsResponse, PostHogCoreOptions, PostHogEventProperties, PostHogCaptureOptions, JsonType, PostHogRemoteConfig, FeatureFlagValue, PostHogFeatureFlagDetails, FeatureFlagDetail, SurveyResponse, PostHogFetchResponse, PostHogFetchOptions, PostHogPersistedProperty, Logger, GetFlagsResult } from './types';
|
|
3
3
|
import { RetriableOptions } from './utils';
|
|
4
4
|
export declare const maybeAdd: (key: string, value: JsonType | undefined) => Record<string, JsonType>;
|
|
5
5
|
export declare function logFlushError(err: any): Promise<void>;
|
|
@@ -97,7 +97,8 @@ export declare abstract class PostHogCoreStateless {
|
|
|
97
97
|
/***
|
|
98
98
|
*** FEATURE FLAGS
|
|
99
99
|
***/
|
|
100
|
-
protected getFlags(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>, fetchConfig?: boolean): Promise<
|
|
100
|
+
protected getFlags(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>, fetchConfig?: boolean): Promise<GetFlagsResult>;
|
|
101
|
+
private categorizeRequestError;
|
|
101
102
|
protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
|
|
102
103
|
response: FeatureFlagValue | undefined;
|
|
103
104
|
requestId: string | undefined;
|
|
@@ -135,9 +136,16 @@ export declare abstract class PostHogCoreStateless {
|
|
|
135
136
|
/***
|
|
136
137
|
*** QUEUEING AND FLUSHING
|
|
137
138
|
***/
|
|
139
|
+
/**
|
|
140
|
+
* Hook that allows subclasses to transform or filter a message before it's queued.
|
|
141
|
+
* Return null to drop the message.
|
|
142
|
+
* @param message The prepared message
|
|
143
|
+
* @returns The transformed message, or null to drop it
|
|
144
|
+
*/
|
|
145
|
+
protected processBeforeEnqueue(message: PostHogEventProperties): PostHogEventProperties | null;
|
|
138
146
|
protected enqueue(type: string, _message: any, options?: PostHogCaptureOptions): void;
|
|
139
147
|
protected sendImmediate(type: string, _message: any, options?: PostHogCaptureOptions): Promise<void>;
|
|
140
|
-
|
|
148
|
+
protected prepareMessage(type: string, _message: any, options?: PostHogCaptureOptions): PostHogEventProperties;
|
|
141
149
|
private clearFlushTimer;
|
|
142
150
|
/**
|
|
143
151
|
* Helper for flushing the queue in the background
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"posthog-core-stateless.d.ts","sourceRoot":"","sources":["../src/posthog-core-stateless.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAGnD,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,QAAQ,EACR,mBAAmB,EACnB,gBAAgB,EAGhB,yBAAyB,EACzB,iBAAiB,EACjB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,wBAAwB,EAExB,MAAM,
|
|
1
|
+
{"version":3,"file":"posthog-core-stateless.d.ts","sourceRoot":"","sources":["../src/posthog-core-stateless.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAGnD,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,QAAQ,EACR,mBAAmB,EACnB,gBAAgB,EAGhB,yBAAyB,EACzB,iBAAiB,EACjB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,wBAAwB,EAExB,MAAM,EACN,cAAc,EAEf,MAAM,SAAS,CAAA;AAChB,OAAO,EAOL,gBAAgB,EAIjB,MAAM,SAAS,CAAA;AAqChB,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS,KAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,CAC9C,CAAA;AAE7C,wBAAsB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAY3D;AAUD,oBAAY,mBAAmB;IAC7B,YAAY,kBAAkB;IAC9B,UAAU,eAAe;CAC1B;AAED,8BAAsB,oBAAoB;IAExC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAA;IACrC,QAAQ,CAAC,cAAc,EAAE,OAAO,C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,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAuD1G,SAAS,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,sBAAsB;IA0B9G,OAAO,CAAC,eAAe;IAOvB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAMvB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAsB5B,SAAS,CAAC,gBAAgB,IAAI;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE;YAazC,MAAM;YA0FN,cAAc;IAsDtB,SAAS,CAAC,iBAAiB,GAAE,MAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAiDjE;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,QAAQ,CAAC,iBAAiB,GAAE,MAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAYjE"}
|
|
@@ -299,10 +299,35 @@ class PostHogCoreStateless {
|
|
|
299
299
|
this._logger.info('Flags URL', url);
|
|
300
300
|
return this.fetchWithRetry(url, fetchOptions, {
|
|
301
301
|
retryCount: 0
|
|
302
|
-
}, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>(
|
|
302
|
+
}, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>({
|
|
303
|
+
success: true,
|
|
304
|
+
response: (0, external_featureFlagUtils_js_namespaceObject.normalizeFlagsResponse)(response)
|
|
305
|
+
})).catch((error)=>{
|
|
303
306
|
this._events.emit('error', error);
|
|
307
|
+
return {
|
|
308
|
+
success: false,
|
|
309
|
+
error: this.categorizeRequestError(error)
|
|
310
|
+
};
|
|
304
311
|
});
|
|
305
312
|
}
|
|
313
|
+
categorizeRequestError(error) {
|
|
314
|
+
if (error instanceof PostHogFetchHttpError) return {
|
|
315
|
+
type: 'api_error',
|
|
316
|
+
statusCode: error.status
|
|
317
|
+
};
|
|
318
|
+
if (error instanceof PostHogFetchNetworkError) {
|
|
319
|
+
const cause = error.error;
|
|
320
|
+
if (cause instanceof Error && ('AbortError' === cause.name || 'TimeoutError' === cause.name)) return {
|
|
321
|
+
type: 'timeout'
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
type: 'connection_error'
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
type: 'unknown_error'
|
|
329
|
+
};
|
|
330
|
+
}
|
|
306
331
|
async getFeatureFlagStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
|
|
307
332
|
await this._initPromise;
|
|
308
333
|
const flagDetailResponse = await this.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
|
|
@@ -369,8 +394,9 @@ class PostHogCoreStateless {
|
|
|
369
394
|
const extraPayload = {};
|
|
370
395
|
if (disableGeoip ?? this.disableGeoip) extraPayload['geoip_disable'] = true;
|
|
371
396
|
if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
|
|
372
|
-
const
|
|
373
|
-
if (
|
|
397
|
+
const result = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
398
|
+
if (!result.success) return;
|
|
399
|
+
const flagsResponse = result.response;
|
|
374
400
|
if (flagsResponse.errorsWhileComputingFlags) console.error('[FEATURE FLAGS] Error while computing feature flags, some flags may be missing or incorrect. Learn more at https://posthog.com/docs/feature-flags/best-practices');
|
|
375
401
|
if (flagsResponse.quotaLimited?.includes("feature_flags")) {
|
|
376
402
|
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - feature flags unavailable. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
@@ -437,10 +463,15 @@ class PostHogCoreStateless {
|
|
|
437
463
|
this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.Props, this.props);
|
|
438
464
|
});
|
|
439
465
|
}
|
|
466
|
+
processBeforeEnqueue(message) {
|
|
467
|
+
return message;
|
|
468
|
+
}
|
|
440
469
|
enqueue(type, _message, options) {
|
|
441
470
|
this.wrap(()=>{
|
|
442
471
|
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
443
|
-
|
|
472
|
+
let message = this.prepareMessage(type, _message, options);
|
|
473
|
+
message = this.processBeforeEnqueue(message);
|
|
474
|
+
if (null === message) return;
|
|
444
475
|
const queue = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.Queue) || [];
|
|
445
476
|
if (queue.length >= this.maxQueueSize) {
|
|
446
477
|
queue.shift();
|
|
@@ -459,10 +490,13 @@ class PostHogCoreStateless {
|
|
|
459
490
|
if (this.disabled) return void this._logger.warn('The client is disabled');
|
|
460
491
|
if (!this._isInitialized) await this._initPromise;
|
|
461
492
|
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
493
|
+
let message = this.prepareMessage(type, _message, options);
|
|
494
|
+
message = this.processBeforeEnqueue(message);
|
|
495
|
+
if (null === message) return;
|
|
462
496
|
const data = {
|
|
463
497
|
api_key: this.apiKey,
|
|
464
498
|
batch: [
|
|
465
|
-
|
|
499
|
+
message
|
|
466
500
|
],
|
|
467
501
|
sent_at: (0, index_js_namespaceObject.currentISOTime)()
|
|
468
502
|
};
|
|
@@ -268,10 +268,35 @@ class PostHogCoreStateless {
|
|
|
268
268
|
this._logger.info('Flags URL', url);
|
|
269
269
|
return this.fetchWithRetry(url, fetchOptions, {
|
|
270
270
|
retryCount: 0
|
|
271
|
-
}, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>
|
|
271
|
+
}, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>({
|
|
272
|
+
success: true,
|
|
273
|
+
response: normalizeFlagsResponse(response)
|
|
274
|
+
})).catch((error)=>{
|
|
272
275
|
this._events.emit('error', error);
|
|
276
|
+
return {
|
|
277
|
+
success: false,
|
|
278
|
+
error: this.categorizeRequestError(error)
|
|
279
|
+
};
|
|
273
280
|
});
|
|
274
281
|
}
|
|
282
|
+
categorizeRequestError(error) {
|
|
283
|
+
if (error instanceof PostHogFetchHttpError) return {
|
|
284
|
+
type: 'api_error',
|
|
285
|
+
statusCode: error.status
|
|
286
|
+
};
|
|
287
|
+
if (error instanceof PostHogFetchNetworkError) {
|
|
288
|
+
const cause = error.error;
|
|
289
|
+
if (cause instanceof Error && ('AbortError' === cause.name || 'TimeoutError' === cause.name)) return {
|
|
290
|
+
type: 'timeout'
|
|
291
|
+
};
|
|
292
|
+
return {
|
|
293
|
+
type: 'connection_error'
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
type: 'unknown_error'
|
|
298
|
+
};
|
|
299
|
+
}
|
|
275
300
|
async getFeatureFlagStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
|
|
276
301
|
await this._initPromise;
|
|
277
302
|
const flagDetailResponse = await this.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
|
|
@@ -338,8 +363,9 @@ class PostHogCoreStateless {
|
|
|
338
363
|
const extraPayload = {};
|
|
339
364
|
if (disableGeoip ?? this.disableGeoip) extraPayload['geoip_disable'] = true;
|
|
340
365
|
if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
|
|
341
|
-
const
|
|
342
|
-
if (
|
|
366
|
+
const result = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
367
|
+
if (!result.success) return;
|
|
368
|
+
const flagsResponse = result.response;
|
|
343
369
|
if (flagsResponse.errorsWhileComputingFlags) console.error('[FEATURE FLAGS] Error while computing feature flags, some flags may be missing or incorrect. Learn more at https://posthog.com/docs/feature-flags/best-practices');
|
|
344
370
|
if (flagsResponse.quotaLimited?.includes("feature_flags")) {
|
|
345
371
|
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - feature flags unavailable. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
@@ -406,10 +432,15 @@ class PostHogCoreStateless {
|
|
|
406
432
|
this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
|
|
407
433
|
});
|
|
408
434
|
}
|
|
435
|
+
processBeforeEnqueue(message) {
|
|
436
|
+
return message;
|
|
437
|
+
}
|
|
409
438
|
enqueue(type, _message, options) {
|
|
410
439
|
this.wrap(()=>{
|
|
411
440
|
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
412
|
-
|
|
441
|
+
let message = this.prepareMessage(type, _message, options);
|
|
442
|
+
message = this.processBeforeEnqueue(message);
|
|
443
|
+
if (null === message) return;
|
|
413
444
|
const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
414
445
|
if (queue.length >= this.maxQueueSize) {
|
|
415
446
|
queue.shift();
|
|
@@ -428,10 +459,13 @@ class PostHogCoreStateless {
|
|
|
428
459
|
if (this.disabled) return void this._logger.warn('The client is disabled');
|
|
429
460
|
if (!this._isInitialized) await this._initPromise;
|
|
430
461
|
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
462
|
+
let message = this.prepareMessage(type, _message, options);
|
|
463
|
+
message = this.processBeforeEnqueue(message);
|
|
464
|
+
if (null === message) return;
|
|
431
465
|
const data = {
|
|
432
466
|
api_key: this.apiKey,
|
|
433
467
|
batch: [
|
|
434
|
-
|
|
468
|
+
message
|
|
435
469
|
],
|
|
436
470
|
sent_at: currentISOTime()
|
|
437
471
|
};
|
package/dist/posthog-core.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { PostHogAutocaptureElement, PostHogFlagsResponse, PostHogCoreOptions, PostHogEventProperties, PostHogCaptureOptions, JsonType, PostHogRemoteConfig, FeatureFlagValue, PostHogFeatureFlagDetails, PostHogGroupProperties } from './types';
|
|
1
|
+
import type { PostHogAutocaptureElement, PostHogFlagsResponse, PostHogFeatureFlagsResponse, PostHogCoreOptions, PostHogEventProperties, PostHogCaptureOptions, JsonType, PostHogRemoteConfig, FeatureFlagValue, PostHogFeatureFlagDetails, PostHogGroupProperties } from './types';
|
|
2
2
|
import { PostHogPersistedProperty } from './types';
|
|
3
3
|
import { PostHogCoreStateless } from './posthog-core-stateless';
|
|
4
4
|
export declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
5
5
|
private sendFeatureFlagEvent;
|
|
6
6
|
private flagCallReported;
|
|
7
|
-
|
|
7
|
+
private _beforeSend?;
|
|
8
|
+
protected _flagsResponsePromise?: Promise<PostHogFeatureFlagsResponse | undefined>;
|
|
8
9
|
protected _sessionExpirationTimeSeconds: number;
|
|
9
10
|
private _sessionMaxLengthSeconds;
|
|
10
11
|
protected sessionProps: PostHogEventProperties;
|
|
@@ -82,14 +83,14 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
82
83
|
/***
|
|
83
84
|
*** FEATURE FLAGS
|
|
84
85
|
***/
|
|
85
|
-
protected flagsAsync(sendAnonDistinctId?: boolean, fetchConfig?: boolean): Promise<
|
|
86
|
+
protected flagsAsync(sendAnonDistinctId?: boolean, fetchConfig?: boolean): Promise<PostHogFeatureFlagsResponse | undefined>;
|
|
86
87
|
private cacheSessionReplay;
|
|
87
88
|
private _remoteConfigAsync;
|
|
88
89
|
private _flagsAsync;
|
|
89
90
|
private setKnownFeatureFlagDetails;
|
|
90
91
|
private getKnownFeatureFlagDetails;
|
|
92
|
+
private getStoredFlagDetails;
|
|
91
93
|
protected getKnownFeatureFlags(): PostHogFlagsResponse['featureFlags'] | undefined;
|
|
92
|
-
private getKnownFeatureFlagPayloads;
|
|
93
94
|
private getBootstrappedFeatureFlagDetails;
|
|
94
95
|
private setBootstrappedFeatureFlagDetails;
|
|
95
96
|
private getBootstrappedFeatureFlags;
|
|
@@ -167,5 +168,23 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
167
168
|
* @param metricValue The value of the metric to capture.
|
|
168
169
|
*/
|
|
169
170
|
captureTraceMetric(traceId: string | number, metricName: string, metricValue: string | number | boolean): void;
|
|
171
|
+
/**
|
|
172
|
+
* Override processBeforeEnqueue to run before_send hooks.
|
|
173
|
+
* This runs after prepareMessage, giving users full control over the final event.
|
|
174
|
+
*
|
|
175
|
+
* The internal message contains many fields (event, distinct_id, properties, type, library,
|
|
176
|
+
* library_version, timestamp, uuid). CaptureEvent exposes a subset matching the web SDK's
|
|
177
|
+
* CaptureResult: uuid, event, properties, $set, $set_once, timestamp.
|
|
178
|
+
* Note: $set/$set_once are extracted from properties.$set and properties.$set_once.
|
|
179
|
+
*/
|
|
180
|
+
protected processBeforeEnqueue(message: PostHogEventProperties): PostHogEventProperties | null;
|
|
181
|
+
/**
|
|
182
|
+
* Runs the before_send hook(s) on the given capture event.
|
|
183
|
+
* If any hook returns null, the event is dropped.
|
|
184
|
+
*
|
|
185
|
+
* @param captureEvent The event to process
|
|
186
|
+
* @returns The processed event, or null if the event should be dropped
|
|
187
|
+
*/
|
|
188
|
+
private _runBeforeSend;
|
|
170
189
|
}
|
|
171
190
|
//# sourceMappingURL=posthog-core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"posthog-core.d.ts","sourceRoot":"","sources":["../src/posthog-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,QAAQ,EACR,mBAAmB,EACnB,gBAAgB,EAGhB,yBAAyB,EAKzB,sBAAsB,
|
|
1
|
+
{"version":3,"file":"posthog-core.d.ts","sourceRoot":"","sources":["../src/posthog-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,oBAAoB,EACpB,2BAA2B,EAC3B,kBAAkB,EAClB,sBAAsB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GAAG,SAAS,CAAC;IAI5D,cAAc,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,oBAAoB,CAAC,cAAc,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI;IASrF,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,MAAM,IAAI;IASvE,mBAAmB,CAAC,KAAK,EAAE,oBAAoB,CAAC,cAAc,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAS5F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,oBAAoB,CAAC,EAAE,sBAAsB,GAAG,IAAI;IAmBrF;;;;;;;;;OASG;IACH,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI;IAO1E;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI;IAQ9G;;;;;;;;OAQG;IACH,SAAS,CAAC,oBAAoB,CAAC,OAAO,EAAE,sBAAsB,GAAG,sBAAsB,GAAG,IAAI;IA+C9F;;;;;;OAMG;IACH,OAAO,CAAC,cAAc;CAsBvB"}
|
package/dist/posthog-core.js
CHANGED
|
@@ -42,6 +42,7 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
42
42
|
}), this.flagCallReported = {}, this._sessionMaxLengthSeconds = 86400, this.sessionProps = {};
|
|
43
43
|
this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
|
|
44
44
|
this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800;
|
|
45
|
+
this._beforeSend = options?.before_send;
|
|
45
46
|
}
|
|
46
47
|
setupBootstrap(options) {
|
|
47
48
|
const bootstrap = options?.bootstrap;
|
|
@@ -339,10 +340,18 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
339
340
|
const extraProperties = {
|
|
340
341
|
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : void 0
|
|
341
342
|
};
|
|
342
|
-
const
|
|
343
|
+
const result = await super.getFlags(distinctId, groups, personProperties, groupProperties, extraProperties, fetchConfig);
|
|
344
|
+
if (!result.success) return void this.setKnownFeatureFlagDetails({
|
|
345
|
+
flags: this.getKnownFeatureFlagDetails()?.flags ?? {},
|
|
346
|
+
requestError: result.error
|
|
347
|
+
});
|
|
348
|
+
const res = result.response;
|
|
343
349
|
if (res?.quotaLimited?.includes(external_posthog_core_stateless_js_namespaceObject.QuotaLimitedFeature.FeatureFlags)) {
|
|
344
|
-
this.setKnownFeatureFlagDetails(
|
|
345
|
-
|
|
350
|
+
this.setKnownFeatureFlagDetails({
|
|
351
|
+
flags: this.getKnownFeatureFlagDetails()?.flags ?? {},
|
|
352
|
+
quotaLimited: res.quotaLimited
|
|
353
|
+
});
|
|
354
|
+
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
346
355
|
return res;
|
|
347
356
|
}
|
|
348
357
|
if (res?.featureFlags) {
|
|
@@ -359,7 +368,13 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
359
368
|
}
|
|
360
369
|
};
|
|
361
370
|
}
|
|
362
|
-
this.setKnownFeatureFlagDetails(
|
|
371
|
+
this.setKnownFeatureFlagDetails({
|
|
372
|
+
flags: newFeatureFlagDetails.flags,
|
|
373
|
+
requestId: res.requestId,
|
|
374
|
+
evaluatedAt: res.evaluatedAt,
|
|
375
|
+
errorsWhileComputingFlags: res.errorsWhileComputingFlags,
|
|
376
|
+
quotaLimited: res.quotaLimited
|
|
377
|
+
});
|
|
363
378
|
this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FlagsEndpointWasHit, true);
|
|
364
379
|
this.cacheSessionReplay('flags', res);
|
|
365
380
|
}
|
|
@@ -385,16 +400,14 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
385
400
|
}
|
|
386
401
|
return (0, external_featureFlagUtils_js_namespaceObject.normalizeFlagsResponse)(storedDetails);
|
|
387
402
|
}
|
|
403
|
+
getStoredFlagDetails() {
|
|
404
|
+
return this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FeatureFlagDetails);
|
|
405
|
+
}
|
|
388
406
|
getKnownFeatureFlags() {
|
|
389
407
|
const featureFlagDetails = this.getKnownFeatureFlagDetails();
|
|
390
408
|
if (!featureFlagDetails) return;
|
|
391
409
|
return (0, external_featureFlagUtils_js_namespaceObject.getFlagValuesFromFlags)(featureFlagDetails.flags);
|
|
392
410
|
}
|
|
393
|
-
getKnownFeatureFlagPayloads() {
|
|
394
|
-
const featureFlagDetails = this.getKnownFeatureFlagDetails();
|
|
395
|
-
if (!featureFlagDetails) return;
|
|
396
|
-
return (0, external_featureFlagUtils_js_namespaceObject.getPayloadsFromFlags)(featureFlagDetails.flags);
|
|
397
|
-
}
|
|
398
411
|
getBootstrappedFeatureFlagDetails() {
|
|
399
412
|
const details = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.BootstrapFeatureFlagDetails);
|
|
400
413
|
if (!details) return;
|
|
@@ -414,16 +427,33 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
414
427
|
return (0, external_featureFlagUtils_js_namespaceObject.getPayloadsFromFlags)(details.flags);
|
|
415
428
|
}
|
|
416
429
|
getFeatureFlag(key) {
|
|
430
|
+
const storedDetails = this.getStoredFlagDetails();
|
|
417
431
|
const details = this.getFeatureFlagDetails();
|
|
418
|
-
|
|
419
|
-
const
|
|
432
|
+
const errors = [];
|
|
433
|
+
const isQuotaLimited = storedDetails?.quotaLimited?.includes(external_posthog_core_stateless_js_namespaceObject.QuotaLimitedFeature.FeatureFlags);
|
|
434
|
+
if (storedDetails?.requestError) {
|
|
435
|
+
const { type, statusCode } = storedDetails.requestError;
|
|
436
|
+
if ('timeout' === type) errors.push(external_types_js_namespaceObject.FeatureFlagError.TIMEOUT);
|
|
437
|
+
else if ('api_error' === type && void 0 !== statusCode) errors.push(external_types_js_namespaceObject.FeatureFlagError.apiError(statusCode));
|
|
438
|
+
else if ('connection_error' === type) errors.push(external_types_js_namespaceObject.FeatureFlagError.CONNECTION_ERROR);
|
|
439
|
+
else errors.push(external_types_js_namespaceObject.FeatureFlagError.UNKNOWN_ERROR);
|
|
440
|
+
} else if (storedDetails) {
|
|
441
|
+
if (storedDetails.errorsWhileComputingFlags) errors.push(external_types_js_namespaceObject.FeatureFlagError.ERRORS_WHILE_COMPUTING);
|
|
442
|
+
if (isQuotaLimited) errors.push(external_types_js_namespaceObject.FeatureFlagError.QUOTA_LIMITED);
|
|
443
|
+
}
|
|
444
|
+
const featureFlag = details?.flags[key];
|
|
420
445
|
let response = (0, external_featureFlagUtils_js_namespaceObject.getFeatureFlagValue)(featureFlag);
|
|
421
|
-
if (void 0 === response)
|
|
446
|
+
if (void 0 === response) {
|
|
447
|
+
const hasCachedFlags = details && Object.keys(details.flags).length > 0;
|
|
448
|
+
if (hasCachedFlags) response = false;
|
|
449
|
+
if (details && !featureFlag && !storedDetails?.requestError && !isQuotaLimited) errors.push(external_types_js_namespaceObject.FeatureFlagError.FLAG_MISSING);
|
|
450
|
+
}
|
|
422
451
|
if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
|
|
423
452
|
const bootstrappedResponse = this.getBootstrappedFeatureFlags()?.[key];
|
|
424
453
|
const bootstrappedPayload = this.getBootstrappedFeatureFlagPayloads()?.[key];
|
|
454
|
+
const featureFlagError = errors.length > 0 ? errors.join(',') : void 0;
|
|
425
455
|
this.flagCallReported[key] = true;
|
|
426
|
-
|
|
456
|
+
const properties = {
|
|
427
457
|
$feature_flag: key,
|
|
428
458
|
$feature_flag_response: response,
|
|
429
459
|
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_id', featureFlag?.metadata?.id),
|
|
@@ -432,9 +462,11 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
432
462
|
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_bootstrapped_response', bootstrappedResponse),
|
|
433
463
|
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_bootstrapped_payload', bootstrappedPayload),
|
|
434
464
|
$used_bootstrap_value: !this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FlagsEndpointWasHit),
|
|
435
|
-
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_request_id', details
|
|
436
|
-
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_evaluated_at', details
|
|
437
|
-
|
|
465
|
+
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_request_id', details?.requestId),
|
|
466
|
+
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_evaluated_at', details?.evaluatedAt),
|
|
467
|
+
...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_error', featureFlagError)
|
|
468
|
+
};
|
|
469
|
+
this.capture('$feature_flag_called', properties);
|
|
438
470
|
}
|
|
439
471
|
return response;
|
|
440
472
|
}
|
|
@@ -544,6 +576,53 @@ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.Pos
|
|
|
544
576
|
$ai_trace_id: String(traceId)
|
|
545
577
|
});
|
|
546
578
|
}
|
|
579
|
+
processBeforeEnqueue(message) {
|
|
580
|
+
if (!this._beforeSend) return message;
|
|
581
|
+
const timestamp = message.timestamp;
|
|
582
|
+
const props = message.properties || {};
|
|
583
|
+
const captureEvent = {
|
|
584
|
+
uuid: message.uuid,
|
|
585
|
+
event: message.event,
|
|
586
|
+
properties: props,
|
|
587
|
+
$set: props.$set,
|
|
588
|
+
$set_once: props.$set_once,
|
|
589
|
+
timestamp: 'string' == typeof timestamp ? new Date(timestamp) : timestamp
|
|
590
|
+
};
|
|
591
|
+
const result = this._runBeforeSend(captureEvent);
|
|
592
|
+
if (!result) return null;
|
|
593
|
+
const resultProps = {
|
|
594
|
+
...result.properties ?? props
|
|
595
|
+
};
|
|
596
|
+
if (void 0 !== result.$set) resultProps.$set = result.$set;
|
|
597
|
+
else delete resultProps.$set;
|
|
598
|
+
if (void 0 !== result.$set_once) resultProps.$set_once = result.$set_once;
|
|
599
|
+
else delete resultProps.$set_once;
|
|
600
|
+
return {
|
|
601
|
+
...message,
|
|
602
|
+
uuid: result.uuid ?? message.uuid,
|
|
603
|
+
event: result.event,
|
|
604
|
+
properties: resultProps,
|
|
605
|
+
timestamp: result.timestamp
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
_runBeforeSend(captureEvent) {
|
|
609
|
+
const beforeSend = this._beforeSend;
|
|
610
|
+
if (!beforeSend) return captureEvent;
|
|
611
|
+
const fns = Array.isArray(beforeSend) ? beforeSend : [
|
|
612
|
+
beforeSend
|
|
613
|
+
];
|
|
614
|
+
let result = captureEvent;
|
|
615
|
+
for (const fn of fns)try {
|
|
616
|
+
result = fn(result);
|
|
617
|
+
if (!result) {
|
|
618
|
+
this._logger.info(`Event '${captureEvent.event}' was rejected in before_send function`);
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
} catch (e) {
|
|
622
|
+
this._logger.error(`Error in before_send function for event '${captureEvent.event}':`, e);
|
|
623
|
+
}
|
|
624
|
+
return result;
|
|
625
|
+
}
|
|
547
626
|
}
|
|
548
627
|
exports.PostHogCore = __webpack_exports__.PostHogCore;
|
|
549
628
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|