FacebookPixel.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // FacebookPixel.js
  2. import React, { useEffect } from "react";
  3. export const fb_setPixelID = (pixelID,ex_id) => {
  4. if (!pixelID) return;
  5. const initFbq=(f, b, e, v, n, t, s)=>{
  6. if (f.fbq) return;
  7. n = f.fbq = function () {
  8. n.callMethod
  9. ? n.callMethod.apply(n, arguments)
  10. : n.queue.push(arguments);
  11. };
  12. if (!f._fbq) f._fbq = n;
  13. n.push = n;
  14. n.loaded = !0;
  15. n.version = "2.0";
  16. n.queue = [];
  17. t = b.createElement(e);
  18. t.async = !0;
  19. t.src = v;
  20. s = b.getElementsByTagName(e)[0];
  21. s.parentNode.insertBefore(t, s);
  22. }
  23. initFbq(
  24. window,
  25. document,
  26. "script",
  27. "https://connect.facebook.net/en_US/fbevents.js"
  28. )
  29. // window.fbq('set', 'autoConfig', false, pixelID);
  30. if(!!ex_id){
  31. window.fbq("init", pixelID, {'external_id': ex_id});
  32. }else{
  33. window.fbq("init", pixelID);
  34. }
  35. window.fbq("track", "PageView");
  36. };
  37. const standardEvents = [
  38. 'PageView',
  39. 'ViewContent',
  40. 'Search',
  41. 'AddToCart',
  42. 'AddToWishlist',
  43. 'InitiateCheckout',
  44. 'AddPaymentInfo',
  45. 'Purchase',
  46. 'Lead',
  47. 'CompleteRegistration'
  48. ];
  49. // fbPixel.js
  50. export const fb_trackPageView = () => {
  51. if (window.fbq) {
  52. window.fbq('track', 'PageView');
  53. }
  54. };
  55. /**
  56. * fbq('trackCustom', "firstpayD0", {external_id: data.udid})
  57. * @param eventName
  58. * @param params
  59. */
  60. export const fb_trackEvent = (eventName, params) => {
  61. if (window.fbq) {
  62. if (standardEvents.includes(eventName)) {
  63. window.fbq('track', eventName, params);
  64. }else{
  65. window.fbq('trackCustom', eventName, params);
  66. }
  67. }
  68. };