Skip to content

Commit

Permalink
feat: add support for invite-type anonymous event tracking (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen authored Apr 27, 2022
1 parent 730c64d commit 086adfd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion test/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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();
Expand Down

0 comments on commit 086adfd

Please sign in to comment.