posthog-node 5.71.0 → 5.73.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/client.d.ts +53 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +94 -19
- package/dist/client.mjs +94 -19
- package/dist/emails/attachments.d.ts +23 -0
- package/dist/emails/attachments.d.ts.map +1 -0
- package/dist/emails/attachments.js +170 -0
- package/dist/emails/attachments.mjs +123 -0
- package/dist/emails/emails.d.ts +7 -2
- package/dist/emails/emails.d.ts.map +1 -1
- package/dist/emails/emails.js +193 -37
- package/dist/emails/emails.mjs +193 -37
- package/dist/emails/errors.d.ts +1 -1
- package/dist/emails/errors.d.ts.map +1 -1
- package/dist/emails/errors.js +6 -1
- package/dist/emails/errors.mjs +6 -1
- package/dist/emails/types.d.ts +23 -0
- package/dist/emails/types.d.ts.map +1 -1
- package/dist/exports.d.ts +1 -1
- package/dist/exports.d.ts.map +1 -1
- package/dist/feature-flag-evaluations.d.ts +14 -0
- package/dist/feature-flag-evaluations.d.ts.map +1 -1
- package/dist/feature-flag-evaluations.js +2 -1
- package/dist/feature-flag-evaluations.mjs +2 -1
- package/dist/flag-events.d.ts +112 -0
- package/dist/flag-events.d.ts.map +1 -0
- package/dist/flag-events.js +230 -0
- package/dist/flag-events.mjs +174 -0
- package/dist/flags-response.d.ts +83 -0
- package/dist/flags-response.d.ts.map +1 -0
- package/dist/flags-response.js +255 -0
- package/dist/flags-response.mjs +208 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +4 -3
- package/src/client.ts +238 -45
- package/src/emails/attachments.ts +209 -0
- package/src/emails/emails.ts +243 -35
- package/src/emails/errors.ts +7 -2
- package/src/emails/types.ts +24 -0
- package/src/exports.ts +1 -0
- package/src/feature-flag-evaluations.ts +17 -0
- package/src/flag-events.ts +280 -0
- package/src/flags-response.ts +340 -0
- package/src/types.ts +2 -1
- package/src/version.ts +1 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { FeatureFlagValue } from '@posthog/core';
|
|
2
|
+
import type { ParsedFlagRecord } from './flags-response';
|
|
3
|
+
/**
|
|
4
|
+
* The flag events of one flag read: `$feature_flag_called` and, for a config version 2 experiment
|
|
5
|
+
* split, `$experiment_exposure`.
|
|
6
|
+
*
|
|
7
|
+
* {@link buildConfigVersion2FlagEvents} builds both events and their dedupe keys in one step, from
|
|
8
|
+
* the parsed `/flags` record that supplied the read's value and the read's `$feature_flag_called`
|
|
9
|
+
* properties. It never reads the value the read returned, and it never evaluates or fetches flags.
|
|
10
|
+
* A read whose record is not a readable config version 2 record gets no events from it, and the
|
|
11
|
+
* client sends its `$feature_flag_called` event exactly as before.
|
|
12
|
+
*/
|
|
13
|
+
export declare const FEATURE_FLAG_CALLED_EVENT = "$feature_flag_called";
|
|
14
|
+
export declare const EXPERIMENT_EXPOSURE_EVENT = "$experiment_exposure";
|
|
15
|
+
/** The attribution a config version 2 `$feature_flag_called` carries, and keeps when minimized. */
|
|
16
|
+
export declare const CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES: readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* What a config version 2 flag record reports on its flag events.
|
|
19
|
+
*
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export type RuleAttribution = {
|
|
23
|
+
readonly reasonCode: string | undefined;
|
|
24
|
+
readonly ruleId: string | undefined;
|
|
25
|
+
readonly ruleType: string | undefined;
|
|
26
|
+
/** Set only for a complete experiment split, the outcome that sends an exposure. */
|
|
27
|
+
readonly variantKey: string | undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Reads the event attribution of a config version 2 flag record.
|
|
31
|
+
*
|
|
32
|
+
* Returns `undefined` for anything else: no record, an unreadable record, a config version 1
|
|
33
|
+
* record, or a record from a server without typed values.
|
|
34
|
+
*
|
|
35
|
+
* The config version and reason code are always reported. The rule ID and rule type are reported
|
|
36
|
+
* together or not at all, only for a targeting match, a rollout miss, a paused experiment, a
|
|
37
|
+
* holdout and a complete experiment split, and only with a rule type the reason code allows. A
|
|
38
|
+
* record with no rule ID, no rule type or another rule type, such as a targeting match that names
|
|
39
|
+
* an experiment rule, reports neither. A rule ID that is not a UUID counts as missing. A failed
|
|
40
|
+
* record, any other reason code (including one this SDK does not know) and a record without a
|
|
41
|
+
* reason code report neither. The variant key is reported only for a complete experiment split:
|
|
42
|
+
* reason `experiment_split`, rule type `experiment`, a rule ID and a variant key (any string,
|
|
43
|
+
* including an empty one). A split without all of them is malformed: it reports its reason but no
|
|
44
|
+
* rule ID, rule type or variant, and sends no exposure. The SDK never guesses the missing fields
|
|
45
|
+
* from the value, the flag key or an earlier result.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export declare function ruleAttribution(record: ParsedFlagRecord | undefined): RuleAttribution | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* How the client captures a config version 2 flag event, after super and context properties are
|
|
52
|
+
* merged: `removed` names are dropped, then `pinned` properties are set. A super property can
|
|
53
|
+
* therefore neither change the event's attribution nor add a field the event never carries.
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
export type FlagEventCapture = {
|
|
58
|
+
readonly removed: ReadonlySet<string>;
|
|
59
|
+
readonly pinned: Readonly<Record<string, unknown>>;
|
|
60
|
+
};
|
|
61
|
+
/** @internal */
|
|
62
|
+
export declare function applyFlagEventCapture(properties: Record<string, any>, capture: FlagEventCapture): Record<string, any>;
|
|
63
|
+
/**
|
|
64
|
+
* The minimal property set of a config version 2 `$feature_flag_called`: the shared allowlist plus
|
|
65
|
+
* the config version 2 attribution. A config version 1 event keeps the shared allowlist alone.
|
|
66
|
+
*
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
export declare function minimizeConfigVersion2FlagCalledProperties(properties: Record<string, any>): Record<string, any>;
|
|
70
|
+
/**
|
|
71
|
+
* One flag event of a read, sent once per key in its own dedupe namespace.
|
|
72
|
+
*
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
export type FlagEvent = {
|
|
76
|
+
readonly event: typeof FEATURE_FLAG_CALLED_EVENT | typeof EXPERIMENT_EXPOSURE_EVENT;
|
|
77
|
+
readonly dedupeKey: string;
|
|
78
|
+
readonly properties: Record<string, any>;
|
|
79
|
+
readonly capture: FlagEventCapture;
|
|
80
|
+
};
|
|
81
|
+
/** @internal */
|
|
82
|
+
export type ConfigVersion2FlagEvents = {
|
|
83
|
+
readonly flagCalled: FlagEvent;
|
|
84
|
+
/** Present only for a complete experiment split whose record supplied the read's value. */
|
|
85
|
+
readonly exposure: FlagEvent | undefined;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Builds the flag events of a read whose value a config version 2 record supplied.
|
|
89
|
+
*
|
|
90
|
+
* Returns `undefined` when `record` has no config version 2 attribution (see
|
|
91
|
+
* {@link ruleAttribution}), including when it is absent because the value came from local
|
|
92
|
+
* evaluation, an override or no flag at all. The caller then sends its `$feature_flag_called` as
|
|
93
|
+
* before.
|
|
94
|
+
*
|
|
95
|
+
* `$feature_flag_called` gets the record's attribution in place of the human-readable
|
|
96
|
+
* `$feature_flag_reason`. Its dedupe key is the flag key, the response, the groups, the config
|
|
97
|
+
* version and the attribution, so equal-valued A/A variants, successive experiments of one flag
|
|
98
|
+
* and a holdout followed by a split of the same rule are each reported. `$experiment_exposure`
|
|
99
|
+
* is built for a complete experiment split only. Its dedupe key is the groups, the flag key, the
|
|
100
|
+
* config version, the rule ID and the variant key, so one assignment sends one exposure however
|
|
101
|
+
* often it is read. The value, request ID and evaluation time are left out of it.
|
|
102
|
+
*
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
export declare function buildConfigVersion2FlagEvents(params: {
|
|
106
|
+
key: string;
|
|
107
|
+
response: FeatureFlagValue | undefined;
|
|
108
|
+
groups: Record<string, string | number> | undefined;
|
|
109
|
+
properties: Record<string, any>;
|
|
110
|
+
record: ParsedFlagRecord | undefined;
|
|
111
|
+
}): ConfigVersion2FlagEvents | undefined;
|
|
112
|
+
//# sourceMappingURL=flag-events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flag-events.d.ts","sourceRoot":"","sources":["../src/flag-events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAqC,MAAM,eAAe,CAAA;AAEnF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAExD;;;;;;;;;GASG;AAEH,eAAO,MAAM,yBAAyB,yBAAyB,CAAA;AAC/D,eAAO,MAAM,yBAAyB,yBAAyB,CAAA;AAkB/D,mGAAmG;AACnG,eAAO,MAAM,uCAAuC,EAAE,SAAS,MAAM,EAMpE,CAAA;AAkCD;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;IACrC,oFAAoF;IACpF,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;CACxC,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,SAAS,GAAG,eAAe,GAAG,SAAS,CAsBjG;AAED;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;IACrC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;CACnD,CAAA;AAED,gBAAgB;AAChB,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,EAAE,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAGrH;AAED;;;;;GAKG;AACH,wBAAgB,0CAA0C,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAQ/G;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,yBAAyB,GAAG,OAAO,yBAAyB,CAAA;IACnF,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACxC,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAA;CACnC,CAAA;AAED,gBAAgB;AAChB,MAAM,MAAM,wBAAwB,GAAG;IACrC,QAAQ,CAAC,UAAU,EAAE,SAAS,CAAA;IAC9B,2FAA2F;IAC3F,QAAQ,CAAC,QAAQ,EAAE,SAAS,GAAG,SAAS,CAAA;CACzC,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE;IACpD,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACtC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,SAAS,CAAA;IACnD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC/B,MAAM,EAAE,gBAAgB,GAAG,SAAS,CAAA;CACrC,GAAG,wBAAwB,GAAG,SAAS,CAsEvC"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES: ()=>CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES,
|
|
32
|
+
EXPERIMENT_EXPOSURE_EVENT: ()=>EXPERIMENT_EXPOSURE_EVENT,
|
|
33
|
+
FEATURE_FLAG_CALLED_EVENT: ()=>FEATURE_FLAG_CALLED_EVENT,
|
|
34
|
+
applyFlagEventCapture: ()=>applyFlagEventCapture,
|
|
35
|
+
buildConfigVersion2FlagEvents: ()=>buildConfigVersion2FlagEvents,
|
|
36
|
+
minimizeConfigVersion2FlagCalledProperties: ()=>minimizeConfigVersion2FlagCalledProperties,
|
|
37
|
+
ruleAttribution: ()=>ruleAttribution
|
|
38
|
+
});
|
|
39
|
+
const core_namespaceObject = require("@posthog/core");
|
|
40
|
+
const FEATURE_FLAG_CALLED_EVENT = '$feature_flag_called';
|
|
41
|
+
const EXPERIMENT_EXPOSURE_EVENT = '$experiment_exposure';
|
|
42
|
+
const EXPERIMENT_SPLIT_REASON = 'experiment_split';
|
|
43
|
+
const EXPERIMENT_RULE_TYPE = 'experiment';
|
|
44
|
+
const RULE_TYPES_BY_REASON_CODE = new Map([
|
|
45
|
+
[
|
|
46
|
+
'targeting_match',
|
|
47
|
+
new Set([
|
|
48
|
+
'targeted_release',
|
|
49
|
+
'percentage_rollout'
|
|
50
|
+
])
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
EXPERIMENT_SPLIT_REASON,
|
|
54
|
+
new Set([
|
|
55
|
+
EXPERIMENT_RULE_TYPE
|
|
56
|
+
])
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
'rollout_miss',
|
|
60
|
+
new Set([
|
|
61
|
+
'percentage_rollout',
|
|
62
|
+
EXPERIMENT_RULE_TYPE
|
|
63
|
+
])
|
|
64
|
+
],
|
|
65
|
+
[
|
|
66
|
+
'experiment_paused',
|
|
67
|
+
new Set([
|
|
68
|
+
EXPERIMENT_RULE_TYPE
|
|
69
|
+
])
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
'holdout',
|
|
73
|
+
new Set([
|
|
74
|
+
EXPERIMENT_RULE_TYPE
|
|
75
|
+
])
|
|
76
|
+
]
|
|
77
|
+
]);
|
|
78
|
+
const CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES = [
|
|
79
|
+
'$feature_flag_config_version',
|
|
80
|
+
'$feature_flag_reason_code',
|
|
81
|
+
'$feature_flag_rule_id',
|
|
82
|
+
'$feature_flag_rule_type',
|
|
83
|
+
'$feature_flag_variant_key'
|
|
84
|
+
];
|
|
85
|
+
const CONFIG_VERSION_2_FLAG_CALLED_REMOVED = new Set([
|
|
86
|
+
...CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES,
|
|
87
|
+
'$feature_flag_reason',
|
|
88
|
+
'$feature_flag_forced_variant',
|
|
89
|
+
'$feature_flag_experiment_id'
|
|
90
|
+
]);
|
|
91
|
+
const EXPOSURE_REMOVED = new Set([
|
|
92
|
+
'$feature_flag_config_version',
|
|
93
|
+
'$feature_flag_reason_code',
|
|
94
|
+
'$feature_flag_rule_type',
|
|
95
|
+
'$experiment_exposure_source',
|
|
96
|
+
'$feature_flag_experiment_id',
|
|
97
|
+
'$feature_flag_forced_variant'
|
|
98
|
+
]);
|
|
99
|
+
const EXPOSURE_PROPERTIES_FROM_FLAG_CALLED = [
|
|
100
|
+
'$feature_flag_id',
|
|
101
|
+
'$feature_flag_version',
|
|
102
|
+
'$feature_flag_request_id',
|
|
103
|
+
'$feature_flag_evaluated_at'
|
|
104
|
+
];
|
|
105
|
+
const UUID_PATTERN = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
106
|
+
function ruleAttribution(record) {
|
|
107
|
+
if (void 0 === record || void 0 !== record.unreadableField || 2 !== record.metadata.configVersion) return;
|
|
108
|
+
const reasonCode = record.reason?.code || void 0;
|
|
109
|
+
const noRule = {
|
|
110
|
+
reasonCode,
|
|
111
|
+
ruleId: void 0,
|
|
112
|
+
ruleType: void 0,
|
|
113
|
+
variantKey: void 0
|
|
114
|
+
};
|
|
115
|
+
const allowedRuleTypes = void 0 === reasonCode ? void 0 : RULE_TYPES_BY_REASON_CODE.get(reasonCode);
|
|
116
|
+
if (record.failed || void 0 === allowedRuleTypes) return noRule;
|
|
117
|
+
const { ruleId, ruleType, variantKey } = record.metadata;
|
|
118
|
+
if (void 0 === ruleId || !UUID_PATTERN.test(ruleId) || void 0 === ruleType || !allowedRuleTypes.has(ruleType)) return noRule;
|
|
119
|
+
if (reasonCode !== EXPERIMENT_SPLIT_REASON) return {
|
|
120
|
+
...noRule,
|
|
121
|
+
ruleId,
|
|
122
|
+
ruleType
|
|
123
|
+
};
|
|
124
|
+
if (void 0 === variantKey) return noRule;
|
|
125
|
+
return {
|
|
126
|
+
reasonCode,
|
|
127
|
+
ruleId,
|
|
128
|
+
ruleType,
|
|
129
|
+
variantKey
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function applyFlagEventCapture(properties, capture) {
|
|
133
|
+
const kept = Object.fromEntries(Object.entries(properties).filter(([name])=>!capture.removed.has(name)));
|
|
134
|
+
return {
|
|
135
|
+
...kept,
|
|
136
|
+
...capture.pinned
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function minimizeConfigVersion2FlagCalledProperties(properties) {
|
|
140
|
+
const minimal = (0, core_namespaceObject.minimizeFlagCalledEventProperties)(properties);
|
|
141
|
+
for (const name of CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES)if (void 0 !== properties[name]) minimal[name] = properties[name];
|
|
142
|
+
return minimal;
|
|
143
|
+
}
|
|
144
|
+
function buildConfigVersion2FlagEvents(params) {
|
|
145
|
+
const { key, response, groups, record } = params;
|
|
146
|
+
const attribution = ruleAttribution(record);
|
|
147
|
+
if (void 0 === attribution) return;
|
|
148
|
+
const { reasonCode, ruleId, ruleType, variantKey } = attribution;
|
|
149
|
+
const groupEntries = Object.entries(groups ?? {}).sort(([a], [b])=>a < b ? -1 : a > b ? 1 : 0);
|
|
150
|
+
const attributionProperties = {
|
|
151
|
+
$feature_flag_config_version: 2
|
|
152
|
+
};
|
|
153
|
+
if (void 0 !== reasonCode) attributionProperties.$feature_flag_reason_code = reasonCode;
|
|
154
|
+
if (void 0 !== ruleId) {
|
|
155
|
+
attributionProperties.$feature_flag_rule_id = ruleId;
|
|
156
|
+
attributionProperties.$feature_flag_rule_type = ruleType;
|
|
157
|
+
}
|
|
158
|
+
if (void 0 !== variantKey) attributionProperties.$feature_flag_variant_key = variantKey;
|
|
159
|
+
const { $feature_flag_reason: _reason, ...flagCalledProperties } = params.properties;
|
|
160
|
+
Object.assign(flagCalledProperties, attributionProperties);
|
|
161
|
+
const flagCalled = {
|
|
162
|
+
event: FEATURE_FLAG_CALLED_EVENT,
|
|
163
|
+
dedupeKey: JSON.stringify([
|
|
164
|
+
key,
|
|
165
|
+
response ?? null,
|
|
166
|
+
groupEntries,
|
|
167
|
+
2,
|
|
168
|
+
reasonCode ?? null,
|
|
169
|
+
ruleId ?? null,
|
|
170
|
+
ruleType ?? null,
|
|
171
|
+
variantKey ?? null
|
|
172
|
+
]),
|
|
173
|
+
properties: flagCalledProperties,
|
|
174
|
+
capture: {
|
|
175
|
+
removed: CONFIG_VERSION_2_FLAG_CALLED_REMOVED,
|
|
176
|
+
pinned: attributionProperties
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
if (void 0 === variantKey) return {
|
|
180
|
+
flagCalled,
|
|
181
|
+
exposure: void 0
|
|
182
|
+
};
|
|
183
|
+
const exposureIdentity = {
|
|
184
|
+
$feature_flag: key,
|
|
185
|
+
$feature_flag_response: response,
|
|
186
|
+
$feature_flag_variant_key: variantKey,
|
|
187
|
+
$feature_flag_rule_id: ruleId,
|
|
188
|
+
locally_evaluated: true === flagCalledProperties.locally_evaluated
|
|
189
|
+
};
|
|
190
|
+
const exposureProperties = {};
|
|
191
|
+
for (const name of EXPOSURE_PROPERTIES_FROM_FLAG_CALLED)if (void 0 !== flagCalledProperties[name]) exposureProperties[name] = flagCalledProperties[name];
|
|
192
|
+
Object.assign(exposureProperties, exposureIdentity);
|
|
193
|
+
return {
|
|
194
|
+
flagCalled,
|
|
195
|
+
exposure: {
|
|
196
|
+
event: EXPERIMENT_EXPOSURE_EVENT,
|
|
197
|
+
dedupeKey: JSON.stringify([
|
|
198
|
+
key,
|
|
199
|
+
groupEntries,
|
|
200
|
+
2,
|
|
201
|
+
ruleId,
|
|
202
|
+
variantKey
|
|
203
|
+
]),
|
|
204
|
+
properties: exposureProperties,
|
|
205
|
+
capture: {
|
|
206
|
+
removed: EXPOSURE_REMOVED,
|
|
207
|
+
pinned: exposureIdentity
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
exports.CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES = __webpack_exports__.CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES;
|
|
213
|
+
exports.EXPERIMENT_EXPOSURE_EVENT = __webpack_exports__.EXPERIMENT_EXPOSURE_EVENT;
|
|
214
|
+
exports.FEATURE_FLAG_CALLED_EVENT = __webpack_exports__.FEATURE_FLAG_CALLED_EVENT;
|
|
215
|
+
exports.applyFlagEventCapture = __webpack_exports__.applyFlagEventCapture;
|
|
216
|
+
exports.buildConfigVersion2FlagEvents = __webpack_exports__.buildConfigVersion2FlagEvents;
|
|
217
|
+
exports.minimizeConfigVersion2FlagCalledProperties = __webpack_exports__.minimizeConfigVersion2FlagCalledProperties;
|
|
218
|
+
exports.ruleAttribution = __webpack_exports__.ruleAttribution;
|
|
219
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
220
|
+
"CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES",
|
|
221
|
+
"EXPERIMENT_EXPOSURE_EVENT",
|
|
222
|
+
"FEATURE_FLAG_CALLED_EVENT",
|
|
223
|
+
"applyFlagEventCapture",
|
|
224
|
+
"buildConfigVersion2FlagEvents",
|
|
225
|
+
"minimizeConfigVersion2FlagCalledProperties",
|
|
226
|
+
"ruleAttribution"
|
|
227
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
228
|
+
Object.defineProperty(exports, '__esModule', {
|
|
229
|
+
value: true
|
|
230
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { minimizeFlagCalledEventProperties } from "@posthog/core";
|
|
2
|
+
const FEATURE_FLAG_CALLED_EVENT = '$feature_flag_called';
|
|
3
|
+
const EXPERIMENT_EXPOSURE_EVENT = '$experiment_exposure';
|
|
4
|
+
const EXPERIMENT_SPLIT_REASON = 'experiment_split';
|
|
5
|
+
const EXPERIMENT_RULE_TYPE = 'experiment';
|
|
6
|
+
const RULE_TYPES_BY_REASON_CODE = new Map([
|
|
7
|
+
[
|
|
8
|
+
'targeting_match',
|
|
9
|
+
new Set([
|
|
10
|
+
'targeted_release',
|
|
11
|
+
'percentage_rollout'
|
|
12
|
+
])
|
|
13
|
+
],
|
|
14
|
+
[
|
|
15
|
+
EXPERIMENT_SPLIT_REASON,
|
|
16
|
+
new Set([
|
|
17
|
+
EXPERIMENT_RULE_TYPE
|
|
18
|
+
])
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
'rollout_miss',
|
|
22
|
+
new Set([
|
|
23
|
+
'percentage_rollout',
|
|
24
|
+
EXPERIMENT_RULE_TYPE
|
|
25
|
+
])
|
|
26
|
+
],
|
|
27
|
+
[
|
|
28
|
+
'experiment_paused',
|
|
29
|
+
new Set([
|
|
30
|
+
EXPERIMENT_RULE_TYPE
|
|
31
|
+
])
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
'holdout',
|
|
35
|
+
new Set([
|
|
36
|
+
EXPERIMENT_RULE_TYPE
|
|
37
|
+
])
|
|
38
|
+
]
|
|
39
|
+
]);
|
|
40
|
+
const CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES = [
|
|
41
|
+
'$feature_flag_config_version',
|
|
42
|
+
'$feature_flag_reason_code',
|
|
43
|
+
'$feature_flag_rule_id',
|
|
44
|
+
'$feature_flag_rule_type',
|
|
45
|
+
'$feature_flag_variant_key'
|
|
46
|
+
];
|
|
47
|
+
const CONFIG_VERSION_2_FLAG_CALLED_REMOVED = new Set([
|
|
48
|
+
...CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES,
|
|
49
|
+
'$feature_flag_reason',
|
|
50
|
+
'$feature_flag_forced_variant',
|
|
51
|
+
'$feature_flag_experiment_id'
|
|
52
|
+
]);
|
|
53
|
+
const EXPOSURE_REMOVED = new Set([
|
|
54
|
+
'$feature_flag_config_version',
|
|
55
|
+
'$feature_flag_reason_code',
|
|
56
|
+
'$feature_flag_rule_type',
|
|
57
|
+
'$experiment_exposure_source',
|
|
58
|
+
'$feature_flag_experiment_id',
|
|
59
|
+
'$feature_flag_forced_variant'
|
|
60
|
+
]);
|
|
61
|
+
const EXPOSURE_PROPERTIES_FROM_FLAG_CALLED = [
|
|
62
|
+
'$feature_flag_id',
|
|
63
|
+
'$feature_flag_version',
|
|
64
|
+
'$feature_flag_request_id',
|
|
65
|
+
'$feature_flag_evaluated_at'
|
|
66
|
+
];
|
|
67
|
+
const UUID_PATTERN = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
68
|
+
function ruleAttribution(record) {
|
|
69
|
+
if (void 0 === record || void 0 !== record.unreadableField || 2 !== record.metadata.configVersion) return;
|
|
70
|
+
const reasonCode = record.reason?.code || void 0;
|
|
71
|
+
const noRule = {
|
|
72
|
+
reasonCode,
|
|
73
|
+
ruleId: void 0,
|
|
74
|
+
ruleType: void 0,
|
|
75
|
+
variantKey: void 0
|
|
76
|
+
};
|
|
77
|
+
const allowedRuleTypes = void 0 === reasonCode ? void 0 : RULE_TYPES_BY_REASON_CODE.get(reasonCode);
|
|
78
|
+
if (record.failed || void 0 === allowedRuleTypes) return noRule;
|
|
79
|
+
const { ruleId, ruleType, variantKey } = record.metadata;
|
|
80
|
+
if (void 0 === ruleId || !UUID_PATTERN.test(ruleId) || void 0 === ruleType || !allowedRuleTypes.has(ruleType)) return noRule;
|
|
81
|
+
if (reasonCode !== EXPERIMENT_SPLIT_REASON) return {
|
|
82
|
+
...noRule,
|
|
83
|
+
ruleId,
|
|
84
|
+
ruleType
|
|
85
|
+
};
|
|
86
|
+
if (void 0 === variantKey) return noRule;
|
|
87
|
+
return {
|
|
88
|
+
reasonCode,
|
|
89
|
+
ruleId,
|
|
90
|
+
ruleType,
|
|
91
|
+
variantKey
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function applyFlagEventCapture(properties, capture) {
|
|
95
|
+
const kept = Object.fromEntries(Object.entries(properties).filter(([name])=>!capture.removed.has(name)));
|
|
96
|
+
return {
|
|
97
|
+
...kept,
|
|
98
|
+
...capture.pinned
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function minimizeConfigVersion2FlagCalledProperties(properties) {
|
|
102
|
+
const minimal = minimizeFlagCalledEventProperties(properties);
|
|
103
|
+
for (const name of CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES)if (void 0 !== properties[name]) minimal[name] = properties[name];
|
|
104
|
+
return minimal;
|
|
105
|
+
}
|
|
106
|
+
function buildConfigVersion2FlagEvents(params) {
|
|
107
|
+
const { key, response, groups, record } = params;
|
|
108
|
+
const attribution = ruleAttribution(record);
|
|
109
|
+
if (void 0 === attribution) return;
|
|
110
|
+
const { reasonCode, ruleId, ruleType, variantKey } = attribution;
|
|
111
|
+
const groupEntries = Object.entries(groups ?? {}).sort(([a], [b])=>a < b ? -1 : a > b ? 1 : 0);
|
|
112
|
+
const attributionProperties = {
|
|
113
|
+
$feature_flag_config_version: 2
|
|
114
|
+
};
|
|
115
|
+
if (void 0 !== reasonCode) attributionProperties.$feature_flag_reason_code = reasonCode;
|
|
116
|
+
if (void 0 !== ruleId) {
|
|
117
|
+
attributionProperties.$feature_flag_rule_id = ruleId;
|
|
118
|
+
attributionProperties.$feature_flag_rule_type = ruleType;
|
|
119
|
+
}
|
|
120
|
+
if (void 0 !== variantKey) attributionProperties.$feature_flag_variant_key = variantKey;
|
|
121
|
+
const { $feature_flag_reason: _reason, ...flagCalledProperties } = params.properties;
|
|
122
|
+
Object.assign(flagCalledProperties, attributionProperties);
|
|
123
|
+
const flagCalled = {
|
|
124
|
+
event: FEATURE_FLAG_CALLED_EVENT,
|
|
125
|
+
dedupeKey: JSON.stringify([
|
|
126
|
+
key,
|
|
127
|
+
response ?? null,
|
|
128
|
+
groupEntries,
|
|
129
|
+
2,
|
|
130
|
+
reasonCode ?? null,
|
|
131
|
+
ruleId ?? null,
|
|
132
|
+
ruleType ?? null,
|
|
133
|
+
variantKey ?? null
|
|
134
|
+
]),
|
|
135
|
+
properties: flagCalledProperties,
|
|
136
|
+
capture: {
|
|
137
|
+
removed: CONFIG_VERSION_2_FLAG_CALLED_REMOVED,
|
|
138
|
+
pinned: attributionProperties
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
if (void 0 === variantKey) return {
|
|
142
|
+
flagCalled,
|
|
143
|
+
exposure: void 0
|
|
144
|
+
};
|
|
145
|
+
const exposureIdentity = {
|
|
146
|
+
$feature_flag: key,
|
|
147
|
+
$feature_flag_response: response,
|
|
148
|
+
$feature_flag_variant_key: variantKey,
|
|
149
|
+
$feature_flag_rule_id: ruleId,
|
|
150
|
+
locally_evaluated: true === flagCalledProperties.locally_evaluated
|
|
151
|
+
};
|
|
152
|
+
const exposureProperties = {};
|
|
153
|
+
for (const name of EXPOSURE_PROPERTIES_FROM_FLAG_CALLED)if (void 0 !== flagCalledProperties[name]) exposureProperties[name] = flagCalledProperties[name];
|
|
154
|
+
Object.assign(exposureProperties, exposureIdentity);
|
|
155
|
+
return {
|
|
156
|
+
flagCalled,
|
|
157
|
+
exposure: {
|
|
158
|
+
event: EXPERIMENT_EXPOSURE_EVENT,
|
|
159
|
+
dedupeKey: JSON.stringify([
|
|
160
|
+
key,
|
|
161
|
+
groupEntries,
|
|
162
|
+
2,
|
|
163
|
+
ruleId,
|
|
164
|
+
variantKey
|
|
165
|
+
]),
|
|
166
|
+
properties: exposureProperties,
|
|
167
|
+
capture: {
|
|
168
|
+
removed: EXPOSURE_REMOVED,
|
|
169
|
+
pinned: exposureIdentity
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
export { CONFIG_VERSION_2_ATTRIBUTION_PROPERTIES, EXPERIMENT_EXPOSURE_EVENT, FEATURE_FLAG_CALLED_EVENT, applyFlagEventCapture, buildConfigVersion2FlagEvents, minimizeConfigVersion2FlagCalledProperties, ruleAttribution };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { FeatureFlagDetail, JsonType } from '@posthog/core';
|
|
2
|
+
/**
|
|
3
|
+
* Reads the flag records of the `/flags?v=4` response.
|
|
4
|
+
*
|
|
5
|
+
* A v4 record carries one typed `value` (boolean, string, number, object or null) instead of
|
|
6
|
+
* `enabled` and `variant`, plus a structured reason and rule metadata. A record without `value`
|
|
7
|
+
* comes from a server that does not serve v4: its value is `variant`, or `enabled` when there is
|
|
8
|
+
* no variant, and its config version is 1.
|
|
9
|
+
*
|
|
10
|
+
* The existing getters and events read the legacy rendering of each record, which is derived
|
|
11
|
+
* from `value` here, in {@link renderValue}. The parsed record is kept for the typed flag API
|
|
12
|
+
* and is internal: {@link parsedFlagRecordOf} returns it.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* A flag's typed value. `null` means the flag has no value.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export type FlagRecordValue = boolean | string | number | {
|
|
20
|
+
[key: string]: JsonType;
|
|
21
|
+
} | null;
|
|
22
|
+
/**
|
|
23
|
+
* A `/flags` flag record as this SDK read it. Internal: it is not part of the public API.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export type ParsedFlagRecord = {
|
|
28
|
+
/**
|
|
29
|
+
* The typed value. `null` for an unreadable record and a failed v4 record. A record without a
|
|
30
|
+
* typed `value` keeps `variant`, or `enabled` when there is no variant, even when it failed.
|
|
31
|
+
*/
|
|
32
|
+
readonly value: FlagRecordValue;
|
|
33
|
+
/** Whether the flag failed to evaluate, or its record could not be read. */
|
|
34
|
+
readonly failed: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The wire path of the known field with the wrong JSON type that made the record unreadable,
|
|
37
|
+
* such as `metadata.id`. Absent from a readable record, including one the server marks failed.
|
|
38
|
+
*/
|
|
39
|
+
readonly unreadableField?: string;
|
|
40
|
+
readonly reason?: {
|
|
41
|
+
readonly code?: string;
|
|
42
|
+
readonly conditionIndex?: number;
|
|
43
|
+
readonly description?: string;
|
|
44
|
+
};
|
|
45
|
+
readonly metadata: {
|
|
46
|
+
readonly id?: number;
|
|
47
|
+
readonly version?: number;
|
|
48
|
+
/** The flag's configuration format: `1` for a record without a typed `value`; absent when a record has none. */
|
|
49
|
+
readonly configVersion?: number;
|
|
50
|
+
readonly description?: string;
|
|
51
|
+
readonly hasExperiment?: boolean;
|
|
52
|
+
/** The rule that produced the result. */
|
|
53
|
+
readonly ruleType?: string;
|
|
54
|
+
readonly ruleId?: string;
|
|
55
|
+
/** The assigned variant. */
|
|
56
|
+
readonly variantKey?: string;
|
|
57
|
+
readonly forcedVariant?: boolean;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
/** Logged once per client, the first time a `/flags` response has a record without a typed value. */
|
|
61
|
+
export declare const FLAGS_RESPONSE_WITHOUT_TYPED_VALUES_MESSAGE: string;
|
|
62
|
+
/**
|
|
63
|
+
* Reads the flag records of a `/flags` response body. Each record in `flags` is replaced by its
|
|
64
|
+
* legacy rendering, the shape the shared v2 reader reads, and the rest of the body is returned
|
|
65
|
+
* unchanged. A body without a `flags` object is returned as is.
|
|
66
|
+
*
|
|
67
|
+
* `withoutTypedValues` says whether the body has a record without a typed `value`, or is a v1
|
|
68
|
+
* response with flags, which a server that does not serve v4 sends.
|
|
69
|
+
*/
|
|
70
|
+
export declare function readFlagRecords(body: unknown): {
|
|
71
|
+
body: unknown;
|
|
72
|
+
withoutTypedValues: boolean;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The parsed record of a flag detail from a `/flags` response. A detail that was not read by
|
|
76
|
+
* {@link readFlagRecords}, such as one from a v1 response, reads as a config version 1 record.
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
export declare function parsedFlagRecordOf(detail: FeatureFlagDetail): ParsedFlagRecord;
|
|
81
|
+
/** Whether a flag's record had a known field with the wrong JSON type, so it could not be read. */
|
|
82
|
+
export declare function isUnreadableFlagRecord(detail: FeatureFlagDetail): boolean;
|
|
83
|
+
//# sourceMappingURL=flags-response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flags-response.d.ts","sourceRoot":"","sources":["../src/flags-response.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAQjB,QAAQ,EACT,MAAM,eAAe,CAAA;AAEtB;;;;;;;;;;;GAWG;AAEH;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAA;CAAE,GAAG,IAAI,CAAA;AAE5F;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAA;IAC/B,4EAA4E;IAC5E,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IACxB;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;IACjC,QAAQ,CAAC,MAAM,CAAC,EAAE;QAChB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;QACtB,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;QAChC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAC9B,CAAA;IACD,QAAQ,CAAC,QAAQ,EAAE;QACjB,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAA;QACpB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;QACzB,gHAAgH;QAChH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAA;QAC/B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;QAC7B,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;QAChC,yCAAyC;QACzC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;QAC1B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;QACxB,4BAA4B;QAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;QAC5B,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;KACjC,CAAA;CACF,CAAA;AAED,qGAAqG;AACrG,eAAO,MAAM,2CAA2C,QAEb,CAAA;AAmB3C;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,kBAAkB,EAAE,OAAO,CAAA;CAAE,CAsB7F;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,GAAG,gBAAgB,CAqB9E;AAED,mGAAmG;AACnG,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAEzE"}
|