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 " ;
77
88/**
99 * Creates a telemetry publisher that sends feature evaluation events to Application Insights.
@@ -12,16 +12,48 @@ import { IEventTelemetry } from "@microsoft/applicationinsights-web";
1212 */
1313export function createTelemetryPublisher ( client : ApplicationInsights ) : ( event : EvaluationResult ) => void {
1414 return ( event : EvaluationResult ) => {
15+ if ( event . feature === undefined ) {
16+ return ;
17+ }
18+
1519 const eventProperties = {
16- "FeatureName" : event . feature ?. id ,
20+ "Version" : EVALUATION_EVENT_VERSION ,
21+ "FeatureName" : event . feature . id ,
1722 "Enabled" : event . enabled . toString ( ) ,
1823 // Ensure targetingId is string so that it will be placed in customDimensions
1924 "TargetingId" : event . targetingId ?. toString ( ) ,
2025 "Variant" : event . variant ?. name ,
2126 "VariantAssignmentReason" : event . variantAssignmentReason ,
2227 } ;
2328
24- const metadata = event . feature ?. telemetry ?. metadata ;
29+ if ( event . feature . allocation ?. default_when_enabled ) {
30+ eventProperties [ "DefaultWhenEnabled" ] = event . feature . allocation . default_when_enabled ;
31+ }
32+
33+ if ( event . variantAssignmentReason === VariantAssignmentReason . DefaultWhenEnabled ) {
34+ let percentileAllocationPercentage = 0 ;
35+ if ( event . variant !== undefined && event . feature . allocation !== undefined && event . feature . allocation . percentile !== undefined ) {
36+ for ( const percentile of event . feature . allocation . percentile ) {
37+ if ( percentile . variant === event . variant . name ) {
38+ percentileAllocationPercentage += percentile . to - percentile . from ;
39+ }
40+ }
41+ }
42+ eventProperties [ "PercentileAllocationPercentage" ] = ( 100 - percentileAllocationPercentage ) . toString ( ) ;
43+ }
44+ else if ( event . variantAssignmentReason === VariantAssignmentReason . Percentile ) {
45+ let percentileAllocationPercentage = 0 ;
46+ if ( event . variant !== undefined && event . feature . allocation !== undefined && event . feature . allocation . percentile !== undefined ) {
47+ for ( const percentile of event . feature . allocation . percentile ) {
48+ if ( percentile . variant === event . variant . name ) {
49+ percentileAllocationPercentage += percentile . to - percentile . from ;
50+ }
51+ }
52+ }
53+ eventProperties [ "PercentileAllocationPercentage" ] = percentileAllocationPercentage . toString ( ) ;
54+ }
55+
56+ const metadata = event . feature . telemetry ?. metadata ;
2557 if ( metadata ) {
2658 for ( const key in metadata ) {
2759 if ( ! ( key in eventProperties ) ) {
0 commit comments