Skip to content

Commit c94f8f1

Browse files
explain targeting context initializer
1 parent c00b129 commit c94f8f1

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

articles/azure-app-configuration/feature-management-javascript-reference.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,24 @@ In the above example, the feature is enabled for users named `Jeff` and `Alicia`
453453

454454
An example web application that uses the targeting feature filter is available in this [example](https://github.com/microsoft/FeatureManagement-JavaScript/tree/preview/examples/express-app) project.
455455

456-
#### Ambient targeting context
457-
458456
In web applications, especially those with multiple components or layers, passing targeting context (`userId` and `groups`) to every feature flag check can become cumbersome and repetitive. This scenario is referred to as "ambient targeting context," where the user identity information is already available in the application context (such as in session data or authentication context) but needs to be accessible to feature management evaluations throughout the application.
459457

460-
The library provides a solution through the `targetingContextAccessor` pattern. Instead of explicitly passing the targeting context with each `isEnabled` or `getVariant` call, you can provide a function that knows how to retrieve the current user's targeting information from your application's context:
458+
#### ITargetingContextAccessor
459+
460+
The library provides a solution through the `ITargetingContextAccessor` pattern.
461+
462+
``` typescript
463+
interface ITargetingContext {
464+
userId?: string;
465+
groups?: string[];
466+
}
467+
468+
interface ITargetingContextAccessor {
469+
getTargetingContext: () => ITargetingContext | undefined;
470+
}
471+
```
472+
473+
Instead of explicitly passing the targeting context with each `isEnabled` or `getVariant` call, you can provide a function that knows how to retrieve the current user's targeting information from your application's context:
461474

462475
```typescript
463476
import { FeatureManager, ConfigurationObjectFeatureFlagProvider } from "@microsoft/feature-management";
@@ -828,6 +841,22 @@ trackEvent(appInsights.defaultClient, "<TARGETING_ID>", {name: "TestEvent", pro
828841

829842
The telemetry publisher sends `FeatureEvaluation` custom events to the Application Insights when a feature flag enabled with telemetry is evaluated. The custom event follows the [FeatureEvaluationEvent](https://github.com/microsoft/FeatureManagement/tree/main/Schema/FeatureEvaluationEvent) schema.
830843

844+
### Targeting telemetry processor
845+
846+
If you have implemented [`ITargetingContextAccessor`](#itargetingcontextaccessor), you can use the built-in Application Insights telemetry processor to automatically attached targeting id information to telemetry by calling `createTargetingTelemetryProcessor` function.
847+
848+
```typescript
849+
const appInsights = require("applicationinsights");
850+
appInsights.setup(process.env.APPINSIGHTS_CONNECTION_STRING).start();
851+
852+
const { createTargetingTelemetryProcessor } = require("@microsoft/feature-management-applicationinsights-node");
853+
appInsights.defaultClient.addTelemetryProcessor(
854+
createTargetingTelemetryProcessor(targetingContextAccessor)
855+
);
856+
```
857+
858+
This ensures that every telemetry sent to Application Insights includes the targeting id information, allowing you to correlate feature flag usage in your analytics.
859+
831860
## Next steps
832861

833862
To learn how to use feature flags in your applications, continue to the following quickstarts.

articles/azure-app-configuration/feature-management-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Feature | .NET | Spring | Python | JavaScript
3939
------- | ---- | ------ | ------ | ----------
4040
Targeting Filter | [GA](./feature-management-dotnet-reference.md#targeting) | GA | [GA](./feature-management-python-reference.md#targeting) | [GA](./feature-management-javascript-reference.md#targeting)
4141
Targeting Exclusion | [GA](./feature-management-dotnet-reference.md#targeting-exclusion) | GA | [GA](./feature-management-python-reference.md#targeting-exclusion) | [GA](./feature-management-javascript-reference.md#targeting-exclusion)
42-
Ambient Targeting | [GA](./feature-management-dotnet-reference.md#targeting-in-a-web-application) | WIP | WIP | Preview
42+
Ambient Targeting | [GA](./feature-management-dotnet-reference.md#targeting-in-a-web-application) | WIP | WIP | [Preview](./feature-management-javascript-reference.md#targeting-in-a-web-application)
4343
Time Window Filter | [GA](./feature-management-dotnet-reference.md#microsofttimewindow) | GA | [GA](./feature-management-python-reference.md#microsofttimewindow) | [GA](./feature-management-javascript-reference.md#microsofttimewindow)
4444
Recurring Time Window | [GA](./feature-management-dotnet-reference.md#microsofttimewindow) | GA | WIP | WIP
4545
Custom Feature Filter | [GA](./feature-management-dotnet-reference.md#implementing-a-feature-filter) | GA | [GA](./feature-management-python-reference.md#implementing-a-feature-filter) | [GA](./feature-management-javascript-reference.md#implementing-a-feature-filter)

0 commit comments

Comments
 (0)