Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active February 23, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/aade98351af28b544ee167fa4b59c166 to your computer and use it in GitHub Desktop.
Save typeoneerror/aade98351af28b544ee167fa4b59c166 to your computer and use it in GitHub Desktop.
drip-es6.js
// @flow
/**
* {
* success: true,
* visitor_uuid: "f627ee608adb01315d1022000ab2058a",
* anonymous: false,
* email: "john@acme.com",
* custom_fields: { "name": "John" },
* tags: ["Customer"],
* lead_score: 35
* }
*/
const DRIP_ACTIVE = true;
export type DripParams = {
[string]: mixed
}
export type DripPayload = {
success: boolean,
visitor_uuid: string,
anonymous: boolean,
email: string,
custom_fields: { [string]: mixed },
tags: Array<string>,
lead_score: number
}
function dripDebug(method, ...params) {
console.debug(`[DRIP: ${method}]`, params);
}
export function identify(params: DripParams = {}) {
return new Promise(function (resolve, reject) {
if (!DRIP_ACTIVE) {
dripDebug('identify', params);
return resolve();
}
window._dcq.push(['identify', {
...params,
success: function (response) {
resolve(response);
},
failure: function (response) {
reject(response);
}
}]);
});
}
export function track(event: string, params: DripParams = {}) {
return new Promise(function (resolve, reject) {
if (!DRIP_ACTIVE) {
dripDebug('track', event, params);
return resolve();
}
window._dcq.push(['track', event, {
...params,
success: function (response) {
resolve(response);
},
failure: function (response) {
reject(response);
}
}]);
});
}
@typeoneerror
Copy link
Author

typeoneerror commented Feb 23, 2018

Use like:

// @flow

import * as Drip from './drip.js';
import type { DripPayload } from './drip.js';

Drip.identify({ email: 'me@example.com' }).then((visitor: DripPayload) => console.log(visitor));
Drip.track('event name');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment