Skip to content
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

feat: track impressions for web experiments #127

Merged
merged 29 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1b469f1
feat: add integration plugin
bgiori Sep 17, 2024
1e0a118
fix: update comment
bgiori Sep 17, 2024
eadceb4
fix: add comments; max queue size
bgiori Sep 17, 2024
cc5016f
fix: lint
bgiori Sep 17, 2024
f285b5d
fix: dont cache web exposures
bgiori Sep 17, 2024
a8d1ed2
feat: track impressions for web experiments
bgiori Sep 18, 2024
9f8c6b8
fix: undefined metadata
bgiori Sep 18, 2024
7cd333e
fix: undefined metadata
bgiori Sep 18, 2024
9955887
feat: change amplitude integration plugin to be feat exp specific
bgiori Sep 21, 2024
64297e5
Merge branch 'main' into integration-plugin
bgiori Sep 21, 2024
f8f7030
feat: remove persistence from analytics connector; fix tests
bgiori Sep 21, 2024
851945d
Merge branch 'integration-plugin' into track-impressions
bgiori Sep 21, 2024
b3cf5a2
set experimentIntegration as plugin on init
bgiori Sep 22, 2024
94a806b
Merge branch 'main' into integration-plugin
bgiori Oct 3, 2024
4ef74dc
chore: add version to tag file
bgiori Oct 3, 2024
ee6b093
fix: dont track exposure if trackExposure metadata is false
bgiori Oct 5, 2024
d6892f2
fix: force integration type on global integration
bgiori Oct 5, 2024
9a3f60b
fix: only allow one web exp instance
bgiori Oct 8, 2024
1763ad3
fix: support feat/web exp instances w/ same key & ua target
bgiori Oct 9, 2024
4d4afd4
chore: add experiment segment plugin (#128)
bgiori Oct 9, 2024
f6cf0da
fix: bind tracking call to correct object
bgiori Oct 9, 2024
818cee7
fix: lint
bgiori Oct 9, 2024
d0c8ecc
fix: dont allow double initialization
bgiori Oct 9, 2024
f0e7d39
fix: poller
bgiori Oct 10, 2024
2859114
fix: test increase timeout
bgiori Oct 10, 2024
3e4b9d2
Merge branch 'integration-plugin' into track-impressions
bgiori Oct 15, 2024
28176c2
Merge branch 'main' into track-impressions
bgiori Oct 15, 2024
3e4c9cb
fix: support custom exposrue events in metadata
bgiori Oct 15, 2024
8c5c7b0
remove segment example
bgiori Oct 15, 2024
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
25 changes: 22 additions & 3 deletions packages/experiment-browser/src/integration/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,30 @@ export class IntegrationManager {
*/
track(exposure: Exposure): void {
if (this.cache.shouldTrack(exposure)) {
this.queue.push({
eventType: '$exposure',
const event = this.getExposureEvent(exposure);
this.queue.push(event);
}
}

private getExposureEvent(exposure: Exposure): ExperimentEvent {
let event: ExperimentEvent = {
eventType: '$exposure',
eventProperties: exposure,
};
if (exposure.metadata?.exposureEvent) {
// Metadata specifically passes the exposure event definition
event = {
eventType: exposure.metadata?.exposureEvent as string,
eventProperties: exposure,
};
} else if (exposure.metadata?.deliveryMethod === 'web') {
// Web experiments track impression events by default
event = {
eventType: '$impression',
eventProperties: exposure,
});
};
}
return event;
}
}

Expand Down
44 changes: 24 additions & 20 deletions packages/experiment-browser/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,26 +676,30 @@ describe('variant fallbacks', () => {
expect(spy.mock.calls[0][0].variant).toBeUndefined();
});

test('default variant returned when no other fallback is provided', async () => {
const user = {};
const exposureTrackingProvider = new TestExposureTrackingProvider();
const spy = jest.spyOn(exposureTrackingProvider, 'track');
const client = new ExperimentClient(API_KEY, {
exposureTrackingProvider: exposureTrackingProvider,
source: Source.LocalStorage,
fetchOnStart: true,
});
mockClientStorage(client);
// Start and fetch
await client.start(user);
const variant = client.variant('sdk-ci-test');
expect(variant.key).toEqual('off');
expect(variant.value).toBeUndefined();
expect(variant.metadata?.default).toEqual(true);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0].flag_key).toEqual('sdk-ci-test');
expect(spy.mock.calls[0][0].variant).toBeUndefined();
});
test(
'default variant returned when no other fallback is provided',
async () => {
const user = {};
const exposureTrackingProvider = new TestExposureTrackingProvider();
const spy = jest.spyOn(exposureTrackingProvider, 'track');
const client = new ExperimentClient(API_KEY, {
exposureTrackingProvider: exposureTrackingProvider,
source: Source.LocalStorage,
fetchOnStart: true,
});
mockClientStorage(client);
// Start and fetch
await client.start(user);
const variant = client.variant('sdk-ci-test');
expect(variant.key).toEqual('off');
expect(variant.value).toBeUndefined();
expect(variant.metadata?.default).toEqual(true);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0].flag_key).toEqual('sdk-ci-test');
expect(spy.mock.calls[0][0].variant).toBeUndefined();
},
10 * 1000,
);
});

describe('initial variants source', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/experiment-browser/test/integration/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ describe('IntegrationManager', () => {
},
});
});
test('web exposure tracked as impression', () => {
manager.track({
flag_key: 'flag-key',
variant: 'treatment',
experiment_key: 'exp-1',
metadata: {
deliveryMethod: 'web',
},
});
expect(manager['queue']['inMemoryQueue'][0]).toEqual({
eventType: '$impression',
eventProperties: {
flag_key: 'flag-key',
variant: 'treatment',
experiment_key: 'exp-1',
metadata: {
deliveryMethod: 'web',
},
},
});
});
});
});

Expand Down
Loading