Skip to content

Commit

Permalink
Fix: include initial stable id in web analytics (#412)
Browse files Browse the repository at this point in the history
* fix: include initial stable id in web analytics

* chore: linter

* chore: set user

* chore: fix accidental default value

* chore: edit fn name
  • Loading branch information
kat-statsig authored Jan 13, 2025
1 parent b6d1ad4 commit 17d7956
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
56 changes: 56 additions & 0 deletions packages/combo/src/__tests__/WebAnalyticsInitialStableID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fetchMock from 'jest-fetch-mock';

import { StatsigClient, StatsigUser } from '@statsig/js-client';
import {
StatsigAutoCapturePlugin,
runStatsigAutoCapture,
} from '@statsig/web-analytics';

describe('Plugin Stable ID Override Tests', () => {
let client: StatsigClient;
const user: StatsigUser = {
userID: 'a-user',
customIDs: {
stableID: 'a-stable-id',
},
};
beforeAll(() => {
fetchMock.enableMocks();
});

afterAll(async () => {
await client.shutdown();
fetchMock.mockClear();
});

it('Run auto capture includes initial user stable Id', async () => {
client = new StatsigClient('client-key', user);
runStatsigAutoCapture(client);
await client.initializeAsync();

const request = fetchMock.mock.calls[1];
const body = JSON.parse(String(request[1]?.body ?? '')) as any;
expect(body.events).toHaveLength(1);

const event = body.events[0];
expect(event.eventName).toEqual('auto_capture::page_view');
expect(event.user?.customIDs?.stableID).toEqual('a-stable-id');
expect(body?.statsigMetadata?.stableID).toEqual('a-stable-id');
});

it('Auto capture plugin includes initial user stable Id', async () => {
client = new StatsigClient('client-key', user, {
plugins: [new StatsigAutoCapturePlugin()],
});
await client.initializeAsync();

const request = fetchMock.mock.calls[1];
const body = JSON.parse(String(request[1]?.body ?? '')) as any;
expect(body.events).toHaveLength(1);

const event = body.events[0];
expect(event.eventName).toEqual('auto_capture::page_view');
expect(event.user?.customIDs?.stableID).toEqual('a-stable-id');
expect(body?.statsigMetadata?.stableID).toEqual('a-stable-id');
});
});
13 changes: 10 additions & 3 deletions packages/js-client/src/StatsigClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class StatsigClient
);

this._store = new EvaluationStore(sdkKey);
this._user = _normalizeUser(user, options);
this._user = this._configureUser(user, options);

const plugins = options?.plugins ?? [];
for (const plugin of plugins) {
Expand Down Expand Up @@ -370,12 +370,19 @@ export default class StatsigClient
this._logger.reset();
this._store.reset();

this._user = _normalizeUser(user, this._options);
this._user = this._configureUser(user, this._options);
}

const stableIdOverride = this._user.customIDs?.stableID;
private _configureUser(
originalUser: StatsigUser,
options: StatsigOptions | null,
): StatsigUserInternal {
const user = _normalizeUser(originalUser, options);
const stableIdOverride = user.customIDs?.stableID;
if (stableIdOverride) {
StableID.setOverride(stableIdOverride, this._sdkKey);
}
return user;
}

private _getFeatureGateImpl(
Expand Down

0 comments on commit 17d7956

Please sign in to comment.