react-use-facebook-pixel
Version:
Type-safe wrapper for integrating Facebook Pixel
67 lines (66 loc) • 2.83 kB
JavaScript
var c = Object.defineProperty;
var h = (i, t, o) => t in i ? c(i, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : i[t] = o;
var n = (i, t, o) => h(i, typeof t != "symbol" ? t + "" : t, o);
const l = () => {
(function(i, t, o, s, e, r, d) {
i.fbq || (e = i.fbq = function() {
e.callMethod ? e.callMethod.apply(e, arguments) : e.queue.push(arguments);
}, i._fbq || (i._fbq = e), e.push = e, e.loaded = !0, e.version = "2.0", e.queue = [], r = t.createElement(o), r.async = !0, r.src = s, d = t.getElementsByTagName(o)[0], d.parentNode.insertBefore(r, d));
})(window, document, "script", "https://connect.facebook.net/en_US/fbevents.js");
};
var u = /* @__PURE__ */ ((i) => (i.AddPaymentInfo = "AddPaymentInfo", i.AddToCart = "AddToCart", i.AddToWishlist = "AddToWishlist", i.CompleteRegistration = "CompleteRegistration", i.Contact = "Contact", i.CustomizeProduct = "CustomizeProduct", i.Donate = "Donate", i.FindLocation = "FindLocation", i.InitiateCheckout = "InitiateCheckout", i.Lead = "Lead", i.Purchase = "Purchase", i.Schedule = "Schedule", i.Search = "Search", i.StartTrial = "StartTrial", i.SubmitApplication = "SubmitApplication", i.Subscribe = "Subscribe", i.ViewContent = "ViewContent", i.PageView = "PageView", i))(u || {});
class a {
constructor({ pixelID: t, debug: o = !0, pageViewOnInit: s = !0, autoConfig: e = !0 }) {
n(this, "autoConfig");
n(this, "pixelID");
n(this, "debug");
n(this, "consolePrefix", "[react-use-facebook-pixel]");
n(this, "initialized", !1);
n(this, "pageViewOnInit");
n(this, "externalId");
l(), this.pixelID = t, this.debug = o, this.pageViewOnInit = s, this.autoConfig = e;
}
init(t) {
this.initialized || (this.initialized = !0, window.fbq("set", "autoConfig", this.autoConfig, this.pixelID), window.fbq("init", this.pixelID, t), t.external_id && (this.externalId = t.external_id), this.pageViewOnInit && this.trackEvent("PageView"), this.debug && console.log(
this.consolePrefix,
(/* @__PURE__ */ new Date()).toLocaleTimeString(),
"Facebook Pixel initialized"
));
}
getExternalId() {
return this.externalId;
}
trackEvent(t, o, s) {
if (!this.initialized && this.debug) {
console.error(
this.consolePrefix,
(/* @__PURE__ */ new Date()).toLocaleTimeString(),
`
Error`,
`
You tried to track event before initialization`
);
return;
}
window.fbq("track", t, o, s), this.debug && console.log(
this.consolePrefix,
(/* @__PURE__ */ new Date()).toLocaleTimeString(),
`
Event tracked.`,
`
Event name: `,
t,
`
Event data: `,
o,
`
Event additiona data`,
s
);
}
}
export {
a as FacebookPixel,
u as TrackableEventNameEnum
};
//# sourceMappingURL=index.esm.js.map