This code snippet defines three functions: loadFBEvents, loadGtagEvents, and loadSurvicateJs. These functions are designed to load and initialize tracking scripts for Facebook, Google, and Survicate, respectively. LetS break down each function in detail:
1.loadFBEvents(isFBCampaignActive)
Purpose: Loads the Facebook Pixel and tracks a PageView event.
isFBCampaignActive: A boolean parameter. If false,the function instantly returns,preventing the Facebook Pixel from loading. This is a common pattern to avoid unneeded tracking when Facebook campaigns aren’t running.
IIFE (Immediately Invoked Function expression): The core logic is wrapped in an IIFE (function(f,b,e,v,n,t,s) { ...})(f, b, e, 'https://connect.facebook.net/en_US/fbevents.js', n, t, s);. This creates a private scope, preventing variable conflicts with other scripts on the page.
f: Represents the window object. b: Represents the document object.
e: Represents the string ‘script’.
v: Represents the URL of the Facebook Pixel script.
n: A variable to hold the fbq function (the Facebook Pixel API). t: A variable to hold the script element. s: A variable to hold the first script element.
Pixel Initialization:
Checks if f.fbq already exists. If it does, it means the Pixel is already loaded, so the function returns.
Creates the fbq function and sets up a queue for events to be executed after the Pixel is fully loaded.
Creates a element, sets its async and defer attributes to true (for non-blocking loading), and sets its src to the Facebook Pixel URL.
Inserts the script element into the of the document,before the first existing tag.
tracking:
fbq('init', '593671331875494');: Initializes the Facebook Pixel with the provided Pixel ID.
fbq('track', 'PageView');: Tracks a PageView event, indicating that a page has been loaded.
2. loadGtagEvents(isGoogleCampaignActive)
Purpose: Loads the Google Tag Manager (GTM) script for Google Analytics and other Google marketing tags.
isGoogleCampaignActive: A boolean parameter. If false, the function returns, preventing the GTM script from loading.
Duplicate Script Prevention:
`var id = document.getElementById('toi-plus-google-campaign');