|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | | -import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management"; |
5 | | -import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web"; |
| 4 | +import { EvaluationResult, createFeatureEvaluationEventProperties, ITargetingContextAccessor } from "@microsoft/feature-management"; |
| 5 | +import { ApplicationInsights, IEventTelemetry, ITelemetryItem } from "@microsoft/applicationinsights-web"; |
6 | 6 |
|
7 | 7 | const TARGETING_ID = "TargetingId"; |
8 | 8 | const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation"; |
@@ -39,3 +39,20 @@ export function trackEvent(client: ApplicationInsights, targetingId: string, eve |
39 | 39 | properties[TARGETING_ID] = targetingId ? targetingId.toString() : ""; |
40 | 40 | client.trackEvent(event, properties); |
41 | 41 | } |
| 42 | + |
| 43 | +/** |
| 44 | + * Creates a telemetry initializer that adds targeting id to telemetry item's custom properties. |
| 45 | + * @param targetingContextAccessor The accessor function to get the targeting context. |
| 46 | + * @returns A telemetry initializer that attaches targeting id to telemetry items. |
| 47 | + */ |
| 48 | +export function createTargetingTelemetryInitializer(targetingContextAccessor: ITargetingContextAccessor): (item: ITelemetryItem) => void { |
| 49 | + return (item: ITelemetryItem) => { |
| 50 | + const targetingContext = targetingContextAccessor.getTargetingContext(); |
| 51 | + if (targetingContext !== undefined) { |
| 52 | + if (targetingContext?.userId === undefined) { |
| 53 | + console.warn("Targeting id is undefined."); |
| 54 | + } |
| 55 | + item.data = {...item.data, [TARGETING_ID]: targetingContext?.userId || ""}; |
| 56 | + } |
| 57 | + }; |
| 58 | +} |
0 commit comments