2017-12-17 17:42:06 +09:00
|
|
|
import { addCallback } from 'meteor/vulcan:core';
|
|
|
|
|
|
|
|
export const initFunctions = [];
|
|
|
|
|
|
|
|
export const trackFunctions = [];
|
|
|
|
|
2017-12-17 20:59:26 +09:00
|
|
|
export const addInitFunction = f => {
|
|
|
|
initFunctions.push(f);
|
2017-12-17 17:42:06 +09:00
|
|
|
// execute init function as soon as possible
|
2017-12-18 09:57:17 +09:00
|
|
|
f();
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|
|
|
|
|
2017-12-17 20:59:26 +09:00
|
|
|
export const addTrackFunction = f => {
|
|
|
|
trackFunctions.push(f);
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|
|
|
|
|
2017-12-18 09:57:17 +09:00
|
|
|
export const track = async (eventName, eventProperties, currentUser) => {
|
|
|
|
for (let f of trackFunctions) {
|
2018-02-15 12:05:47 +09:00
|
|
|
try {
|
|
|
|
await f(eventName, eventProperties, currentUser);
|
|
|
|
} catch (error) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`// ${f.name} track error`);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(error);
|
|
|
|
}
|
2017-12-18 09:57:17 +09:00
|
|
|
}
|
|
|
|
};
|
2017-12-17 17:42:06 +09:00
|
|
|
|
2017-12-26 11:17:44 +09:00
|
|
|
export const addUserFunction = f => {
|
|
|
|
addCallback('users.new.async', f);
|
|
|
|
};
|
|
|
|
|
2017-12-17 20:59:26 +09:00
|
|
|
export const addIdentifyFunction = f => {
|
|
|
|
addCallback('events.identify', f);
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|
|
|
|
|
2017-12-17 20:59:26 +09:00
|
|
|
export const addPageFunction = f => {
|
|
|
|
const f2 = (empty, route) => f(route);
|
|
|
|
|
|
|
|
// rename f2 to same name as f
|
|
|
|
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
|
|
|
|
const descriptor = Object.create(null); // no inherited properties
|
|
|
|
descriptor.value = f.name;
|
2017-12-18 09:57:17 +09:00
|
|
|
Object.defineProperty(f2, 'name', descriptor);
|
2017-12-17 20:59:26 +09:00
|
|
|
|
|
|
|
addCallback('router.onUpdate', f2);
|
2017-12-17 17:42:06 +09:00
|
|
|
};
|