Update type definitions for analytics-node

This commit is contained in:
Thomas Thiebaud
2018-02-07 21:50:38 +01:00
parent a593fd2d60
commit dd7f22c76e
2 changed files with 112 additions and 14 deletions

View File

@@ -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); }
});

View File

@@ -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 <https://github.com/fongandrew>
// Thomas Thiebaud <https://github.com/thomasthiebaud>
// 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;
}
}