posthog-node 2.5.4 → 3.0.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/CHANGELOG.md +22 -0
- package/lib/index.cjs.js +196 -44
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +20 -9
- package/lib/index.esm.js +196 -44
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +7 -6
- package/lib/posthog-core/src/types.d.ts +2 -0
- package/lib/posthog-node/src/feature-flags.d.ts +2 -1
- package/lib/posthog-node/src/posthog-node.d.ts +9 -3
- package/lib/posthog-node/src/types.d.ts +14 -6
- package/package.json +1 -1
- package/src/feature-flags.ts +132 -9
- package/src/posthog-node.ts +42 -17
- package/src/types.ts +16 -6
- package/test/feature-flags.spec.ts +175 -2
- package/test/posthog-node.spec.ts +177 -11
package/lib/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ declare type PosthogCoreOptions = {
|
|
|
17
17
|
requestTimeout?: number;
|
|
18
18
|
sessionExpirationTimeSeconds?: number;
|
|
19
19
|
captureMode?: 'json' | 'form';
|
|
20
|
+
disableGeoip?: boolean;
|
|
20
21
|
};
|
|
21
22
|
declare enum PostHogPersistedProperty {
|
|
22
23
|
AnonymousId = "anonymous_id",
|
|
@@ -44,6 +45,7 @@ declare type PostHogFetchOptions = {
|
|
|
44
45
|
};
|
|
45
46
|
declare type PosthogCaptureOptions = {
|
|
46
47
|
timestamp?: Date;
|
|
48
|
+
disableGeoip?: boolean;
|
|
47
49
|
};
|
|
48
50
|
declare type PostHogFetchResponse = {
|
|
49
51
|
status: number;
|
|
@@ -105,6 +107,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
105
107
|
private removeDebugCallback?;
|
|
106
108
|
private debugMode;
|
|
107
109
|
private pendingPromises;
|
|
110
|
+
private disableGeoip;
|
|
108
111
|
private _optoutOverride;
|
|
109
112
|
protected _events: SimpleEventEmitter;
|
|
110
113
|
protected _flushTimer?: any;
|
|
@@ -132,7 +135,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
132
135
|
}, options?: PosthogCaptureOptions): this;
|
|
133
136
|
protected aliasStateless(alias: string, distinctId: string, properties?: {
|
|
134
137
|
[key: string]: any;
|
|
135
|
-
}): this;
|
|
138
|
+
}, options?: PosthogCaptureOptions): this;
|
|
136
139
|
/***
|
|
137
140
|
*** GROUPS
|
|
138
141
|
***/
|
|
@@ -141,12 +144,12 @@ declare abstract class PostHogCoreStateless {
|
|
|
141
144
|
*** FEATURE FLAGS
|
|
142
145
|
***/
|
|
143
146
|
protected getDecide(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>): Promise<PostHogDecideResponse | undefined>;
|
|
144
|
-
protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
145
|
-
protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
146
|
-
protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
147
|
+
protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<boolean | string | undefined>;
|
|
148
|
+
protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<JsonType | undefined>;
|
|
149
|
+
protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
|
|
147
150
|
protected _parsePayload(response: any): any;
|
|
148
|
-
protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
149
|
-
protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string
|
|
151
|
+
protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
|
|
152
|
+
protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
|
|
150
153
|
flags: PostHogDecideResponse['featureFlags'] | undefined;
|
|
151
154
|
payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
|
|
152
155
|
}>;
|
|
@@ -164,6 +167,7 @@ declare abstract class PostHogCoreStateless {
|
|
|
164
167
|
interface IdentifyMessageV1 {
|
|
165
168
|
distinctId: string;
|
|
166
169
|
properties?: Record<string | number, any>;
|
|
170
|
+
disableGeoip?: boolean;
|
|
167
171
|
}
|
|
168
172
|
interface EventMessageV1 extends IdentifyMessageV1 {
|
|
169
173
|
event: string;
|
|
@@ -176,6 +180,7 @@ interface GroupIdentifyMessage {
|
|
|
176
180
|
groupKey: string;
|
|
177
181
|
properties?: Record<string | number, any>;
|
|
178
182
|
distinctId?: string;
|
|
183
|
+
disableGeoip?: boolean;
|
|
179
184
|
}
|
|
180
185
|
declare type PostHogNodeV1 = {
|
|
181
186
|
/**
|
|
@@ -321,11 +326,12 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
321
326
|
enable(): void;
|
|
322
327
|
disable(): void;
|
|
323
328
|
debug(enabled?: boolean): void;
|
|
324
|
-
capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp }: EventMessageV1): void;
|
|
325
|
-
identify({ distinctId, properties }: IdentifyMessageV1): void;
|
|
329
|
+
capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip }: EventMessageV1): void;
|
|
330
|
+
identify({ distinctId, properties, disableGeoip }: IdentifyMessageV1): void;
|
|
326
331
|
alias(data: {
|
|
327
332
|
distinctId: string;
|
|
328
333
|
alias: string;
|
|
334
|
+
disableGeoip?: boolean;
|
|
329
335
|
}): void;
|
|
330
336
|
getFeatureFlag(key: string, distinctId: string, options?: {
|
|
331
337
|
groups?: Record<string, string>;
|
|
@@ -333,6 +339,7 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
333
339
|
groupProperties?: Record<string, Record<string, string>>;
|
|
334
340
|
onlyEvaluateLocally?: boolean;
|
|
335
341
|
sendFeatureFlagEvents?: boolean;
|
|
342
|
+
disableGeoip?: boolean;
|
|
336
343
|
}): Promise<string | boolean | undefined>;
|
|
337
344
|
getFeatureFlagPayload(key: string, distinctId: string, matchValue?: string | boolean, options?: {
|
|
338
345
|
groups?: Record<string, string>;
|
|
@@ -340,6 +347,7 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
340
347
|
groupProperties?: Record<string, Record<string, string>>;
|
|
341
348
|
onlyEvaluateLocally?: boolean;
|
|
342
349
|
sendFeatureFlagEvents?: boolean;
|
|
350
|
+
disableGeoip?: boolean;
|
|
343
351
|
}): Promise<JsonType | undefined>;
|
|
344
352
|
isFeatureEnabled(key: string, distinctId: string, options?: {
|
|
345
353
|
groups?: Record<string, string>;
|
|
@@ -347,20 +355,23 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
347
355
|
groupProperties?: Record<string, Record<string, string>>;
|
|
348
356
|
onlyEvaluateLocally?: boolean;
|
|
349
357
|
sendFeatureFlagEvents?: boolean;
|
|
358
|
+
disableGeoip?: boolean;
|
|
350
359
|
}): Promise<boolean | undefined>;
|
|
351
360
|
getAllFlags(distinctId: string, options?: {
|
|
352
361
|
groups?: Record<string, string>;
|
|
353
362
|
personProperties?: Record<string, string>;
|
|
354
363
|
groupProperties?: Record<string, Record<string, string>>;
|
|
355
364
|
onlyEvaluateLocally?: boolean;
|
|
365
|
+
disableGeoip?: boolean;
|
|
356
366
|
}): Promise<Record<string, string | boolean>>;
|
|
357
367
|
getAllFlagsAndPayloads(distinctId: string, options?: {
|
|
358
368
|
groups?: Record<string, string>;
|
|
359
369
|
personProperties?: Record<string, string>;
|
|
360
370
|
groupProperties?: Record<string, Record<string, string>>;
|
|
361
371
|
onlyEvaluateLocally?: boolean;
|
|
372
|
+
disableGeoip?: boolean;
|
|
362
373
|
}): Promise<PosthogFlagsAndPayloadsResponse>;
|
|
363
|
-
groupIdentify({ groupType, groupKey, properties, distinctId }: GroupIdentifyMessage): void;
|
|
374
|
+
groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }: GroupIdentifyMessage): void;
|
|
364
375
|
reloadFeatureFlags(): Promise<void>;
|
|
365
376
|
shutdown(): void;
|
|
366
377
|
shutdownAsync(): Promise<void>;
|
package/lib/index.esm.js
CHANGED
|
@@ -155,7 +155,7 @@ function __spreadArray(to, from, pack) {
|
|
|
155
155
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
var version = "
|
|
158
|
+
var version = "3.0.0";
|
|
159
159
|
|
|
160
160
|
var PostHogPersistedProperty;
|
|
161
161
|
(function (PostHogPersistedProperty) {
|
|
@@ -716,9 +716,10 @@ var SimpleEventEmitter = /** @class */ (function () {
|
|
|
716
716
|
|
|
717
717
|
var PostHogCoreStateless = /** @class */ (function () {
|
|
718
718
|
function PostHogCoreStateless(apiKey, options) {
|
|
719
|
-
var _a, _b, _c, _d;
|
|
719
|
+
var _a, _b, _c, _d, _e;
|
|
720
720
|
this.debugMode = false;
|
|
721
721
|
this.pendingPromises = {};
|
|
722
|
+
this.disableGeoip = true;
|
|
722
723
|
// internal
|
|
723
724
|
this._events = new SimpleEventEmitter();
|
|
724
725
|
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
@@ -734,6 +735,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
734
735
|
retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
|
|
735
736
|
};
|
|
736
737
|
this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
|
|
738
|
+
this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
|
|
737
739
|
}
|
|
738
740
|
PostHogCoreStateless.prototype.getCommonEventProperties = function () {
|
|
739
741
|
return {
|
|
@@ -793,13 +795,13 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
793
795
|
this.enqueue('capture', payload, options);
|
|
794
796
|
return this;
|
|
795
797
|
};
|
|
796
|
-
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties) {
|
|
798
|
+
PostHogCoreStateless.prototype.aliasStateless = function (alias, distinctId, properties, options) {
|
|
797
799
|
var payload = this.buildPayload({
|
|
798
800
|
event: '$create_alias',
|
|
799
801
|
distinct_id: distinctId,
|
|
800
802
|
properties: __assign(__assign({}, (properties || {})), { distinct_id: distinctId, alias: alias }),
|
|
801
803
|
});
|
|
802
|
-
this.enqueue('alias', payload);
|
|
804
|
+
this.enqueue('alias', payload, options);
|
|
803
805
|
return this;
|
|
804
806
|
};
|
|
805
807
|
/***
|
|
@@ -840,7 +842,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
840
842
|
});
|
|
841
843
|
});
|
|
842
844
|
};
|
|
843
|
-
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
845
|
+
PostHogCoreStateless.prototype.getFeatureFlagStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
844
846
|
if (groups === void 0) { groups = {}; }
|
|
845
847
|
if (personProperties === void 0) { personProperties = {}; }
|
|
846
848
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -848,7 +850,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
848
850
|
var featureFlags, response;
|
|
849
851
|
return __generator(this, function (_a) {
|
|
850
852
|
switch (_a.label) {
|
|
851
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
853
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
852
854
|
case 1:
|
|
853
855
|
featureFlags = _a.sent();
|
|
854
856
|
if (!featureFlags) {
|
|
@@ -867,7 +869,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
867
869
|
});
|
|
868
870
|
});
|
|
869
871
|
};
|
|
870
|
-
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties) {
|
|
872
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadStateless = function (key, distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
871
873
|
if (groups === void 0) { groups = {}; }
|
|
872
874
|
if (personProperties === void 0) { personProperties = {}; }
|
|
873
875
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -875,7 +877,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
875
877
|
var payloads, response;
|
|
876
878
|
return __generator(this, function (_a) {
|
|
877
879
|
switch (_a.label) {
|
|
878
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
880
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
879
881
|
case 1:
|
|
880
882
|
payloads = _a.sent();
|
|
881
883
|
if (!payloads) {
|
|
@@ -891,7 +893,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
891
893
|
});
|
|
892
894
|
});
|
|
893
895
|
};
|
|
894
|
-
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
896
|
+
PostHogCoreStateless.prototype.getFeatureFlagPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
895
897
|
if (groups === void 0) { groups = {}; }
|
|
896
898
|
if (personProperties === void 0) { personProperties = {}; }
|
|
897
899
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
@@ -900,7 +902,7 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
900
902
|
var _this = this;
|
|
901
903
|
return __generator(this, function (_a) {
|
|
902
904
|
switch (_a.label) {
|
|
903
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
905
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
904
906
|
case 1:
|
|
905
907
|
payloads = (_a.sent()).payloads;
|
|
906
908
|
if (payloads) {
|
|
@@ -922,28 +924,33 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
922
924
|
return response;
|
|
923
925
|
}
|
|
924
926
|
};
|
|
925
|
-
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
927
|
+
PostHogCoreStateless.prototype.getFeatureFlagsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
926
928
|
if (groups === void 0) { groups = {}; }
|
|
927
929
|
if (personProperties === void 0) { personProperties = {}; }
|
|
928
930
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
929
931
|
return __awaiter(this, void 0, void 0, function () {
|
|
930
932
|
return __generator(this, function (_a) {
|
|
931
933
|
switch (_a.label) {
|
|
932
|
-
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties)];
|
|
934
|
+
case 0: return [4 /*yield*/, this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
933
935
|
case 1: return [2 /*return*/, (_a.sent()).flags];
|
|
934
936
|
}
|
|
935
937
|
});
|
|
936
938
|
});
|
|
937
939
|
};
|
|
938
|
-
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties) {
|
|
940
|
+
PostHogCoreStateless.prototype.getFeatureFlagsAndPayloadsStateless = function (distinctId, groups, personProperties, groupProperties, disableGeoip) {
|
|
939
941
|
if (groups === void 0) { groups = {}; }
|
|
940
942
|
if (personProperties === void 0) { personProperties = {}; }
|
|
941
943
|
if (groupProperties === void 0) { groupProperties = {}; }
|
|
942
944
|
return __awaiter(this, void 0, void 0, function () {
|
|
943
|
-
var decideResponse, flags, payloads;
|
|
945
|
+
var extraPayload, decideResponse, flags, payloads;
|
|
944
946
|
return __generator(this, function (_a) {
|
|
945
947
|
switch (_a.label) {
|
|
946
|
-
case 0:
|
|
948
|
+
case 0:
|
|
949
|
+
extraPayload = {};
|
|
950
|
+
if (disableGeoip !== null && disableGeoip !== void 0 ? disableGeoip : this.disableGeoip) {
|
|
951
|
+
extraPayload['geoip_disable'] = true;
|
|
952
|
+
}
|
|
953
|
+
return [4 /*yield*/, this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload)];
|
|
947
954
|
case 1:
|
|
948
955
|
decideResponse = _a.sent();
|
|
949
956
|
flags = decideResponse === null || decideResponse === void 0 ? void 0 : decideResponse.featureFlags;
|
|
@@ -961,11 +968,19 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
961
968
|
***/
|
|
962
969
|
PostHogCoreStateless.prototype.enqueue = function (type, _message, options) {
|
|
963
970
|
var _this = this;
|
|
971
|
+
var _a;
|
|
964
972
|
if (this.optedOut) {
|
|
965
973
|
this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
|
|
966
974
|
return;
|
|
967
975
|
}
|
|
968
976
|
var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
|
|
977
|
+
var addGeoipDisableProperty = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : this.disableGeoip;
|
|
978
|
+
if (addGeoipDisableProperty) {
|
|
979
|
+
if (!message.properties) {
|
|
980
|
+
message.properties = {};
|
|
981
|
+
}
|
|
982
|
+
message['properties']['$geoip_disable'] = true;
|
|
983
|
+
}
|
|
969
984
|
if (message.distinctId) {
|
|
970
985
|
message.distinct_id = message.distinctId;
|
|
971
986
|
delete message.distinctId;
|
|
@@ -1092,11 +1107,13 @@ var PostHogCoreStateless = /** @class */ (function () {
|
|
|
1092
1107
|
__extends(PostHogCore, _super);
|
|
1093
1108
|
function PostHogCore(apiKey, options) {
|
|
1094
1109
|
var _this = this;
|
|
1095
|
-
var _a, _b;
|
|
1096
|
-
|
|
1110
|
+
var _a, _b, _c;
|
|
1111
|
+
// Default for stateful mode is to not disable geoip. Only override if explicitly set
|
|
1112
|
+
var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
|
|
1113
|
+
_this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
|
|
1097
1114
|
_this.flagCallReported = {};
|
|
1098
|
-
_this.sendFeatureFlagEvent = (
|
|
1099
|
-
_this._sessionExpirationTimeSeconds = (
|
|
1115
|
+
_this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
|
|
1116
|
+
_this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
|
|
1100
1117
|
// NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
|
|
1101
1118
|
if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
|
|
1102
1119
|
safeSetTimeout(function () {
|
|
@@ -1598,6 +1615,7 @@ function () {
|
|
|
1598
1615
|
this.featureFlags = [];
|
|
1599
1616
|
this.featureFlagsByKey = {};
|
|
1600
1617
|
this.groupTypeMapping = {};
|
|
1618
|
+
this.cohorts = {};
|
|
1601
1619
|
this.loadedSuccessfullyOnce = false;
|
|
1602
1620
|
this.timeout = timeout;
|
|
1603
1621
|
this.projectApiKey = projectApiKey;
|
|
@@ -1935,14 +1953,23 @@ function () {
|
|
|
1935
1953
|
var rolloutPercentage = condition.rollout_percentage;
|
|
1936
1954
|
|
|
1937
1955
|
if ((condition.properties || []).length > 0) {
|
|
1938
|
-
var
|
|
1939
|
-
|
|
1940
|
-
|
|
1956
|
+
for (var _i = 0, _a = condition.properties; _i < _a.length; _i++) {
|
|
1957
|
+
var prop = _a[_i];
|
|
1958
|
+
var propertyType = prop.type;
|
|
1959
|
+
var matches = false;
|
|
1941
1960
|
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1961
|
+
if (propertyType === 'cohort') {
|
|
1962
|
+
matches = matchCohort(prop, properties, this.cohorts);
|
|
1963
|
+
} else {
|
|
1964
|
+
matches = matchProperty(prop, properties);
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
if (!matches) {
|
|
1968
|
+
return false;
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
if (rolloutPercentage == undefined) {
|
|
1946
1973
|
return true;
|
|
1947
1974
|
}
|
|
1948
1975
|
}
|
|
@@ -2075,6 +2102,7 @@ function () {
|
|
|
2075
2102
|
return acc[curr.key] = curr, acc;
|
|
2076
2103
|
}, {});
|
|
2077
2104
|
this.groupTypeMapping = responseJson.group_type_mapping || {};
|
|
2105
|
+
this.cohorts = responseJson.cohorts || [];
|
|
2078
2106
|
this.loadedSuccessfullyOnce = true;
|
|
2079
2107
|
return [3
|
|
2080
2108
|
/*break*/
|
|
@@ -2107,7 +2135,7 @@ function () {
|
|
|
2107
2135
|
return __generator(this, function (_a) {
|
|
2108
2136
|
switch (_a.label) {
|
|
2109
2137
|
case 0:
|
|
2110
|
-
url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey);
|
|
2138
|
+
url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey, "&send_cohorts");
|
|
2111
2139
|
options = {
|
|
2112
2140
|
method: 'GET',
|
|
2113
2141
|
headers: {
|
|
@@ -2244,6 +2272,119 @@ function matchProperty(property, propertyValues) {
|
|
|
2244
2272
|
}
|
|
2245
2273
|
}
|
|
2246
2274
|
|
|
2275
|
+
function matchCohort(property, propertyValues, cohortProperties) {
|
|
2276
|
+
var cohortId = String(property.value);
|
|
2277
|
+
|
|
2278
|
+
if (!(cohortId in cohortProperties)) {
|
|
2279
|
+
throw new InconclusiveMatchError("can't match cohort without a given cohort property value");
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
var propertyGroup = cohortProperties[cohortId];
|
|
2283
|
+
return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties);
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
|
|
2287
|
+
if (!propertyGroup) {
|
|
2288
|
+
return true;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
var propertyGroupType = propertyGroup.type;
|
|
2292
|
+
var properties = propertyGroup.values;
|
|
2293
|
+
|
|
2294
|
+
if (!properties || properties.length === 0) {
|
|
2295
|
+
// empty groups are no-ops, always match
|
|
2296
|
+
return true;
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
var errorMatchingLocally = false;
|
|
2300
|
+
|
|
2301
|
+
if ('values' in properties[0]) {
|
|
2302
|
+
// a nested property group
|
|
2303
|
+
for (var _i = 0, _a = properties; _i < _a.length; _i++) {
|
|
2304
|
+
var prop = _a[_i];
|
|
2305
|
+
|
|
2306
|
+
try {
|
|
2307
|
+
var matches = matchPropertyGroup(prop, propertyValues, cohortProperties);
|
|
2308
|
+
|
|
2309
|
+
if (propertyGroupType === 'AND') {
|
|
2310
|
+
if (!matches) {
|
|
2311
|
+
return false;
|
|
2312
|
+
}
|
|
2313
|
+
} else {
|
|
2314
|
+
// OR group
|
|
2315
|
+
if (matches) {
|
|
2316
|
+
return true;
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
} catch (err) {
|
|
2320
|
+
if (err instanceof InconclusiveMatchError) {
|
|
2321
|
+
console.debug("Failed to compute property ".concat(prop, " locally: ").concat(err));
|
|
2322
|
+
errorMatchingLocally = true;
|
|
2323
|
+
} else {
|
|
2324
|
+
throw err;
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
|
|
2329
|
+
if (errorMatchingLocally) {
|
|
2330
|
+
throw new InconclusiveMatchError("Can't match cohort without a given cohort property value");
|
|
2331
|
+
} // if we get here, all matched in AND case, or none matched in OR case
|
|
2332
|
+
|
|
2333
|
+
|
|
2334
|
+
return propertyGroupType === 'AND';
|
|
2335
|
+
} else {
|
|
2336
|
+
for (var _b = 0, _c = properties; _b < _c.length; _b++) {
|
|
2337
|
+
var prop = _c[_b];
|
|
2338
|
+
|
|
2339
|
+
try {
|
|
2340
|
+
var matches = void 0;
|
|
2341
|
+
|
|
2342
|
+
if (prop.type === 'cohort') {
|
|
2343
|
+
matches = matchCohort(prop, propertyValues, cohortProperties);
|
|
2344
|
+
} else {
|
|
2345
|
+
matches = matchProperty(prop, propertyValues);
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
var negation = prop.negation || false;
|
|
2349
|
+
|
|
2350
|
+
if (propertyGroupType === 'AND') {
|
|
2351
|
+
// if negated property, do the inverse
|
|
2352
|
+
if (!matches && !negation) {
|
|
2353
|
+
return false;
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
if (matches && negation) {
|
|
2357
|
+
return false;
|
|
2358
|
+
}
|
|
2359
|
+
} else {
|
|
2360
|
+
// OR group
|
|
2361
|
+
if (matches && !negation) {
|
|
2362
|
+
return true;
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
if (!matches && negation) {
|
|
2366
|
+
return true;
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
} catch (err) {
|
|
2370
|
+
if (err instanceof InconclusiveMatchError) {
|
|
2371
|
+
console.debug("Failed to compute property ".concat(prop, " locally: ").concat(err));
|
|
2372
|
+
errorMatchingLocally = true;
|
|
2373
|
+
} else {
|
|
2374
|
+
throw err;
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
if (errorMatchingLocally) {
|
|
2380
|
+
throw new InconclusiveMatchError("can't match cohort without a given cohort property value");
|
|
2381
|
+
} // if we get here, all matched in AND case, or none matched in OR case
|
|
2382
|
+
|
|
2383
|
+
|
|
2384
|
+
return propertyGroupType === 'AND';
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2247
2388
|
function isValidRegex(regex) {
|
|
2248
2389
|
try {
|
|
2249
2390
|
new RegExp(regex);
|
|
@@ -2359,16 +2500,18 @@ function (_super) {
|
|
|
2359
2500
|
properties = _a.properties,
|
|
2360
2501
|
groups = _a.groups,
|
|
2361
2502
|
sendFeatureFlags = _a.sendFeatureFlags,
|
|
2362
|
-
timestamp = _a.timestamp
|
|
2503
|
+
timestamp = _a.timestamp,
|
|
2504
|
+
disableGeoip = _a.disableGeoip;
|
|
2363
2505
|
|
|
2364
2506
|
var _capture = function (props) {
|
|
2365
2507
|
_super.prototype.captureStateless.call(_this, distinctId, event, props, {
|
|
2366
|
-
timestamp: timestamp
|
|
2508
|
+
timestamp: timestamp,
|
|
2509
|
+
disableGeoip: disableGeoip
|
|
2367
2510
|
});
|
|
2368
2511
|
};
|
|
2369
2512
|
|
|
2370
2513
|
if (sendFeatureFlags) {
|
|
2371
|
-
_super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups).then(function (flags) {
|
|
2514
|
+
_super.prototype.getFeatureFlagsStateless.call(this, distinctId, groups, undefined, undefined, disableGeoip).then(function (flags) {
|
|
2372
2515
|
var featureVariantProperties = {};
|
|
2373
2516
|
|
|
2374
2517
|
if (flags) {
|
|
@@ -2404,29 +2547,34 @@ function (_super) {
|
|
|
2404
2547
|
|
|
2405
2548
|
PostHog.prototype.identify = function (_a) {
|
|
2406
2549
|
var distinctId = _a.distinctId,
|
|
2407
|
-
properties = _a.properties
|
|
2550
|
+
properties = _a.properties,
|
|
2551
|
+
disableGeoip = _a.disableGeoip; // Catch properties passed as $set and move them to the top level
|
|
2408
2552
|
|
|
2409
2553
|
var personProperties = (properties === null || properties === void 0 ? void 0 : properties.$set) || properties;
|
|
2410
2554
|
|
|
2411
2555
|
_super.prototype.identifyStateless.call(this, distinctId, {
|
|
2412
2556
|
$set: personProperties
|
|
2557
|
+
}, {
|
|
2558
|
+
disableGeoip: disableGeoip
|
|
2413
2559
|
});
|
|
2414
2560
|
};
|
|
2415
2561
|
|
|
2416
2562
|
PostHog.prototype.alias = function (data) {
|
|
2417
|
-
_super.prototype.aliasStateless.call(this, data.alias, data.distinctId
|
|
2563
|
+
_super.prototype.aliasStateless.call(this, data.alias, data.distinctId, undefined, {
|
|
2564
|
+
disableGeoip: data.disableGeoip
|
|
2565
|
+
});
|
|
2418
2566
|
};
|
|
2419
2567
|
|
|
2420
2568
|
PostHog.prototype.getFeatureFlag = function (key, distinctId, options) {
|
|
2421
2569
|
var _a;
|
|
2422
2570
|
|
|
2423
2571
|
return __awaiter(this, void 0, void 0, function () {
|
|
2424
|
-
var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
|
|
2572
|
+
var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
|
|
2425
2573
|
|
|
2426
2574
|
return __generator(this, function (_d) {
|
|
2427
2575
|
switch (_d.label) {
|
|
2428
2576
|
case 0:
|
|
2429
|
-
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
|
|
2577
|
+
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
|
|
2430
2578
|
_c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, sendFeatureFlagEvents = _c.sendFeatureFlagEvents; // set defaults
|
|
2431
2579
|
|
|
2432
2580
|
if (onlyEvaluateLocally == undefined) {
|
|
@@ -2449,7 +2597,7 @@ function (_super) {
|
|
|
2449
2597
|
, 3];
|
|
2450
2598
|
return [4
|
|
2451
2599
|
/*yield*/
|
|
2452
|
-
, _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
|
|
2600
|
+
, _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
2453
2601
|
|
|
2454
2602
|
case 2:
|
|
2455
2603
|
response = _d.sent();
|
|
@@ -2477,7 +2625,8 @@ function (_super) {
|
|
|
2477
2625
|
$feature_flag_response: response,
|
|
2478
2626
|
locally_evaluated: flagWasLocallyEvaluated
|
|
2479
2627
|
},
|
|
2480
|
-
groups: groups
|
|
2628
|
+
groups: groups,
|
|
2629
|
+
disableGeoip: disableGeoip
|
|
2481
2630
|
});
|
|
2482
2631
|
}
|
|
2483
2632
|
|
|
@@ -2493,12 +2642,12 @@ function (_super) {
|
|
|
2493
2642
|
var _a;
|
|
2494
2643
|
|
|
2495
2644
|
return __awaiter(this, void 0, void 0, function () {
|
|
2496
|
-
var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
|
|
2645
|
+
var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
|
|
2497
2646
|
|
|
2498
2647
|
return __generator(this, function (_d) {
|
|
2499
2648
|
switch (_d.label) {
|
|
2500
2649
|
case 0:
|
|
2501
|
-
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
|
|
2650
|
+
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
|
|
2502
2651
|
_c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, _c.sendFeatureFlagEvents;
|
|
2503
2652
|
response = undefined;
|
|
2504
2653
|
if (!!matchValue) return [3
|
|
@@ -2543,7 +2692,7 @@ function (_super) {
|
|
|
2543
2692
|
, 6];
|
|
2544
2693
|
return [4
|
|
2545
2694
|
/*yield*/
|
|
2546
|
-
, _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties)];
|
|
2695
|
+
, _super.prototype.getFeatureFlagPayloadStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
2547
2696
|
|
|
2548
2697
|
case 5:
|
|
2549
2698
|
response = _d.sent();
|
|
@@ -2619,12 +2768,12 @@ function (_super) {
|
|
|
2619
2768
|
var _a;
|
|
2620
2769
|
|
|
2621
2770
|
return __awaiter(this, void 0, void 0, function () {
|
|
2622
|
-
var _b, groups, personProperties, groupProperties, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
|
|
2771
|
+
var _b, groups, personProperties, groupProperties, disableGeoip, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
|
|
2623
2772
|
|
|
2624
2773
|
return __generator(this, function (_c) {
|
|
2625
2774
|
switch (_c.label) {
|
|
2626
2775
|
case 0:
|
|
2627
|
-
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
|
|
2776
|
+
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
|
|
2628
2777
|
onlyEvaluateLocally = (options || {}).onlyEvaluateLocally; // set defaults
|
|
2629
2778
|
|
|
2630
2779
|
if (onlyEvaluateLocally == undefined) {
|
|
@@ -2652,7 +2801,7 @@ function (_super) {
|
|
|
2652
2801
|
, 3];
|
|
2653
2802
|
return [4
|
|
2654
2803
|
/*yield*/
|
|
2655
|
-
, _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties)];
|
|
2804
|
+
, _super.prototype.getFeatureFlagsAndPayloadsStateless.call(this, distinctId, groups, personProperties, groupProperties, disableGeoip)];
|
|
2656
2805
|
|
|
2657
2806
|
case 2:
|
|
2658
2807
|
remoteEvaluationResult = _c.sent();
|
|
@@ -2676,9 +2825,12 @@ function (_super) {
|
|
|
2676
2825
|
var groupType = _a.groupType,
|
|
2677
2826
|
groupKey = _a.groupKey,
|
|
2678
2827
|
properties = _a.properties,
|
|
2679
|
-
distinctId = _a.distinctId
|
|
2828
|
+
distinctId = _a.distinctId,
|
|
2829
|
+
disableGeoip = _a.disableGeoip;
|
|
2680
2830
|
|
|
2681
|
-
_super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties,
|
|
2831
|
+
_super.prototype.groupIdentifyStateless.call(this, groupType, groupKey, properties, {
|
|
2832
|
+
disableGeoip: disableGeoip
|
|
2833
|
+
}, distinctId);
|
|
2682
2834
|
};
|
|
2683
2835
|
|
|
2684
2836
|
PostHog.prototype.reloadFeatureFlags = function () {
|