From 849d8328315c8d6986861a08aeb3bb3b2be7550c Mon Sep 17 00:00:00 2001 From: Zhang Yi Jiang Date: Mon, 6 Apr 2020 19:23:10 -0700 Subject: [PATCH] Enforce either userId or anonymousId as identifiers (#43633) --- package.json | 3 +- types/analytics-node/analytics-node-tests.ts | 18 ++++++++++++ types/analytics-node/index.d.ts | 31 ++++++++------------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index f2114216ac..864a4495cd 100644 --- a/package.json +++ b/package.json @@ -32,5 +32,6 @@ "_comment": "This will remove husky from when we started migrating to use prettier", "pre-commit": "npm uninstall husky" } - } + }, + "dependencies": {} } diff --git a/types/analytics-node/analytics-node-tests.ts b/types/analytics-node/analytics-node-tests.ts index eef2065293..f8f0a80772 100644 --- a/types/analytics-node/analytics-node-tests.ts +++ b/types/analytics-node/analytics-node-tests.ts @@ -59,6 +59,24 @@ function testTrack(): void { } }); + analytics.track({ + anonymousId: '019mr8mf4r', + event: 'Purchased an Item', + properties: { + revenue: 39.95, + shippingMethod: '2-day' + } + }); + + // $ExpectError + analytics.track({ + event: 'Purchased an Item', + properties: { + revenue: 39.95, + shippingMethod: '2-day' + } + }); + analytics.track({ userId: '019mr8mf4r', event: 'Purchased an Item', diff --git a/types/analytics-node/index.d.ts b/types/analytics-node/index.d.ts index fba1ffc246..101852b8c8 100644 --- a/types/analytics-node/index.d.ts +++ b/types/analytics-node/index.d.ts @@ -3,11 +3,16 @@ // Definitions by: Andrew Fong // Thomas Thiebaud // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 export = AnalyticsNode.Analytics; declare namespace AnalyticsNode { - interface Message { + type Identity = + | { userId: string | number } + | { userId?: string | number; anonymousId: string | number }; + + type Message = Identity & { type: string; context: { library: { @@ -22,9 +27,7 @@ declare namespace AnalyticsNode { }; timestamp?: Date; messageId?: string; - anonymousId?: string | number; - userId?: string | number; - } + }; interface Data { batch: Message[]; @@ -49,9 +52,7 @@ declare namespace AnalyticsNode { /* The identify method lets you tie a user to their actions and record traits about them. */ - identify(message: { - userId?: string | number; - anonymousId?: string | number; + identify(message: Identity & { traits?: any; timestamp?: Date; context?: any; @@ -59,9 +60,7 @@ declare namespace AnalyticsNode { }, callback?: (err: Error, data: Data) => void): Analytics; /* The track method lets you record the actions your users perform. */ - track(message: { - userId?: string | number; - anonymousId?: string | number; + track(message: Identity & { event: string; properties?: any; timestamp?: Date; @@ -71,9 +70,7 @@ declare namespace AnalyticsNode { /* The page method lets you record page views on your website, along with optional extra information about the page being viewed. */ - page(message: { - userId?: string | number; - anonymousId?: string | number; + page(message: Identity & { category?: string; name?: string; properties?: any; @@ -83,18 +80,14 @@ declare namespace AnalyticsNode { }, callback?: (err: Error, data: Data) => void): Analytics; /* alias is how you associate one identity with another. */ - alias(message: { + alias(message: Identity & { previousId: string | number; - userId?: string | number; - anonymousId?: string | number; integrations?: Integrations; }, callback?: (err: Error, data: Data) => void): Analytics; /* Group calls can be used to associate individual users with shared accounts or companies. */ - group(message: { - userId?: string | number; - anonymousId?: string | number; + group(message: Identity & { groupId: string | number; traits?: any; context?: any;