Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions sdk/feature-management-applicationinsights-browser/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { EvaluationResult } from "@microsoft/feature-management";
import { ApplicationInsights } from "@microsoft/applicationinsights-web";
import { IEventTelemetry } from "@microsoft/applicationinsights-web";
import { EvaluationResult, VariantAssignmentReason } from "@microsoft/feature-management";
import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web";
import { EVALUATION_EVENT_VERSION } from "./version.js";

const VERSION = "Version";
const FEATURE_NAME = "FeatureName";
const ENABLED = "Enabled";
const TARGETING_ID = "TargetingId";
const VARIANT = "Variant";
const VARIANT_ASSIGNMENT_REASON = "VariantAssignmentReason";
const DEFAULT_WHEN_ENABLED = "DefaultWhenEnabled";
const VARIANT_ASSIGNMENT_PERCENTAGE = "VariantAssignmentPercentage";
const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation";

/**
* Creates a telemetry publisher that sends feature evaluation events to Application Insights.
Expand All @@ -12,16 +22,46 @@ import { IEventTelemetry } from "@microsoft/applicationinsights-web";
*/
export function createTelemetryPublisher(client: ApplicationInsights): (event: EvaluationResult) => void {
return (event: EvaluationResult) => {
if (event.feature === undefined) {
return;
}

const eventProperties = {
"FeatureName": event.feature ? event.feature.id : "",
"Enabled": event.enabled.toString(),
[VERSION]: EVALUATION_EVENT_VERSION,
[FEATURE_NAME]: event.feature ? event.feature.id : "",
[ENABLED]: event.enabled.toString(),
// Ensure targetingId is string so that it will be placed in customDimensions
"TargetingId": event.targetingId ? event.targetingId.toString() : "",
"Variant": event.variant ? event.variant.name : "",
"VariantAssignmentReason": event.variantAssignmentReason,
[TARGETING_ID]: event.targetingId ? event.targetingId.toString() : "",
[VARIANT]: event.variant ? event.variant.name : "",
[VARIANT_ASSIGNMENT_REASON]: event.variantAssignmentReason,
};

const metadata = event.feature?.telemetry?.metadata;
if (event.feature.allocation?.default_when_enabled) {
eventProperties[DEFAULT_WHEN_ENABLED] = event.feature.allocation.default_when_enabled;
}

if (event.variantAssignmentReason === VariantAssignmentReason.DefaultWhenEnabled) {
let percentileAllocationPercentage = 0;
if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) {
for (const percentile of event.feature.allocation.percentile) {
percentileAllocationPercentage += percentile.to - percentile.from;
}
}
eventProperties[VARIANT_ASSIGNMENT_PERCENTAGE] = (100 - percentileAllocationPercentage).toString();
}
else if (event.variantAssignmentReason === VariantAssignmentReason.Percentile) {
let percentileAllocationPercentage = 0;
if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) {
for (const percentile of event.feature.allocation.percentile) {
if (percentile.variant === event.variant.name) {
percentileAllocationPercentage += percentile.to - percentile.from;
}
}
}
eventProperties[VARIANT_ASSIGNMENT_PERCENTAGE] = percentileAllocationPercentage.toString();
}

const metadata = event.feature.telemetry?.metadata;
if (metadata) {
for (const key in metadata) {
if (!(key in eventProperties)) {
Expand All @@ -30,7 +70,7 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E
}
}

client.trackEvent({ name: "FeatureEvaluation" }, eventProperties);
client.trackEvent({ name: FEATURE_EVALUATION_EVENT_NAME }, eventProperties);
};
}

Expand All @@ -47,6 +87,6 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E
export function trackEvent(client: ApplicationInsights, targetingId: string, event: IEventTelemetry, customProperties?: {[key: string]: any}): void {
const properties = customProperties ? { ...customProperties } : {};
// Ensure targetingId is string so that it will be placed in customDimensions
properties["TargetingId"] = targetingId ? targetingId.toString() : "";
properties[TARGETING_ID] = targetingId ? targetingId.toString() : "";
client.trackEvent(event, properties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
// Licensed under the MIT license.

export const VERSION = "2.0.0-preview.2";
export const EVALUATION_EVENT_VERSION = "1.0.0";
Loading