Skip to content

Commit e888d03

Browse files
authored
feat(launchdarkly-client-provider): Add tracking API (#1219)
Signed-off-by: Tommy Josépovic <[email protected]>
1 parent 0a87fd6 commit e888d03

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

libs/providers/launchdarkly-client/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ await OpenFeature.setContext({ key: 'my-key' });
5959

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

62+
## Tracking
63+
64+
You can send custom events to LaunchDarkly metrics for use in
65+
experiments and guarded rollouts. To learn more, read [Sending custom events](https://launchdarkly.com/docs/sdk/features/events).
66+
67+
```ts
68+
const client = await OpenFeature.getClient();
69+
client.track('event-key-123abc', { customProperty: someValue })
70+
```
71+
6272
## Building
6373

6474
Run `nx package providers-launchdarkly-client` to build the library.

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('LaunchDarklyClientProvider', () => {
2727
waitForInitialization: jest.fn(),
2828
on: jest.fn(),
2929
close: jest.fn(),
30+
track: jest.fn(),
3031
} as unknown as jest.Mocked<LDClient>;
3132

3233
beforeAll(() => {
@@ -376,6 +377,18 @@ describe('LaunchDarklyClientProvider', () => {
376377
});
377378
});
378379

380+
it('calls the client track method properly', async () => {
381+
ldClientMock.track = jest.fn().mockResolvedValue({});
382+
ofClient.track('event-key-123abc', { value: 99.77, currency: 'USD' });
383+
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', { currency: 'USD' }, 99.77);
384+
385+
ofClient.track('event-key-123abc', { value: 99.77 });
386+
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', {}, 99.77);
387+
388+
ofClient.track('event-key-123abc', { currency: 'USD' });
389+
expect(ldClientMock.track).toHaveBeenCalledWith('event-key-123abc', { currency: 'USD' }, undefined);
390+
});
391+
379392
describe('onContextChange', () => {
380393
it('logs information about missing keys', async () => {
381394
ldClientMock.identify = jest.fn().mockResolvedValue({});

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.ts

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
OpenFeatureEventEmitter,
1212
ProviderEvents,
1313
ProviderStatus,
14+
TrackingEventDetails,
1415
} from '@openfeature/web-sdk';
1516

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

157+
track(trackingEventName: string, _context: EvaluationContext, { value, ...details }: TrackingEventDetails): void {
158+
// The LD Client already has the context form the identify method, so we can omit it here.
159+
this.client.track(trackingEventName, details, value);
160+
}
161+
156162
private translateContext(context: EvaluationContext) {
157163
return translateContext(this.logger, context);
158164
}

0 commit comments

Comments
 (0)