6 Cancer-Fighting Foods: Top Doc Reveals
Okay, this code snippet is responsible for loading and initializing several third-party analytics and survey tools: Facebook Pixel, Google Tag Manager (specifically for Google Ads conversion tracking), and Survicate. Let’s break down each function and its purpose.
1. loadFbEvents()
* Purpose: Loads the Facebook Pixel and initializes it. The Facebook pixel is used to track website visitor behavior and create custom audiences for advertising on Facebook and Instagram.
* How it works:
* It’s a self-executing anonymous function (IIFE) that takes several arguments (f,b,e,v,n,t,s). These represent the window object, document object, the string ‘script’, the Facebook Pixel URL, a variable to store the pixel array, a variable for the script element, and a variable for the existing script element.
* It checks if the _fbq object already exists. If not, it creates it. _fbq is the array that holds the commands to be sent to Facebook.
* It defines the push function to add commands to the _fbq array.
* It sets _fbq.loaded to true and _fbq.version to ‘2.0’.
* It creates a <script> element, sets its async and defer attributes to true (for non-blocking loading), and sets its src attribute to the facebook Pixel URL.
* It inserts the <script> element into the <head> of the document, before the first <script> tag.
* it calls fbq('init', '593671331875494') to initialize the pixel with the provided Pixel ID.
* It calls fbq('track', 'PageView') to track a page view event.
2. loadGtagEvents(isGoogleCampaignActive)
* Purpose: Loads the Google Tag Manager script for Google Ads conversion tracking. This is used to track conversions (e.g., sign-ups, purchases) that result from Google Ads campaigns.
* How it works:
* It takes a boolean argument isGoogleCampaignActive. If this is false, the function returns immediately, preventing the script from loading. This is a way to conditionally load the script onyl when needed.
* It checks if an element with the ID toi-plus-google-campaign already exists. If it does, it returns, preventing duplicate script loading.
* It’s another IIFE similar to loadFbEvents().
* It creates a <script> element,sets its async and defer attributes to true,and sets its src attribute to the Google Tag manager URL,including the Google Ads conversion ID (AW-877820074).
* It sets the id attribute of the script to toi-plus-google-campaign to prevent duplicate loading.
* It inserts the <script> element into the <head> of the document, before the first <script> tag.
3. loadSurvicateJs(allowedSurvicateSections = [])
* Purpose: loads the Survicate JavaScript library and initializes it.Survicate is a tool for collecting user feedback thru surveys and in-app messages.
* how it works:
* It takes an optional argument allowedSurvicateSections, which is an array of strings representing the sections of the website where Survicate should be loaded. If no argument is provided, it defaults to an empty array.
* It determines the current section of the website based on the URL path (window.location.pathname).