-
Notifications
You must be signed in to change notification settings - Fork 49
feat: implement tracking as per spec #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dcdb631
feat: tracking
toddbaert 1dc6ced
fixup: test var names
toddbaert ac2caa2
fixup: try/catch and freeze context
toddbaert 623f939
fixup: dont merge ctx uneccessarily
toddbaert c939571
fixup: use shared sig
toddbaert d9a704e
fixup: pr feedback from lukas
toddbaert 6932ea2
Merge branch 'main' into feat/tracking
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './use-track'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { TrackingEventDetails } from '@openfeature/web-sdk'; | ||
| import { useCallback } from 'react'; | ||
| import { useOpenFeatureClient } from '../provider'; | ||
|
|
||
| export type Track = { | ||
| /** | ||
| * Context-aware tracking function for the parent `<OpenFeatureProvider/>`. | ||
| * Track a user action or application state, usually representing a business objective or outcome. | ||
| * @param trackingEventName an identifier for the event | ||
| * @param trackingEventDetails the details of the tracking event | ||
| */ | ||
| track: (trackingEventName: string, trackingEventDetails?: TrackingEventDetails) => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Get a context-aware tracking function. | ||
| * @returns {Track} context-aware tracking | ||
| */ | ||
| export function useTrack(): Track { | ||
| const client = useOpenFeatureClient(); | ||
|
|
||
| const track = useCallback((trackingEventName: string, trackingEventDetails?: TrackingEventDetails) => { | ||
| client.track(trackingEventName, trackingEventDetails); | ||
| }, []); | ||
|
|
||
| return { | ||
| track, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { jest } from '@jest/globals'; | ||
| import '@testing-library/jest-dom'; // see: https://testing-library.com/docs/react-testing-library/setup | ||
| import { render } from '@testing-library/react'; | ||
| import * as React from 'react'; | ||
| import type { Provider, TrackingEventDetails } from '../src'; | ||
| import { | ||
| OpenFeature, | ||
| OpenFeatureProvider, | ||
| useTrack | ||
| } from '../src'; | ||
|
|
||
| describe('tracking', () => { | ||
|
|
||
| const eventName = 'test-tracking-event'; | ||
| const trackingValue = 1234; | ||
| const trackingDetails: TrackingEventDetails = { | ||
| value: trackingValue, | ||
| }; | ||
| const domain = 'someDomain'; | ||
|
|
||
| const mockProvider = () => { | ||
| const mockProvider: Provider = { | ||
| metadata: { | ||
| name: 'mock', | ||
| }, | ||
|
|
||
| track: jest.fn((): void => { | ||
| return; | ||
| }), | ||
| } as unknown as Provider; | ||
|
|
||
| return mockProvider; | ||
| }; | ||
|
|
||
| describe('no domain', () => { | ||
| it('should call default provider', async () => { | ||
|
|
||
| const provider = mockProvider(); | ||
| await OpenFeature.setProviderAndWait(provider); | ||
|
|
||
| function Component() { | ||
| const { track } = useTrack(); | ||
| track(eventName, trackingDetails); | ||
|
|
||
| return <div></div>; | ||
| } | ||
|
|
||
| render( | ||
| <OpenFeatureProvider suspend={false} > | ||
| <Component></Component> | ||
| </OpenFeatureProvider>, | ||
| ); | ||
|
|
||
| expect(provider.track).toHaveBeenCalledWith( | ||
| eventName, | ||
| expect.anything(), | ||
| expect.objectContaining({ value: trackingValue }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('domain set', () => { | ||
| it('should call provider for domain', async () => { | ||
|
|
||
| const domainProvider = mockProvider(); | ||
| await OpenFeature.setProviderAndWait(domain, domainProvider); | ||
|
|
||
| function Component() { | ||
| const { track } = useTrack(); | ||
| track(eventName, trackingDetails); | ||
|
|
||
| return <div></div>; | ||
| } | ||
|
|
||
| render( | ||
| <OpenFeatureProvider domain={domain} suspend={false} > | ||
| <Component></Component> | ||
| </OpenFeatureProvider>, | ||
| ); | ||
|
|
||
| expect(domainProvider.track).toHaveBeenCalledWith( | ||
| eventName, | ||
| expect.anything(), | ||
| expect.objectContaining({ value: trackingValue }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './tracking'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import type { EvaluationContext, TrackingEventDetails } from '@openfeature/core'; | ||
|
|
||
| export interface Tracking { | ||
|
|
||
| /** | ||
| * Track a user action or application state, usually representing a business objective or outcome. | ||
| * @param trackingEventName an identifier for the event | ||
| * @param context the evaluation context | ||
| * @param trackingEventDetails the details of the tracking event | ||
| */ | ||
| track(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.