Skip to content

Commit 2138d20

Browse files
pavkamroncohen
authored andcommitted
feat(node-sdk): enhance feature evaluation with detailed event tracking (#335)
- Add support for tracking rule evaluation results and missing context fields, - Improve feature flag event details in `sendFeatureEvent`, - Update types to include new evaluation metadata - Enhance test coverage for new event tracking features, - Bump package version to 1.5.4
1 parent c7048d6 commit 2138d20

4 files changed

Lines changed: 251 additions & 47 deletions

File tree

packages/node-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bucketco/node-sdk",
3-
"version": "1.5.3",
3+
"version": "1.5.4",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

packages/node-sdk/src/client.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ export class BucketClient {
512512
key,
513513
isEnabled: feature?.isEnabled ?? false,
514514
targetingVersion: feature?.targetingVersion,
515+
missingContextFields: feature?.missingContextFields,
516+
ruleEvaluationResults: feature?.ruleEvaluationResults,
515517
});
516518
}
517519

@@ -561,6 +563,7 @@ export class BucketClient {
561563
companyId,
562564
additionalContext,
563565
);
566+
564567
return features[key];
565568
}
566569

@@ -940,6 +943,8 @@ export class BucketClient {
940943
key: res.featureKey,
941944
isEnabled: res.value ?? false,
942945
targetingVersion: keyToVersionMap.get(res.featureKey),
946+
ruleEvaluationResults: res.ruleEvaluationResults,
947+
missingContextFields: res.missingContextFields,
943948
};
944949
return acc;
945950
},
@@ -964,20 +969,29 @@ export class BucketClient {
964969
}
965970

966971
private _wrapRawFeature(
967-
options: { enableTracking: boolean } & Context,
968-
{ key, isEnabled, targetingVersion }: RawFeature,
972+
{ enableTracking, ...context }: { enableTracking: boolean } & Context,
973+
{
974+
key,
975+
isEnabled,
976+
targetingVersion,
977+
missingContextFields,
978+
ruleEvaluationResults,
979+
}: RawFeature,
969980
): Feature {
970981
// eslint-disable-next-line @typescript-eslint/no-this-alias
971982
const client = this;
972983

973984
return {
974985
get isEnabled() {
975-
if (options.enableTracking) {
986+
if (enableTracking) {
976987
void client
977988
.sendFeatureEvent({
978989
action: "check",
979990
key,
980991
targetingVersion,
992+
evalContext: context,
993+
evalMissingFields: missingContextFields,
994+
evalRuleResults: ruleEvaluationResults,
981995
evalResult: isEnabled,
982996
})
983997
.catch((err) => {
@@ -992,14 +1006,14 @@ export class BucketClient {
9921006
},
9931007
key,
9941008
track: async () => {
995-
if (typeof options.user?.id === "undefined") {
1009+
if (typeof context.user?.id === "undefined") {
9961010
this._config.logger?.warn("no user set, cannot track event");
9971011
return;
9981012
}
9991013

1000-
if (options.enableTracking) {
1001-
await this.track(options.user.id, key, {
1002-
companyId: options.company?.id,
1014+
if (enableTracking) {
1015+
await this.track(context.user.id, key, {
1016+
companyId: context.company?.id,
10031017
});
10041018
} else {
10051019
this._config.logger?.debug("tracking disabled, not tracking event");
@@ -1026,6 +1040,7 @@ export class BucketClient {
10261040
...context,
10271041
enableTracking: true,
10281042
};
1043+
10291044
checkContextWithTracking(contextWithTracking);
10301045

10311046
const params = new URLSearchParams(

packages/node-sdk/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ export interface RawFeature {
7474
*/
7575
targetingVersion?: number;
7676

77+
/**
78+
* The rule results of the evaluation (optional).
79+
*/
80+
ruleEvaluationResults?: boolean[];
81+
7782
/**
7883
* The missing fields in the evaluation context (optional).
7984
*/

0 commit comments

Comments
 (0)