From dd7f22c76e15b2a080d65451f2e4858b2103cabf Mon Sep 17 00:00:00 2001 From: Thomas Thiebaud Date: Wed, 7 Feb 2018 21:50:38 +0100 Subject: [PATCH] Update type definitions for analytics-node --- types/analytics-node/analytics-node-tests.ts | 82 +++++++++++++++++++- types/analytics-node/index.d.ts | 44 +++++++---- 2 files changed, 112 insertions(+), 14 deletions(-) diff --git a/types/analytics-node/analytics-node-tests.ts b/types/analytics-node/analytics-node-tests.ts index 5207fe560b..5dfabb426c 100644 --- a/types/analytics-node/analytics-node-tests.ts +++ b/types/analytics-node/analytics-node-tests.ts @@ -18,6 +18,22 @@ function testIdentify(): void { friends: 42 } }); + + analytics.identify({ + userId: '019mr8mf4r', + traits: { + name: 'Michael Bolton', + email: 'mbolton@initech.com', + plan: 'Enterprise', + friends: 42 + } + }, (err, data) => { + if (err) { + console.error(err); + } else { + data.batch.forEach((message) => console.log(`${data.sentAt} : ${message}`)) + } + }); } function testTrack(): void { @@ -29,6 +45,21 @@ function testTrack(): void { shippingMethod: '2-day' } }); + + analytics.track({ + userId: '019mr8mf4r', + event: 'Purchased an Item', + properties: { + revenue: 39.95, + shippingMethod: '2-day' + } + }, (err, data) => { + if (err) { + console.error(err); + } else { + data.batch.forEach((message) => console.log(`${data.sentAt} : ${message}`)) + } + }); } function testPage(): void { @@ -43,6 +74,24 @@ function testPage(): void { referrer: 'https://github.com/segmentio/analytics-node' } }); + + analytics.page({ + userId: '019mr8mf4r', + category: 'Docs', + name: 'Node.js Library', + properties: { + url: 'https://segment.com/docs/libraries/node', + path: '/docs/libraries/node/', + title: 'Node.js Library - Segment', + referrer: 'https://github.com/segmentio/analytics-node' + } + }, (err, data) => { + if (err) { + console.error(err); + } else { + data.batch.forEach((message) => console.log(`${data.sentAt} : ${message}`)) + } + }); } function testAlias(): void { @@ -65,6 +114,21 @@ function testGroup(): void { description: 'Accounting Software' } }); + + analytics.group({ + userId: '019mr8mf4r', + groupId: '56', + traits: { + name: 'Initech', + description: 'Accounting Software' + } + }, (err, data) => { + if (err) { + console.error(err); + } else { + data.batch.forEach((message) => console.log(`${data.sentAt} : ${message}`)) + } + }); } function testIntegrations(): void { @@ -77,11 +141,27 @@ function testIntegrations(): void { 'Google Analytics': false } }); + + analytics.track({ + event: 'Upgraded Membershipt', + userId: '97234974', + integrations: { + 'All': false, + 'Vero': true, + 'Google Analytics': false + } + }, (err, data) => { + if (err) { + console.error(err); + } else { + data.batch.forEach((message) => console.log(`${data.sentAt} : ${message}`)) + } + }); } function testFlush(): void { analytics.flush(); - analytics.flush(function(err, batch) { + analytics.flush((err, batch) => { if (err) { alert("Oh nos!"); } else { console.log(batch.batch[0].type); } }); diff --git a/types/analytics-node/index.d.ts b/types/analytics-node/index.d.ts index fa0b8d6426..a1de134195 100644 --- a/types/analytics-node/index.d.ts +++ b/types/analytics-node/index.d.ts @@ -1,11 +1,36 @@ // Type definitions for Segment's analytics.js for Node.js // Project: https://segment.com/docs/libraries/node/ // Definitions by: Andrew Fong +// Thomas Thiebaud // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export = AnalyticsNode.Analytics; declare namespace AnalyticsNode { + interface Message { + type: string; + context: { + library: { + name: string; + version: string; + }, + [key: string]: any + }; + _metadata: { + nodeVersion: string; + [key: string]: any; + }, + timestamp?: Date; + messageId?: string; + anonymousId: string | number; + userId: string | number; + } + + interface Data { + batch: Message[], + timestamp: Date; + sentAt: Date; + } interface Integrations { [index: string]: boolean; @@ -25,7 +50,7 @@ declare namespace AnalyticsNode { timestamp?: Date; context?: Object; integrations?: Integrations; - }): Analytics; + }, callback?: (err: Error, data: Data) => void): Analytics; /* The track method lets you record the actions your users perform. */ track(message: { @@ -35,7 +60,7 @@ declare namespace AnalyticsNode { timestamp?: Date; context?: Object; integrations?: Integrations; - }): Analytics; + }, callback?: (err: Error, data: Data) => void): Analytics; /* The page method lets you record page views on your website, along with optional extra information about the page being viewed. */ @@ -47,14 +72,14 @@ declare namespace AnalyticsNode { timestamp?: Date; context?: Object; integrations?: Integrations; - }): Analytics; + }, callback?: (err: Error, data: Data) => void): Analytics; /* alias is how you associate one identity with another. */ alias(message: { previousId: string | number; userId: string | number; integrations?: Integrations; - }): Analytics; + }, callback?: (err: Error, data: Data) => void): Analytics; /* Group calls can be used to associate individual users with shared accounts or companies. */ @@ -66,16 +91,9 @@ declare namespace AnalyticsNode { timestamp?: Date; anonymous_id?: string | number; integrations?: Integrations; - }): Analytics; + }, callback?: (err: Error, data: Data) => void): Analytics; /* Flush batched calls to make sure nothing is left in the queue */ - flush(fn?: (err: Error, batch: { - batch: Array<{ - type: string; - }>; - messageId: string; - sentAt: Date; - timestamp: Date; - }) => void): Analytics; + flush(callback?: (err: Error, data: Data) => void): Analytics; } }