From 086adfd443b2c337278b440a89e3874e5f70c042 Mon Sep 17 00:00:00 2001 From: Stephen Young Date: Wed, 27 Apr 2022 03:30:40 -0400 Subject: [PATCH] feat: add support for invite-type anonymous event tracking (#106) --- lib/track.ts | 12 +++++++----- test/track.ts | 13 ++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/track.ts b/lib/track.ts index c0307c9..bc462a2 100644 --- a/lib/track.ts +++ b/lib/track.ts @@ -72,15 +72,17 @@ export class TrackClient { } trackAnonymous(anonymousId: string | number, data: RequestData = {}) { - if (isEmpty(anonymousId)) { - throw new MissingParamError('anonymousId'); - } - if (isEmpty(data.name)) { throw new MissingParamError('data.name'); } - return this.request.post(`${this.trackRoot}/events`, { ...data, anonymous_id: anonymousId }); + let payload = { ...data}; + + if (!isEmpty(anonymousId)) { + payload["anonymous_id"] = anonymousId; + } + + return this.request.post(`${this.trackRoot}/events`, payload); } trackPageView(customerId: string | number, path: string) { diff --git a/test/track.ts b/test/track.ts index 1b38af7..388cccc 100644 --- a/test/track.ts +++ b/test/track.ts @@ -119,7 +119,7 @@ ID_INPUTS.forEach(([input, expected]) => { test('#trackAnonymous works', (t) => { sinon.stub(t.context.client.request, 'post'); - t.throws(() => t.context.client.trackAnonymous(''), { message: 'anonymousId is required' }); + t.throws(() => t.context.client.trackAnonymous(''), { message: 'data.name is required' }); t.throws(() => t.context.client.trackAnonymous('123'), { message: 'data.name is required' }); t.throws(() => t.context.client.trackAnonymous('123', { data: {} }), { message: 'data.name is required' }); t.context.client.trackAnonymous('123', { name: 'purchase', data: 'yep' }); @@ -132,6 +132,17 @@ test('#trackAnonymous works', (t) => { ); }); +test('#trackAnonymous ignores blank anonymousId', (t) => { + sinon.stub(t.context.client.request, 'post'); + t.context.client.trackAnonymous('', { name: 'purchase', data: 'yep' }) + t.truthy( + (t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/events`, { + name: 'purchase', + data: 'yep', + }), + ); + }); + test('#trackPush works', (t) => { sinon.stub(t.context.client.request, 'post'); t.context.client.trackPush();