29
D’accord, j’ai analysé le code JavaScript fourni. Voici une ventilation de ce que fait, ainsi que des explications et des améliorations potentielles:
But général
Cet extrait de code est conçu To charger et initialiser plusieurs scripts de suivi tiers et d’analyse sur une page Web. Plus précisément, il s’occupe:
- Facebook Pixel (Fbq): Pour les conversions de suivi et de la construction du public pour la publicité Facebook.
- Google Tag Manager (GTM): Plus précisément, une balise de suivi de conversion Google Ads.
- Survivre: Une commentaire et une enquête client Platform.
Ventilation détaillée
1 et 1loadFacebookPixel()
function loadFacebookPixel() {
(function(f, b, e, v, n, t, s) {
//... (Facebook Pixel initialization code) ...})(f, b, e, 'https://connect.facebook.net/en_US/fbevents.js', n, t, s);
fbq('init', '593671331875494');
fbq('track', 'PageView');
}
* Iife (Immédiatement invoqué par fonction expression): Le code est enveloppé dans un iife pour créer une portée privée et éviter de Polluter l’espace de noms global. C’est une bonne pratique.
* f fenêtre), b (document), e (scénario): Ces alias communs pour le window, document et script objets, respectivement.
* v (URL de pixel): L’URL du fichier JavaScript Facebook Pixel.
* n (objet fbq): Cette variable tiendra le fbq objet, qui est l’interface principale for interagissant avec le facebook Pixel.
* t (élément de script): Une variable à maintenir La créée dynamiquement element.
* s (first script element): A variable to hold the first element in the document.
* Pixel Initialization:
* f._fbq = n;: Creates the _fbq object on the window object if it doesn’t already exist, and assigns the fbq object to it.
* n.push = n;: This is a clever trick to allow the fbq function to be called before the Pixel script has fully loaded. It essentially makes fbq an array-like object that can store commands.
* n.loaded = !0;: Sets a flag to indicate that the Pixel script is loaded.
* n.version = '2.0';: Sets the Pixel version.
* n.queue = [];: Creates an array to store commands that are executed after the Pixel script has loaded.
* Script Injection:
* t = b.createElement(e);: Creates a new element.
* t.async = !0;: Sets the async attribute to load the script asynchronously (non-blocking).
* t.defer = !0;: Sets the defer attribute to execute the script after the HTML has been parsed.
* t.src = v;: Sets the src attribute to the Pixel URL.
* s = b.getElementsByTagName(e)[0];: Gets the first element in the document.
* s.parentNode.insertBefore(t, s);: Inserts the new element before the first element.
* fbq('init', '593671331875494');: initializes the Pixel with your Pixel ID.
* **`fbq(‘
Related