Skip to content

Instantly share code, notes, and snippets.

@rsukale
Last active February 11, 2019 20:17
Show Gist options
  • Save rsukale/c922a75dcb7586ab477c0fdbfe49828a to your computer and use it in GitHub Desktop.
Save rsukale/c922a75dcb7586ab477c0fdbfe49828a to your computer and use it in GitHub Desktop.
export default function createTracker(config, context) {
function addContext(data) {
const newContext = {...data, _parent: context};
return createTracker(config, newContext);
}
function trackEvent(eventName, payload) {
// Custom logic for your analytics backend.
console.log(`tracking event ${eventName}`, {...payload, _parent: context});
}
return {trackEvent, addContext, getConfig};
}
export default function createTracker(source) {
const config = typeof source.getConfig === 'function' ? source.getConfig() : source;
const _parent = typeof source.getContext === 'function' ? source.getContext() : undefined;
let context;
let tracker;
function getConfig() { return config; }
function getContext() { return context; }
const setContext = (ctx) => {
context = _parent ? {...ctx, _parent} : ctx;
return tracker;
}
function trackEvent(eventName, payload) {
// Custom logic for your analytics backend.
console.log(`tracking event ${eventName}`, {...payload, _parent: context});
}
tracker = {trackEvent, getContext, setContext, getConfig};
return tracker;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment