Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,5 @@ examples/**/**/package-lock.json

# playwright test result
test-results

**/public
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { createTelemetryPublisher, trackEvent } from "./telemetry.js";
export { createTargetingTelemetryInitializer, createTelemetryPublisher, trackEvent } from "./telemetry.js";
export { VERSION } from "./version.js";
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management";
import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web";
import { EvaluationResult, createFeatureEvaluationEventProperties, ITargetingContextAccessor } from "@microsoft/feature-management";
import { ApplicationInsights, IEventTelemetry, ITelemetryItem } from "@microsoft/applicationinsights-web";

const TARGETING_ID = "TargetingId";
const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation";
Expand Down Expand Up @@ -39,3 +39,20 @@ export function trackEvent(client: ApplicationInsights, targetingId: string, eve
properties[TARGETING_ID] = targetingId ? targetingId.toString() : "";
client.trackEvent(event, properties);
}

/**
* Creates a telemetry initializer that adds targeting id to telemetry item's custom properties.
* @param targetingContextAccessor The accessor function to get the targeting context.
* @returns A telemetry initializer that attaches targeting id to telemetry items.
*/
export function createTargetingTelemetryInitializer(targetingContextAccessor: ITargetingContextAccessor): (item: ITelemetryItem) => void {
return (item: ITelemetryItem) => {
const targetingContext = targetingContextAccessor.getTargetingContext();
if (targetingContext !== undefined) {
if (targetingContext?.userId === undefined) {
console.warn("Targeting id is undefined.");
}
item.data = {...item.data, [TARGETING_ID]: targetingContext?.userId || ""};
}
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { createTelemetryPublisher, trackEvent } from "./telemetry.js";
export { createTargetingTelemetryProcessor, createTelemetryPublisher, trackEvent } from "./telemetry.js";
export { VERSION } from "./version.js";
21 changes: 20 additions & 1 deletion src/feature-management-applicationinsights-node/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management";
import { EvaluationResult, createFeatureEvaluationEventProperties, ITargetingContextAccessor } from "@microsoft/feature-management";
import { TelemetryClient, Contracts } from "applicationinsights";

const TARGETING_ID = "TargetingId";
Expand Down Expand Up @@ -39,3 +39,22 @@ export function trackEvent(client: TelemetryClient, targetingId: string, event:
};
client.trackEvent(event);
}

/**
* Creates a telemetry processor that adds targeting id to telemetry envelope's custom properties.
* @param targetingContextAccessor The accessor function to get the targeting context.
* @returns A telemetry processor that attaches targeting id to telemetry envelopes.
*/
export function createTargetingTelemetryProcessor(targetingContextAccessor: ITargetingContextAccessor): (envelope: Contracts.EnvelopeTelemetry) => boolean {
return (envelope: Contracts.EnvelopeTelemetry) => {
const targetingContext = targetingContextAccessor.getTargetingContext();
if (targetingContext !== undefined) {
if (targetingContext?.userId === undefined) {
console.warn("Targeting id is undefined.");
}
envelope.data.baseData = envelope.data.baseData || {};
envelope.data.baseData.properties = {...envelope.data.baseData.properties, [TARGETING_ID]: targetingContext?.userId || ""};
}
return true;
};
}