11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { EvaluationResult } from "@microsoft/feature-management" ;
5- import { ApplicationInsights } from "@microsoft/applicationinsights-web" ;
6- import { IEventTelemetry } from "@microsoft/applicationinsights-web" ;
4+ import { EvaluationResult , VariantAssignmentReason } from "@microsoft/feature-management" ;
5+ import { ApplicationInsights , IEventTelemetry } from "@microsoft/applicationinsights-web" ;
6+ import { EVALUATION_EVENT_VERSION } from "./version.js" ;
7+
8+ const VERSION = "Version" ;
9+ const FEATURE_NAME = "FeatureName" ;
10+ const ENABLED = "Enabled" ;
11+ const TARGETING_ID = "TargetingId" ;
12+ const VARIANT = "Variant" ;
13+ const VARIANT_ASSIGNMENT_REASON = "VariantAssignmentReason" ;
14+ const DEFAULT_WHEN_ENABLED = "DefaultWhenEnabled" ;
15+ const VARIANT_ASSIGNMENT_PERCENTAGE = "VariantAssignmentPercentage" ;
16+ const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation" ;
717
818/**
919 * Creates a telemetry publisher that sends feature evaluation events to Application Insights.
@@ -12,16 +22,46 @@ import { IEventTelemetry } from "@microsoft/applicationinsights-web";
1222 */
1323export function createTelemetryPublisher ( client : ApplicationInsights ) : ( event : EvaluationResult ) => void {
1424 return ( event : EvaluationResult ) => {
25+ if ( event . feature === undefined ) {
26+ return ;
27+ }
28+
1529 const eventProperties = {
16- "FeatureName" : event . feature ? event . feature . id : "" ,
17- "Enabled" : event . enabled . toString ( ) ,
30+ [ VERSION ] : EVALUATION_EVENT_VERSION ,
31+ [ FEATURE_NAME ] : event . feature ? event . feature . id : "" ,
32+ [ ENABLED ] : event . enabled . toString ( ) ,
1833 // Ensure targetingId is string so that it will be placed in customDimensions
19- "TargetingId" : event . targetingId ? event . targetingId . toString ( ) : "" ,
20- "Variant" : event . variant ? event . variant . name : "" ,
21- "VariantAssignmentReason" : event . variantAssignmentReason ,
34+ [ TARGETING_ID ] : event . targetingId ? event . targetingId . toString ( ) : "" ,
35+ [ VARIANT ] : event . variant ? event . variant . name : "" ,
36+ [ VARIANT_ASSIGNMENT_REASON ] : event . variantAssignmentReason ,
2237 } ;
2338
24- const metadata = event . feature ?. telemetry ?. metadata ;
39+ if ( event . feature . allocation ?. default_when_enabled ) {
40+ eventProperties [ DEFAULT_WHEN_ENABLED ] = event . feature . allocation . default_when_enabled ;
41+ }
42+
43+ if ( event . variantAssignmentReason === VariantAssignmentReason . DefaultWhenEnabled ) {
44+ let percentileAllocationPercentage = 0 ;
45+ if ( event . variant !== undefined && event . feature . allocation !== undefined && event . feature . allocation . percentile !== undefined ) {
46+ for ( const percentile of event . feature . allocation . percentile ) {
47+ percentileAllocationPercentage += percentile . to - percentile . from ;
48+ }
49+ }
50+ eventProperties [ VARIANT_ASSIGNMENT_PERCENTAGE ] = ( 100 - percentileAllocationPercentage ) . toString ( ) ;
51+ }
52+ else if ( event . variantAssignmentReason === VariantAssignmentReason . Percentile ) {
53+ let percentileAllocationPercentage = 0 ;
54+ if ( event . variant !== undefined && event . feature . allocation !== undefined && event . feature . allocation . percentile !== undefined ) {
55+ for ( const percentile of event . feature . allocation . percentile ) {
56+ if ( percentile . variant === event . variant . name ) {
57+ percentileAllocationPercentage += percentile . to - percentile . from ;
58+ }
59+ }
60+ }
61+ eventProperties [ VARIANT_ASSIGNMENT_PERCENTAGE ] = percentileAllocationPercentage . toString ( ) ;
62+ }
63+
64+ const metadata = event . feature . telemetry ?. metadata ;
2565 if ( metadata ) {
2666 for ( const key in metadata ) {
2767 if ( ! ( key in eventProperties ) ) {
@@ -30,7 +70,7 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E
3070 }
3171 }
3272
33- client . trackEvent ( { name : "FeatureEvaluation" } , eventProperties ) ;
73+ client . trackEvent ( { name : FEATURE_EVALUATION_EVENT_NAME } , eventProperties ) ;
3474 } ;
3575}
3676
@@ -47,6 +87,6 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E
4787export function trackEvent ( client : ApplicationInsights , targetingId : string , event : IEventTelemetry , customProperties ?: { [ key : string ] : any } ) : void {
4888 const properties = customProperties ? { ...customProperties } : { } ;
4989 // Ensure targetingId is string so that it will be placed in customDimensions
50- properties [ "TargetingId" ] = targetingId ? targetingId . toString ( ) : "" ;
90+ properties [ TARGETING_ID ] = targetingId ? targetingId . toString ( ) : "" ;
5191 client . trackEvent ( event , properties ) ;
5292}
0 commit comments