Skip to content

Commit cbf4f25

Browse files
authored
feat: Implement Tracking in .NET #309 (#327)
## This PR Adds support for tracking ### Related Issues Closes #309 --------- Signed-off-by: christian.lutnik <[email protected]>
1 parent 70f847b commit cbf4f25

File tree

8 files changed

+505
-1
lines changed

8 files changed

+505
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public async Task Example()
7474
|| [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. |
7575
|| [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). |
7676
|| [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. |
77+
|| [Tracking](#tracking) | Associate user actions with feature flag evaluations. |
7778
|| [Logging](#logging) | Integrate with popular logging packages. |
7879
|| [Domains](#domains) | Logically bind clients with providers. |
7980
|| [Eventing](#eventing) | React to state changes in the provider or flag management system. |
@@ -212,6 +213,19 @@ await Api.Instance.SetProviderAsync(myClient.GetMetadata().Name, provider);
212213
myClient.AddHandler(ProviderEventTypes.ProviderReady, callback);
213214
```
214215

216+
### Tracking
217+
218+
The [tracking API](https://openfeature.dev/specification/sections/tracking) allows you to use OpenFeature abstractions and objects to associate user actions with feature flag evaluations.
219+
This is essential for robust experimentation powered by feature flags.
220+
For example, a flag enhancing the appearance of a UI component might drive user engagement to a new feature; to test this hypothesis, telemetry collected by a hook(#hooks) or provider(#providers) can be associated with telemetry reported in the client's `track` function.
221+
222+
```csharp
223+
var client = Api.Instance.GetClient();
224+
client.Track("visited-promo-page", trackingEventDetails: new TrackingEventDetailsBuilder().SetValue(99.77).Set("currency", "USD").Build());
225+
```
226+
227+
Note that some providers may not support tracking; check the documentation for your provider for more information.
228+
215229
### Shutdown
216230

217231
The OpenFeature API provides a close function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down.
@@ -320,7 +334,7 @@ builder.Services.AddOpenFeature(featureBuilder => {
320334
featureBuilder
321335
.AddHostedFeatureLifecycle() // From Hosting package
322336
.AddContext((contextBuilder, serviceProvider) => { /* Custom context configuration */ })
323-
.AddInMemoryProvider();
337+
.AddInMemoryProvider();
324338
});
325339
```
326340
**Domain-Scoped Provider Configuration:**

src/OpenFeature/FeatureProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OpenFeature.Model;
88

99
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // required to allow NSubstitute mocking of internal methods
10+
1011
namespace OpenFeature
1112
{
1213
/// <summary>
@@ -140,5 +141,16 @@ public virtual Task ShutdownAsync(CancellationToken cancellationToken = default)
140141
/// </summary>
141142
/// <returns>The event channel of the provider</returns>
142143
public virtual Channel<object> GetEventChannel() => this.EventChannel;
144+
145+
/// <summary>
146+
/// Track a user action or application state, usually representing a business objective or outcome. The implementation of this method is optional.
147+
/// </summary>
148+
/// <param name="trackingEventName">The name associated with this tracking event</param>
149+
/// <param name="evaluationContext">The evaluation context used in the evaluation of the flag (optional)</param>
150+
/// <param name="trackingEventDetails">Data pertinent to the tracking event (Optional)</param>
151+
public virtual void Track(string trackingEventName, EvaluationContext? evaluationContext = default, TrackingEventDetails? trackingEventDetails = default)
152+
{
153+
// Intentionally left blank.
154+
}
143155
}
144156
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
5+
namespace OpenFeature.Model;
6+
7+
/// <summary>
8+
/// The `tracking event details` structure defines optional data pertinent to a particular `tracking event`.
9+
/// </summary>
10+
/// <seealso href="https://github.com/open-feature/spec/blob/main/specification/sections/06-tracking.md#62-tracking-event-details"/>
11+
public sealed class TrackingEventDetails
12+
{
13+
/// <summary>
14+
///A predefined value field for the tracking details.
15+
/// </summary>
16+
public readonly double? Value;
17+
18+
private readonly Structure _structure;
19+
20+
/// <summary>
21+
/// Internal constructor used by the builder.
22+
/// </summary>
23+
/// <param name="content"></param>
24+
/// <param name="value"></param>
25+
internal TrackingEventDetails(Structure content, double? value)
26+
{
27+
this.Value = value;
28+
this._structure = content;
29+
}
30+
31+
32+
/// <summary>
33+
/// Private constructor for making an empty <see cref="TrackingEventDetails"/>.
34+
/// </summary>
35+
private TrackingEventDetails()
36+
{
37+
this._structure = Structure.Empty;
38+
this.Value = null;
39+
}
40+
41+
/// <summary>
42+
/// Empty tracking event details.
43+
/// </summary>
44+
public static TrackingEventDetails Empty { get; } = new();
45+
46+
47+
/// <summary>
48+
/// Gets the Value at the specified key
49+
/// </summary>
50+
/// <param name="key">The key of the value to be retrieved</param>
51+
/// <returns>The <see cref="Model.Value"/> associated with the key</returns>
52+
/// <exception cref="KeyNotFoundException">
53+
/// Thrown when the context does not contain the specified key
54+
/// </exception>
55+
/// <exception cref="ArgumentNullException">
56+
/// Thrown when the key is <see langword="null" />
57+
/// </exception>
58+
public Value GetValue(string key) => this._structure.GetValue(key);
59+
60+
/// <summary>
61+
/// Bool indicating if the specified key exists in the evaluation context
62+
/// </summary>
63+
/// <param name="key">The key of the value to be checked</param>
64+
/// <returns><see cref="bool" />indicating the presence of the key</returns>
65+
/// <exception cref="ArgumentNullException">
66+
/// Thrown when the key is <see langword="null" />
67+
/// </exception>
68+
public bool ContainsKey(string key) => this._structure.ContainsKey(key);
69+
70+
/// <summary>
71+
/// Gets the value associated with the specified key
72+
/// </summary>
73+
/// <param name="value">The <see cref="Model.Value"/> or <see langword="null" /> if the key was not present</param>
74+
/// <param name="key">The key of the value to be retrieved</param>
75+
/// <returns><see cref="bool" />indicating the presence of the key</returns>
76+
/// <exception cref="ArgumentNullException">
77+
/// Thrown when the key is <see langword="null" />
78+
/// </exception>
79+
public bool TryGetValue(string key, out Value? value) => this._structure.TryGetValue(key, out value);
80+
81+
/// <summary>
82+
/// Gets all values as a Dictionary
83+
/// </summary>
84+
/// <returns>New <see cref="IDictionary{TKey,TValue}"/> representation of this Structure</returns>
85+
public IImmutableDictionary<string, Value> AsDictionary()
86+
{
87+
return this._structure.AsDictionary();
88+
}
89+
90+
/// <summary>
91+
/// Return a count of all values
92+
/// </summary>
93+
public int Count => this._structure.Count;
94+
95+
/// <summary>
96+
/// Return an enumerator for all values
97+
/// </summary>
98+
/// <returns>An enumerator for all values</returns>
99+
public IEnumerator<KeyValuePair<string, Value>> GetEnumerator()
100+
{
101+
return this._structure.GetEnumerator();
102+
}
103+
104+
/// <summary>
105+
/// Get a builder which can build an <see cref="EvaluationContext"/>.
106+
/// </summary>
107+
/// <returns>The builder</returns>
108+
public static TrackingEventDetailsBuilder Builder()
109+
{
110+
return new TrackingEventDetailsBuilder();
111+
}
112+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using System;
2+
3+
namespace OpenFeature.Model
4+
{
5+
/// <summary>
6+
/// A builder which allows the specification of attributes for an <see cref="TrackingEventDetails"/>.
7+
/// <para>
8+
/// A <see cref="TrackingEventDetailsBuilder"/> object is intended for use by a single thread and should not be used
9+
/// from multiple threads. Once an <see cref="TrackingEventDetails"/> has been created it is immutable and safe for use
10+
/// from multiple threads.
11+
/// </para>
12+
/// </summary>
13+
public sealed class TrackingEventDetailsBuilder
14+
{
15+
private readonly StructureBuilder _attributes = Structure.Builder();
16+
private double? _value;
17+
18+
/// <summary>
19+
/// Internal to only allow direct creation by <see cref="TrackingEventDetails.Builder()"/>.
20+
/// </summary>
21+
internal TrackingEventDetailsBuilder() { }
22+
23+
/// <summary>
24+
/// Set the predefined value field for the tracking details.
25+
/// </summary>
26+
/// <param name="value"></param>
27+
/// <returns></returns>
28+
public TrackingEventDetailsBuilder SetValue(double? value)
29+
{
30+
this._value = value;
31+
return this;
32+
}
33+
34+
/// <summary>
35+
/// Set the key to the given <see cref="Value"/>.
36+
/// </summary>
37+
/// <param name="key">The key for the value</param>
38+
/// <param name="value">The value to set</param>
39+
/// <returns>This builder</returns>
40+
public TrackingEventDetailsBuilder Set(string key, Value value)
41+
{
42+
this._attributes.Set(key, value);
43+
return this;
44+
}
45+
46+
/// <summary>
47+
/// Set the key to the given string.
48+
/// </summary>
49+
/// <param name="key">The key for the value</param>
50+
/// <param name="value">The value to set</param>
51+
/// <returns>This builder</returns>
52+
public TrackingEventDetailsBuilder Set(string key, string value)
53+
{
54+
this._attributes.Set(key, value);
55+
return this;
56+
}
57+
58+
/// <summary>
59+
/// Set the key to the given int.
60+
/// </summary>
61+
/// <param name="key">The key for the value</param>
62+
/// <param name="value">The value to set</param>
63+
/// <returns>This builder</returns>
64+
public TrackingEventDetailsBuilder Set(string key, int value)
65+
{
66+
this._attributes.Set(key, value);
67+
return this;
68+
}
69+
70+
/// <summary>
71+
/// Set the key to the given double.
72+
/// </summary>
73+
/// <param name="key">The key for the value</param>
74+
/// <param name="value">The value to set</param>
75+
/// <returns>This builder</returns>
76+
public TrackingEventDetailsBuilder Set(string key, double value)
77+
{
78+
this._attributes.Set(key, value);
79+
return this;
80+
}
81+
82+
/// <summary>
83+
/// Set the key to the given long.
84+
/// </summary>
85+
/// <param name="key">The key for the value</param>
86+
/// <param name="value">The value to set</param>
87+
/// <returns>This builder</returns>
88+
public TrackingEventDetailsBuilder Set(string key, long value)
89+
{
90+
this._attributes.Set(key, value);
91+
return this;
92+
}
93+
94+
/// <summary>
95+
/// Set the key to the given bool.
96+
/// </summary>
97+
/// <param name="key">The key for the value</param>
98+
/// <param name="value">The value to set</param>
99+
/// <returns>This builder</returns>
100+
public TrackingEventDetailsBuilder Set(string key, bool value)
101+
{
102+
this._attributes.Set(key, value);
103+
return this;
104+
}
105+
106+
/// <summary>
107+
/// Set the key to the given <see cref="Structure"/>.
108+
/// </summary>
109+
/// <param name="key">The key for the value</param>
110+
/// <param name="value">The value to set</param>
111+
/// <returns>This builder</returns>
112+
public TrackingEventDetailsBuilder Set(string key, Structure value)
113+
{
114+
this._attributes.Set(key, value);
115+
return this;
116+
}
117+
118+
/// <summary>
119+
/// Set the key to the given DateTime.
120+
/// </summary>
121+
/// <param name="key">The key for the value</param>
122+
/// <param name="value">The value to set</param>
123+
/// <returns>This builder</returns>
124+
public TrackingEventDetailsBuilder Set(string key, DateTime value)
125+
{
126+
this._attributes.Set(key, value);
127+
return this;
128+
}
129+
130+
/// <summary>
131+
/// Incorporate existing tracking details into the builder.
132+
/// <para>
133+
/// Any existing keys in the builder will be replaced by keys in the tracking details, including the Value set
134+
/// through <see cref="SetValue(double?)"/>.
135+
/// </para>
136+
/// </summary>
137+
/// <param name="trackingDetails">The tracking details to add merge</param>
138+
/// <returns>This builder</returns>
139+
public TrackingEventDetailsBuilder Merge(TrackingEventDetails trackingDetails)
140+
{
141+
this._value = trackingDetails.Value;
142+
foreach (var kvp in trackingDetails)
143+
{
144+
this.Set(kvp.Key, kvp.Value);
145+
}
146+
147+
return this;
148+
}
149+
150+
/// <summary>
151+
/// Build an immutable <see cref="TrackingEventDetails"/>.
152+
/// </summary>
153+
/// <returns>An immutable <see cref="TrackingEventDetails"/></returns>
154+
public TrackingEventDetails Build()
155+
{
156+
return new TrackingEventDetails(this._attributes.Build(), this._value);
157+
}
158+
}
159+
}

src/OpenFeature/OpenFeatureClient.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,31 @@ private async Task TriggerFinallyHooksAsync<T>(IReadOnlyList<Hook> hooks, HookCo
367367
}
368368
}
369369

370+
/// <summary>
371+
/// Use this method to track user interactions and the application state.
372+
/// </summary>
373+
/// <param name="trackingEventName">The name associated with this tracking event</param>
374+
/// <param name="evaluationContext">The evaluation context used in the evaluation of the flag (optional)</param>
375+
/// <param name="trackingEventDetails">Data pertinent to the tracking event (Optional)</param>
376+
/// <exception cref="ArgumentException">When trackingEventName is null or empty</exception>
377+
public void Track(string trackingEventName, EvaluationContext? evaluationContext = default, TrackingEventDetails? trackingEventDetails = default)
378+
{
379+
if (string.IsNullOrWhiteSpace(trackingEventName))
380+
{
381+
throw new ArgumentException("Tracking event cannot be null or empty.", nameof(trackingEventName));
382+
}
383+
384+
var globalContext = Api.Instance.GetContext();
385+
var clientContext = this.GetContext();
386+
387+
var evaluationContextBuilder = EvaluationContext.Builder()
388+
.Merge(globalContext)
389+
.Merge(clientContext);
390+
if (evaluationContext != null) evaluationContextBuilder.Merge(evaluationContext);
391+
392+
this._providerAccessor.Invoke().Track(trackingEventName, evaluationContextBuilder.Build(), trackingEventDetails);
393+
}
394+
370395
[LoggerMessage(100, LogLevel.Debug, "Hook {HookName} returned null, nothing to merge back into context")]
371396
partial void HookReturnedNull(string hookName);
372397

0 commit comments

Comments
 (0)