Skip to content

feat(launchdarkly-client-provider): Add tracking API #1219

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 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libs/providers/launchdarkly-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ await OpenFeature.setContext({ key: 'my-key' });

Read more about LD contexts [here](https://launchdarkly.github.io/js-client-sdk/interfaces/LDContextCommon.html)

## Tracking

You can send custom events to LaunchDarkly metrics for use in
experiments and guarded rollouts. To learn more, read [Sending custom events](https://launchdarkly.com/docs/sdk/features/events).

```ts
const client = await OpenFeature.getClient();
client.track('event-key-123abc', { customProperty: someValue })
```

## Building

Run `nx package providers-launchdarkly-client` to build the library.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('LaunchDarklyClientProvider', () => {
waitForInitialization: jest.fn(),
on: jest.fn(),
close: jest.fn(),
track: jest.fn(),
} as unknown as jest.Mocked<LDClient>;

beforeAll(() => {
Expand Down Expand Up @@ -376,6 +377,18 @@ describe('LaunchDarklyClientProvider', () => {
});
});

it('calls the client track method properly', async () => {
ldClientMock.track = jest.fn().mockResolvedValue({});
ofClient.track('event-key-123abc', { value: 99.77, currency: 'USD' });
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', { currency: 'USD' }, 99.77);

ofClient.track('event-key-123abc', { value: 99.77 });
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', {}, 99.77);

ofClient.track('event-key-123abc', { currency: 'USD' });
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', { currency: 'USD' }, undefined);
});

describe('onContextChange', () => {
it('logs information about missing keys', async () => {
ldClientMock.identify = jest.fn().mockResolvedValue({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
OpenFeatureEventEmitter,
ProviderEvents,
ProviderStatus,
TrackingEventDetails,
} from '@openfeature/web-sdk';

import isEmpty from 'lodash.isempty';
Expand Down Expand Up @@ -153,6 +154,11 @@ export class LaunchDarklyClientProvider implements Provider {
return wrongTypeResult(defaultValue);
}

track(trackingEventName: string, _context: EvaluationContext, { value, ...details }: TrackingEventDetails): void {
// The LD Client already has the context form the identify method, so we can omit it here.
this.client.track(trackingEventName, details, value);
}

private translateContext(context: EvaluationContext) {
return translateContext(this.logger, context);
}
Expand Down