|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// |
| 4 | +using Microsoft.ApplicationInsights; |
| 5 | +using Microsoft.ApplicationInsights.DataContracts; |
| 6 | +using Microsoft.FeatureManagement.FeatureFilters; |
| 7 | + |
| 8 | +namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Provides extension methods for tracking events with TargetingContext. |
| 12 | + /// </summary> |
| 13 | + public static class TelemetryClientExtensions |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Extension method to track an event with <see cref="TargetingContext"/>. |
| 17 | + /// </summary> |
| 18 | + public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null) |
| 19 | + { |
| 20 | + ValidateTargetingContext(targetingContext); |
| 21 | + |
| 22 | + if (properties == null) |
| 23 | + { |
| 24 | + properties = new Dictionary<string, string>(); |
| 25 | + } |
| 26 | + |
| 27 | + properties["TargetingId"] = targetingContext.UserId; |
| 28 | + |
| 29 | + telemetryClient.TrackEvent(eventName, properties, metrics); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Extension method to track an <see cref="EventTelemetry"/> with <see cref="TargetingContext"/>. |
| 34 | + /// </summary> |
| 35 | + public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext) |
| 36 | + { |
| 37 | + ValidateTargetingContext(targetingContext); |
| 38 | + |
| 39 | + if (telemetry == null) |
| 40 | + { |
| 41 | + telemetry = new EventTelemetry(); |
| 42 | + } |
| 43 | + |
| 44 | + telemetry.Properties["TargetingId"] = targetingContext.UserId; |
| 45 | + |
| 46 | + telemetryClient.TrackEvent(telemetry); |
| 47 | + } |
| 48 | + |
| 49 | + private static void ValidateTargetingContext(TargetingContext targetingContext) |
| 50 | + { |
| 51 | + if (targetingContext == null) |
| 52 | + { |
| 53 | + throw new ArgumentNullException(nameof(targetingContext)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments