Last active
February 11, 2019 20:17
-
-
Save rsukale/c922a75dcb7586ab477c0fdbfe49828a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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