UNPKG

fbq

Version:
73 lines (72 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var initialized = false; exports.init = function (pixelId, advancedMatching, options) { if (advancedMatching === void 0) { advancedMatching = {}; } if (options === void 0) { options = { autoConfig: true }; } if (typeof window === 'undefined') return initialized; if (initialized) return initialized; if (typeof pixelId !== 'string' || pixelId.trim().length === 0) throw new TypeError('pixelId should be a non empty "string"'); if (typeof advancedMatching !== 'object') throw new TypeError('advancedMatching should be of type "object"'); if (typeof options !== 'object') throw new TypeError('options should be of type "object"'); /* tslint:disable */ // Facebook Pixel Code --> var v = (function (f, b, e, v, n, t, s) { if (f.fbq) return; n = f.fbq = function () { n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments); }; if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0; t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s); })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js'); // End Facebook Pixel Code --> /* tslint:enable */ var fbq = window.fbq; fbq('track', 'PageView'); if (options.autoConfig === false) { fbq('set', 'autoConfig', false, pixelId); } fbq('init', pixelId, advancedMatching); initialized = true; return initialized; }; exports.track = function (trackType, options) { if (typeof window !== 'undefined' && initialized && 'fbq' in window) { if (options) window.fbq('track', trackType, options); else window.fbq('track', trackType); return true; } else { return false; } }; exports.trackCustom = function (trackType, options) { if (typeof window !== 'undefined' && initialized && 'fbq' in window) { if (options) window.fbq('trackCustom', trackType, options); else window.fbq('trackCustom', trackType); return true; } else { return false; } };