Skip to content
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 { createTelemetryPublisher, trackEvent, createTargetingTelemetryInitializer } 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, TargetingContextAccessor } 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,18 @@ 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: TargetingContextAccessor): (item: ITelemetryItem) => void {
return (item: ITelemetryItem) => {
const targetingContext = targetingContextAccessor();
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 { createTelemetryPublisher, trackEvent, createTargetingTelemetryProcessor } from "./telemetry.js";
export { VERSION } from "./version.js";
19 changes: 18 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, TargetingContextAccessor } from "@microsoft/feature-management";
import { TelemetryClient, Contracts } from "applicationinsights";

const TARGETING_ID = "TargetingId";
Expand Down Expand Up @@ -39,3 +39,20 @@ 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: TargetingContextAccessor): (envelope: Contracts.EnvelopeTelemetry) => boolean {
return (envelope: Contracts.EnvelopeTelemetry) => {
const targetingContext = targetingContextAccessor();
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; // If a telemetry processor returns false, that telemetry item isn't sent.
};
}