22// Licensed under the MIT license.
33
44import { EvaluationResult } from "@microsoft/feature-management" ;
5- import { applicationInsights } from "applicationinsights" ;
5+ import { TelemetryClient , Contracts } from "applicationinsights" ;
66
7- export function publishTelemetry ( event : EvaluationResult ) : void {
8- if ( applicationInsights . defaultClient === undefined ) {
9- console . warn ( `Application Insights default client is not found.` ) ;
10- return ;
11- }
7+ /**
8+ * Creates a telemetry publisher that sends feature evaluation events to Application Insights.
9+ * @param client The Application Insights telemetry client.
10+ * @returns A callback function that takes an evaluation result and tracks an event with the evaluation details.
11+ */
12+ export function createTelemetryPublisher ( client : TelemetryClient ) : ( event : EvaluationResult ) => void {
13+ return ( event : EvaluationResult ) => {
14+ const eventProperties = {
15+ "FeatureName" : event . feature ? event . feature . id : "" ,
16+ "Enabled" : event . enabled . toString ( ) ,
17+ // Ensure targetingId is string so that it will be placed in customDimensions
18+ "TargetingId" : event . targetingId ? event . targetingId . toString ( ) : "" ,
19+ "Variant" : event . variant ? event . variant . name : "" ,
20+ "VariantAssignmentReason" : event . variantAssignmentReason ,
21+ } ;
1222
13- const eventProperties = {
14- "FeatureName" : event . feature ? event . feature . id : "" ,
15- "Enabled" : event . enabled . toString ( ) ,
16- // Ensure targetingId is string so that it will be placed in customDimensions
17- "TargetingId" : event . targetingId ? event . targetingId . toString ( ) : "" ,
18- "Variant" : event . variant ? event . variant . name : "" ,
19- "VariantAssignmentReason" : event . variantAssignmentReason ,
20- } ;
21-
22- const metadata = event . feature ?. telemetry ?. metadata ;
23- if ( metadata ) {
24- for ( const key in metadata ) {
25- if ( ! ( key in eventProperties ) ) {
26- eventProperties [ key ] = metadata [ key ] ;
23+ const metadata = event . feature ?. telemetry ?. metadata ;
24+ if ( metadata ) {
25+ for ( const key in metadata ) {
26+ if ( ! ( key in eventProperties ) ) {
27+ eventProperties [ key ] = metadata [ key ] ;
28+ }
2729 }
2830 }
29- }
3031
31- applicationInsights . defaultClient . trackEvent ( { name : "FeatureEvaluation" , properties : eventProperties } ) ;
32- }
32+ client . trackEvent ( { name : "FeatureEvaluation" , properties : eventProperties } ) ;
33+ } ;
34+ }
35+
36+ /**
37+ * Tracks a custom event using Application Insights, ensuring that the "TargetingId"
38+ * is included in the custom properties. If the "TargetingId" already exists in
39+ * the provided custom properties, it will be overwritten.
40+ *
41+ * @param client The Application Insights client instance used to track the event.
42+ * @param targetingId The unique targeting identifier that will be included in the custom properties.
43+ * @param event The event telemetry object to be tracked, containing event details.
44+ */
45+ export function trackEvent ( client : TelemetryClient , targetingId : string , event : Contracts . EventTelemetry ) : void {
46+ event . properties = {
47+ ...event . properties ,
48+ TargetingId : targetingId ? targetingId . toString ( ) : ""
49+ } ;
50+ client . trackEvent ( event ) ;
51+ }
0 commit comments