diff --git a/segment-analytics/segment-analytics-tests.ts b/segment-analytics/segment-analytics-tests.ts new file mode 100755 index 0000000000..b876ea1bed --- /dev/null +++ b/segment-analytics/segment-analytics-tests.ts @@ -0,0 +1,216 @@ +/// +/// + +// Some random vals to use + +// Use for page props or user traits +var testProps = { + favoriteCheese: "brie", + favoritePie: "apple" +}; + +// Segment options +var testOpts = { + integrations: { + Mixpanel: true + } +}; + +var testCb = function() {}; + + +///////////// + +function test_load() { + analytics.load("YOUR_WRITE_KEY"); +} + +function test_identify() { + // userId and traits + analytics.identify('1e810c197e', { + name: 'Bill Lumbergh', + email: 'bill@initech.com' + }); + + // No traits + analytics.identify('1e810c197e'); + + // No userId + analytics.identify({ + email: 'bill@initech.com', + newsletter: true, + industry: 'Technology' + }); + + // Callback + analytics.identify('1e810c197e', function(){ + // Do something after the identify request has been sent, like + // submit a form or redirect to a new page. + }); + + // With options + analytics.identify('1e810c197e', testProps, testOpts); + + // All args + analytics.identify('1e810c197e', testProps, testOpts, testCb); +} + +function testTrack() { + analytics.track('Signed Up'); + + analytics.track('Signed Up', { + plan: 'Startup', + source: 'Analytics Academy' + }); + + analytics.track('Signed Up', testProps, testOpts, testCb); +} + +function testPage() { + analytics.page(); + analytics.page('Signup'); + + analytics.page('Pricing', { + title: 'Segment Pricing', + url: 'https://segment.com/pricing', + path: '/pricing', + referrer: 'https://segment.com' + }); + + analytics.page('Category', 'Signup'); + + analytics.page('Signup', testProps, testOpts, testCb); +} + +function testAlias() { + analytics.alias('019mr8mf4r'); + analytics.alias('newId', 'oldId'); + analytics.alias('019mr8mf4r', testOpts, testCb); +} + +function testGroup() { + analytics.group('test_group'); + analytics.group('test_group', { + name: "Initech", + industry: "Technology", + employees: 329 + }); + analytics.group('test_group', testProps, testOpts, testCb); +} + +function testTrackLink() { + var link1 = document.getElementById('free-trial-link'); + var link2 = document.getElementById('free-trial-link-2'); + var links = $('.free-trial-links'); + + analytics.trackLink(link1, 'Clicked Free-Trial Link'); + analytics.trackLink(link1, 'Clicked Free-Trial Link', { + plan: 'Enterprise' + }); + + analytics.trackLink([link1, link2], 'Clicked Free-Trial Link', testProps); + analytics.trackLink(links, 'Clicked Free-Trial Link', testProps); + + // With function name and properties + analytics.trackLink(links, + function(elm) { + return String(elm); + }, + function(elm) { + return { + x: 123, + y: 456 + }; + }); +} + +function testTrackForm() { + var form1 = document.getElementById('signup-form'); + var form2 = document.getElementById('signin-form'); + var forms = $('.forms'); + + analytics.trackForm(form1, 'Signed up'); + analytics.trackForm(form1, 'Signed Up', { + plan: 'Premium', + revenue: 99.00 + }); + + analytics.trackForm([form1, form2], 'Clicked Free-Trial Link', testProps); + analytics.trackForm(forms, 'Clicked Free-Trial Link', testProps); + + // With function name and properties + analytics.trackForm(forms, + function(elm) { + return String(elm); + }, + function(elm) { + return { + x: 123, + y: 456 + }; + }); +} + +function testReady() { + analytics.ready(function(){ + ( window).mixpanel.set_config({ verbose: true }); + }); +} + +function testUserGroup() { + analytics.ready(function(){ + var user = analytics.user(); + var id = user.id(); + var traits = user.traits(); + }); + + analytics.ready(function(){ + var group = analytics.group(); + var id = group.id(); + var traits = group.traits(); + }); +} + +function testClearTraits() { + analytics.user().traits({}); + analytics.group().traits({}); +} + +function testResetLogout() { + analytics.reset(); +} + +function testAnonId() { + analytics.user().anonymousId(); + analytics.user().anonymousId('ABC-123-XYZ'); + + analytics.identify('123', { + gender: 'Male', + }, { + anonymousId: 'ABC-123-XYZ' + }); + + analytics.page({}, { anonymousId: 'ABC-123-XYZ' }); + + analytics.track('Clicked CTA', { + callToAction: 'Signup' + }, { + anonymousId: 'ABC-123-XYZ' + }); +} + +function testDebug() { + analytics.debug(); + analytics.debug(false); +} + +declare var bigdata: any; +function testEmitter() { + analytics.on('track', function(event, properties, options){ + bigdata.push(['recordEvent', event]); + }); +} + +function testTimeout() { + analytics.timeout(500); +} \ No newline at end of file diff --git a/segment-analytics/segment-analytics.d.ts b/segment-analytics/segment-analytics.d.ts new file mode 100755 index 0000000000..3cdfbbc560 --- /dev/null +++ b/segment-analytics/segment-analytics.d.ts @@ -0,0 +1,132 @@ +// Type definitions for Segment's analytics.js +// Project: https://segment.com/docs/libraries/analytics.js/ +// Definitions by: Andrew Fong +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module SegmentAnalytics { + + // Generic options object with integrations + interface SegmentOpts { + integrations?: any; + anonymousId?: string; + } + + // The actual analytics.js object + interface AnalyticsJS { + + /* Configure Segment with write key */ + load(writeKey: string): void; + + /* The identify method is how you tie one of your users and their actions + to a recognizable userId and traits. */ + identify(userId: string, traits?: Object, options?: SegmentOpts, + callback?: () => void): void; + identify(userId: string, traits: Object, callback?: () => void): void; + identify(userId: string, callback?: () => void): void; + identify(traits?: Object, options?: SegmentOpts, + callback?: () => void): void; + identify(traits?: Object, callback?: () => void): void; + identify(callback: () => void): void; + + /* The track method lets you record any actions your users perform. */ + track(event: string, properties?: Object, options?: SegmentOpts, + callback?: () => void): void; + track(event: string, properties?: Object, + callback?: () => void): void; + track(event: string, callback?: () => void): void; + + /* The page method lets you record page views on your website, along with + optional extra information about the page being viewed. */ + page(category: string, name: string, properties?: Object, + options?: SegmentOpts, callback?: () => void): void; + page(name?: string, properties?: Object, + options?: SegmentOpts, callback?: () => void): void; + page(name?: string, properties?: Object, callback?: () => void): void; + page(name?: string, callback?: () => void): void; + page(properties?: Object, options?: SegmentOpts, + callback?: () => void): void; + page(callback?: () => void): void; + + /* The group method associates an individual user with a group. The group + can a company, organization, account, project, team or any other name + you came up with for the same concept. */ + group(groupId: string, traits?: Object, options?: SegmentOpts, + callback?: () => void): void; + group(groupId: string, traits?: Object, callback?: () => void): void; + group(groupId: string, callback?: () => void): void; + + /* The alias method combines two previously unassociated user identities. + This comes in handy if the same user visits from two different devices + and you want to combine their history. + + Some providers also don’t alias automatically for you when an anonymous + user signs up (like Mixpanel), so you need to call alias manually right + after sign up with their brand new userId. */ + alias(userId: string, previousId?: string, options?: SegmentOpts, + callback?: () => void): void; + alias(userId: string, previousId?: string, callback?: () => void): void; + alias(userId: string, callback?: () => void): void; + alias(userId: string, options?: SegmentOpts, callback?: () => void): void; + + /* trackLink is a helper that binds a track call to whenever a link is + clicked. Usually the page would change before you could call track, but + with trackLink a small timeout is inserted to give the track call enough + time to fire. */ + trackLink(elements: JQuery|Element[]|Element, + event: string|{ (elm: Element): string }, + properties?: Object|{ (elm: Element): Object }): void; + + /* trackForm is a helper that binds a track call to a form submission. + Usually the page would change before you could call track, but with + trackForm a small timeout is inserted to give the track call enough + time to fire. */ + trackForm(elements: JQuery|Element[]|Element, + event: string|{ (elm: Element): string }, + properties?: Object|{ (elm: Element): Object }): void; + + /* The ready method allows you to pass in a callback that will be called as + soon as all of your enabled integrations have loaded. It’s like jQuery’s + ready method, except for integrations. */ + ready(callback: () => void): void; + + /* If you need to clear the user and group id and traits we’ve added a + reset function that is most commonly used when your identified users + logout of your application. */ + reset(): void; + + /* Once Analytics.js loaded, you can retrieve information about the + currently identified user or group like their id and traits. */ + user(): { + id(): string; + logout(): void; + reset(): void; + anonymousId(newId?: string): string; + traits(newTraits?: Object): void; + } + + group(): { + id(): string; + traits(newTraits?: Object): void; + } + + /* Analytics.js has a debug mode that logs helpful messages to the + console. */ + debug(state?: boolean): void; + + /* The global analytics object emits events whenever you call alias, group, + identify, track or page. That way you can listen to those events and run + your own custom code. */ + on(event: string, + callback: { + (event: string, properties: Object, options: SegmentOpts): void + }): void; + + /* You can extend the length (in milliseconds) of the method callbacks and + helpers */ + timeout(milliseconds: number): void; + } +} + +declare var analytics: SegmentAnalytics.AnalyticsJS;