Handle identity changes and keep SDK outages from interrupting pages

This commit is contained in:
2026-09-16 04:49:22 +08:00
parent 9d6ef1ec39
commit af5b797290
3 changed files with 36 additions and 12 deletions
+17
View File
@@ -47,3 +47,20 @@ test('tracking requires collection configuration and cannot send another app env
const client = createClient({ baseUrl: 'https://api.example', appId: 'shop', collectionKey: 'collection-demo', storage: null });
await assert.rejects(client.sendEvent({ ...client.prepareEvent('page', {}), app_id: 'other' }), /configured application/);
});
test('login identity update and logout rotation replace the cached tracking identity', async () => {
const requests = [];
const next = { ...identity, anonymous_id: 'new-anonymous', anonymous_token: 'new-signed-token' };
globalThis.fetch = async (url, options) => {
requests.push({ url, body: JSON.parse(options.body) });
if (url.endsWith('?rotate=true')) return response(next);
return response(identity);
};
const client = createClient({ baseUrl: 'https://api.example', appId: 'shop', collectionKey: 'collection-demo', storage: null });
client.updateIdentity(next);
await client.track('page', { page_id: 'home' });
assert.equal(requests[0].body.anonymous_token, next.anonymous_token);
await client.resetIdentity();
assert.equal(requests[1].url, 'https://api.example/v1/identity/anonymous?rotate=true');
assert.throws(() => client.updateIdentity({}), /Invalid anonymous identity/);
});