Skip to content

Commit 443e780

Browse files
Adds extension methods for TrackEvent and TrackMetric (#348)
* Adds extension methods for TrackEvent and TrackMetric * Adds copyright, crefs, and uses []= instead of .Add for properties * Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com> * Added null check, moved shared logic to helper functions * Removed TrackMetric extension for now --------- Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>
1 parent 27ba646 commit 443e780

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)