From 4b3cd54ddfe0e9d043f96b976b5d060e5a151e6d Mon Sep 17 00:00:00 2001 From: Carlos Date: Sun, 25 Nov 2018 18:57:49 +0100 Subject: [PATCH 1/2] feat(types): adds mixpanel-browser types --- types/mixpanel-browser/index.d.ts | 105 ++++++++++++++++++ .../mixpanel-browser-tests.ts | 91 +++++++++++++++ types/mixpanel-browser/tsconfig.json | 23 ++++ types/mixpanel-browser/tslint.json | 1 + 4 files changed, 220 insertions(+) create mode 100644 types/mixpanel-browser/index.d.ts create mode 100644 types/mixpanel-browser/mixpanel-browser-tests.ts create mode 100644 types/mixpanel-browser/tsconfig.json create mode 100644 types/mixpanel-browser/tslint.json diff --git a/types/mixpanel-browser/index.d.ts b/types/mixpanel-browser/index.d.ts new file mode 100644 index 0000000000..ff4daf4240 --- /dev/null +++ b/types/mixpanel-browser/index.d.ts @@ -0,0 +1,105 @@ +// Type definitions for mixpanel-browser 2.23 +// Project: https://github.com/mixpanel/mixpanel-js +// Definitions by: Carlos López +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export type Persistence = 'cookie' | 'localStorage'; + +export type PushItem = Array; + +export interface Dict {[key: string]: any; } + +export interface XhrHeadersDef {[header: string]: any; } + +export interface HasOptedInOutOptions { + persistence_type: Persistence; + cookie_prefix: string; +} + +export interface ClearOptOutInOutOptions extends HasOptedInOutOptions { + cookie_expiration: number; + cross_subdomain_cookie: boolean; + secure_cookie: boolean; +} + +export interface InTrackingOptions extends ClearOptOutInOutOptions { + track: () => void; + track_event_name: string; + track_event_properties: Dict; +} + +export interface OutTrackingOptions extends ClearOptOutInOutOptions { + delete_user: boolean; +} + +export interface Config { + api_host: string; + app_host: string; + autrotrack: boolean; + cdn: string; + cross_subdomain_cookie: boolean; + persistence: Persistence; + persistence_name: string; + cookie_name: string; + loaded: (mixpanel: MixPanel) => void; + store_google: boolean; + save_referrer: boolean; + test: boolean; + verbose: boolean; + img: boolean; + track_pageview: boolean; + debug: boolean; + track_links_timeout: number; + cookie_expiration: number; + upgrade: boolean; + disable_persistence: boolean; + disable_cookie: boolean; + secure_cookie: boolean; + ip: boolean; + property_blacklist: string[]; + xhr_headers: XhrHeadersDef; + opt_out_tracking_by_default: boolean; + opt_out_tracking_persistence_type: Persistence; + opt_out_tracking_cookie_prefix: string; +} + +export interface People { + set(prop: Dict | string, to?: any, callback?: () => void): void; + set_once(prop: Dict | string, to?: any, callback?: () => void): void; + unset(prop: string[] | string, callback?: () => void): void; + increment(prop: Dict | string, by?: number, callback?: () => void): void; + append(prop: Dict | string, value?: any, callback?: () => void): void; + union(prop: Dict | string, value?: any, callback?: () => void): void; + track_charge(amount: number, properties?: Dict, callback?: () => void): void; + clear_charges(callback?: () => void): void; + delete_user(): void; +} + +export interface MixPanel { + alias(alias: string, original?: string): void; + clear_opt_in_out_tracking(options?: Partial): void; + disable(events?: string[]): void; + get_config(prop_name?: string): any; + get_distinct_id(): any; + get_property(property_name: string): any; + has_opted_in_tracking(options?: Partial): boolean; + has_opted_out_tracking(options?: Partial): boolean; + identify(unique_id?: string): any; + init(token: string, config?: Partial, name?: string): MixPanel; + opt_in_tracking(options?: Partial): void; + opt_out_tracking(options?: Partial): void; + push(item: PushItem): void; + register(props: Dict, days?: number): void; + register_once(props: Dict, default_value?: any, days?: number): void; + reset(): void; + set_config(config: Partial): void; + time_event(event_name: string): void; + track(event_name: string, properties?: Dict, callback?: () => void): void; + track_forms(query: string, event_name: string, properties?: Dict | (() => void)): void; + track_links(query: string, event_name: string, properties?: Dict | (() => void)): void; + unregister(property: string): void; + people: People; +} + +export const mixpanel: MixPanel; diff --git a/types/mixpanel-browser/mixpanel-browser-tests.ts b/types/mixpanel-browser/mixpanel-browser-tests.ts new file mode 100644 index 0000000000..046b96db5c --- /dev/null +++ b/types/mixpanel-browser/mixpanel-browser-tests.ts @@ -0,0 +1,91 @@ +import { mixpanel } from 'mixpanel-browser'; + +const lib = mixpanel.init('new token', { secure_cookie: true }, 'library_name'); +lib.track('event name'); +mixpanel.push(['register', { a: 'b' }]); +mixpanel.disable(); +mixpanel.track('Registered', {Gender: 'Male', Age: 21}); +mixpanel.track_links('#nav', 'Clicked Nav Link'); +mixpanel.track_forms('#register', 'Created Account'); +mixpanel.time_event('Registered'); +mixpanel.track('Registered', {Gender: 'Male', Age: 21}); +mixpanel.register({Gender: 'Female'}); +mixpanel.register({ + Email: 'jdoe@example.com', + 'Account Type': 'Free' +}); +mixpanel.register_once({ + 'First Login Date': new Date().toISOString() +}); +mixpanel.init('YOUR PROJECT TOKEN', { + loaded: (mixpanel) => { + const distinct_id = mixpanel.get_distinct_id(); + } +}); +mixpanel.alias('new_id', 'existing_id'); +mixpanel.alias('newer_id', 'new_id'); +mixpanel.init('YOUR PROJECT TOKEN', { + loaded: (mixpanel) => { + const user_id = mixpanel.get_property('user_id'); + } +}); +mixpanel.opt_in_tracking(); +mixpanel.opt_in_tracking({ + track_event_name: 'User opted in', + track_event_properties: { + Email: 'jdoe@example.com' + }, + cookie_expiration: 30, + secure_cookie: true +}); +mixpanel.opt_out_tracking(); +mixpanel.opt_out_tracking({ + cookie_expiration: 30, + secure_cookie: true +}); +const has_opted_in = mixpanel.has_opted_in_tracking(); +const has_opted_out = mixpanel.has_opted_out_tracking(); +mixpanel.clear_opt_in_out_tracking(); +mixpanel.clear_opt_in_out_tracking({ + cookie_expiration: 30, + secure_cookie: true +}); +mixpanel.people.set('gender', 'm'); +mixpanel.people.set({ + Company: 'Acme', + Plan: 'Premium', + 'Upgrade date': new Date() +}); +mixpanel.people.set_once('First Login Date', new Date()); +mixpanel.people.set_once({ + 'First Login Date': new Date(), + 'Starting Plan': 'Premium' +}); +mixpanel.people.unset('gender'); +mixpanel.people.unset(['gender', 'Company']); +mixpanel.people.increment('page_views', 1); +mixpanel.people.increment('page_views'); +mixpanel.people.increment('credits_left', -1); +mixpanel.people.increment({ + counter1: 1, + counter2: 6 +}); +mixpanel.people.append('pages_visited', 'homepage'); +mixpanel.people.append({ + list1: 'bob', + list2: 123 +}); +mixpanel.people.union('pages_visited', 'homepage'); +mixpanel.people.union({ + list1: 'bob', + list2: 123 +}); +mixpanel.people.union({ + list1: ['bob', 'billy'] +}); +mixpanel.people.track_charge(50); +mixpanel.people.track_charge(30.50, { + $time: new Date('jan 1 2012') +}); +mixpanel.people.clear_charges(); +mixpanel.people.delete_user(); diff --git a/types/mixpanel-browser/tsconfig.json b/types/mixpanel-browser/tsconfig.json new file mode 100644 index 0000000000..da5a025e50 --- /dev/null +++ b/types/mixpanel-browser/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mixpanel-browser-tests.ts" + ] +} diff --git a/types/mixpanel-browser/tslint.json b/types/mixpanel-browser/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mixpanel-browser/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 7a69b991ef4979496e1030d8fe0de0dd9b52b805 Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 26 Nov 2018 18:47:18 +0100 Subject: [PATCH 2/2] fix() --- types/mixpanel-browser/index.d.ts | 30 ++++++++++++++++--- .../mixpanel-browser-tests.ts | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/types/mixpanel-browser/index.d.ts b/types/mixpanel-browser/index.d.ts index ff4daf4240..c976330177 100644 --- a/types/mixpanel-browser/index.d.ts +++ b/types/mixpanel-browser/index.d.ts @@ -42,7 +42,7 @@ export interface Config { persistence: Persistence; persistence_name: string; cookie_name: string; - loaded: (mixpanel: MixPanel) => void; + loaded: (mixpanel: Mixpanel) => void; store_google: boolean; save_referrer: boolean; test: boolean; @@ -76,7 +76,7 @@ export interface People { delete_user(): void; } -export interface MixPanel { +export interface Mixpanel { alias(alias: string, original?: string): void; clear_opt_in_out_tracking(options?: Partial): void; disable(events?: string[]): void; @@ -86,7 +86,7 @@ export interface MixPanel { has_opted_in_tracking(options?: Partial): boolean; has_opted_out_tracking(options?: Partial): boolean; identify(unique_id?: string): any; - init(token: string, config?: Partial, name?: string): MixPanel; + init(token: string, config?: Partial, name?: string): Mixpanel; opt_in_tracking(options?: Partial): void; opt_out_tracking(options?: Partial): void; push(item: PushItem): void; @@ -102,4 +102,26 @@ export interface MixPanel { people: People; } -export const mixpanel: MixPanel; +export function alias(alias: string, original?: string): void; +export function clear_opt_in_out_tracking(options?: Partial): void; +export function disable(events?: string[]): void; +export function get_config(prop_name?: string): any; +export function get_distinct_id(): any; +export function get_property(property_name: string): any; +export function has_opted_in_tracking(options?: Partial): boolean; +export function has_opted_out_tracking(options?: Partial): boolean; +export function identify(unique_id?: string): any; +export function init(token: string, config?: Partial, name?: string): Mixpanel; +export function opt_in_tracking(options?: Partial): void; +export function opt_out_tracking(options?: Partial): void; +export function push(item: PushItem): void; +export function register(props: Dict, days?: number): void; +export function register_once(props: Dict, default_value?: any, days?: number): void; +export function reset(): void; +export function set_config(config: Partial): void; +export function time_event(event_name: string): void; +export function track(event_name: string, properties?: Dict, callback?: () => void): void; +export function track_forms(query: string, event_name: string, properties?: Dict | (() => void)): void; +export function track_links(query: string, event_name: string, properties?: Dict | (() => void)): void; +export function unregister(property: string): void; +export const people: People; diff --git a/types/mixpanel-browser/mixpanel-browser-tests.ts b/types/mixpanel-browser/mixpanel-browser-tests.ts index 046b96db5c..e43c7cd47e 100644 --- a/types/mixpanel-browser/mixpanel-browser-tests.ts +++ b/types/mixpanel-browser/mixpanel-browser-tests.ts @@ -1,4 +1,4 @@ -import { mixpanel } from 'mixpanel-browser'; +import mixpanel = require('mixpanel-browser'); const lib = mixpanel.init('new token', { secure_cookie: true }, 'library_name'); lib.track('event name');