UNPKG

@mntm/stats

Version:

A package for integrating analytics tools

45 lines (44 loc) 1.26 kB
import { safeWindow } from '../dom.js'; import { loadScript, thenable } from '../utils.js'; /** * Implement FB Pixel * * @param code XXXXXXX */ export const createPixelFB = (code) => { const context = safeWindow; context.fbq = Object.assign(function fbq() { // eslint-disable-next-line prefer-rest-params const args = arguments; if (context.fbq.callMethod) { context.fbq.callMethod.apply(context.fbq, args); } else { context.fbq.queue.push(args); } }, { loaded: true, version: '2.0', queue: [] }); context.fbq.push = context.fbq; context._fbq = context.fbq; const load = loadScript('https://connect.facebook.net/en_US/fbevents.js'); const ready = load.then(() => { context.fbq('init', code, {}); context.fbq('track', 'PageView', {}); }); const pixel = { track(event, params) { return thenable(ready, () => { context.fbq('track', event, params || {}); }); }, custom(event, params) { return thenable(ready, () => { context.fbq('trackCustom', event, params || {}); }); } }; return pixel; };