@chordcommerce/analytics
Version:
Chord Commerce event tracking
29 lines (28 loc) • 1.04 kB
JavaScript
/*
* Adds the Chord CDP snippet to the page.
*/
export const addCdpSnippet = (options) => {
if (!document || typeof document.createElement === 'undefined')
return;
const s = document.createElement('script');
s.async = true;
s.src = `${options.cdpDomain}/p.js`;
s.setAttribute('data-debug', `${!!options.debug}`);
s.setAttribute('data-init-only', 'true');
s.setAttribute('data-namespace', `_${options.namespace}`);
s.setAttribute('data-write-key', options.cdpWriteKey);
document.head.appendChild(s);
};
/*
* Returns the CDP queue to hold events ahead of CDP initialization.
*/
export const cdpQueue = (namespace) => {
var _a;
const q = window && (window[_a = `_${namespace}Q`] || (window[_a] = []));
return {
track: (...args) => q.push((cdp) => cdp.track(...args)),
identify: (...args) => q.push((cdp) => cdp.identify(...args)),
page: (...args) => q.push((cdp) => cdp.page(...args)),
reset: (...args) => q.push((cdp) => cdp.reset(...args)),
};
};