Merge pull request #6097 from fongandrew/af/segment-node

Type defs + test for Segment's analytics library for Node.js
This commit is contained in:
Masahiro Wakame
2015-10-07 02:04:13 +09:00
2 changed files with 155 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
/// <reference path="./analytics-node.d.ts" />
var analytics: AnalyticsNode.Analytics;
import Analytics = require("analytics-node");
function testConfig(): void {
analytics = new Analytics('YOUR_WRITE_KEY', {
flushAt: 20,
flushAfter: 10000
});
}
function testIdentify(): void {
analytics.identify({
userId: '019mr8mf4r',
traits: {
name: 'Michael Bolton',
email: 'mbolton@initech.com',
plan: 'Enterprise',
friends: 42
}
});
}
function testTrack(): void {
analytics.track({
userId: '019mr8mf4r',
event: 'Purchased an Item',
properties: {
revenue: 39.95,
shippingMethod: '2-day'
}
});
}
function testPage(): void {
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'
}
});
}
function testAlias(): void {
// the anonymous user does actions ...
analytics.track({ userId: 'anonymous_user', event: 'Anonymous Event' })
// the anonymous user signs up and is aliased
analytics.alias({ previousId: 'anonymous_user', userId: 'identified@gmail.com' })
// the identified user is identified
analytics.identify({ userId: 'identified@gmail.com', traits: { plan: 'Free' } })
// the identified user does actions ...
analytics.track({ userId: 'identified@gmail.com', event: 'Identified Action' })
}
function testGroup(): void {
analytics.group({
userId: '019mr8mf4r',
groupId: '56',
traits: {
name: 'Initech',
description: 'Accounting Software'
}
});
}
function testIntegrations(): void {
analytics.track({
event: 'Upgraded Membershipt',
userId: '97234974',
integrations: {
'All': false,
'Vero': true,
'Google Analytics': false
}
});
}
+73
View File
@@ -0,0 +1,73 @@
// 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>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module AnalyticsNode {
interface Integrations {
[index: string]: boolean;
}
export class Analytics {
constructor(writeKey: string, opts?: {
flushAt?: number,
flushAfter?: number
});
/* The identify method lets you tie a user to their actions and record
traits about them. */
identify(message: {
userId: string | number;
traits?: Object;
timestamp?: Date;
context?: Object;
integrations?: Integrations;
}): Analytics;
/* The track method lets you record the actions your users perform. */
track(message: {
userId: string | number;
event: string;
properties?: Object;
timestamp?: Date;
context?: Object;
integrations?: Integrations;
}): Analytics;
/* 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;
category?: string;
name?: string;
properties?: Object;
timestamp?: Date;
context?: Object;
integrations?: Integrations;
}): Analytics;
/* alias is how you associate one identity with another. */
alias(message: {
previousId: string | number;
userId: string | number;
integrations?: Integrations;
}): Analytics;
/* Group calls can be used to associate individual users with shared
accounts or companies. */
group(message: {
userId: string | number;
groupId: string | number;
traits?: Object;
context?: Object;
timestamp?: Date;
anonymous_id?: string | number;
integrations?: Integrations;
}): Analytics;
}
}
declare module "analytics-node" {
export = AnalyticsNode.Analytics;
}