14
D’accord, j’ai analysé le code JavaScript fourni. Voici une ventilation de ce qu’elle fait, avec les explications et des améliorations potentielles:
But global
Cet extrait de code est conçu pour charger et initialiser plusieurs scripts d’analyse de suivi tiers »et d’analyse sur une page Web. Plus précisément, il s’occupe:
- Facebook Pixel (Fbq): Pour le suivi des conversions et la construction du public pour la publicité Facebook.
- Google Tag Manager (GTM): Plus précisément, la balise de suivi de la conversion Google ADS.
- Survivre: Une rétroaction des clients et une plate-forme d’enquête.
Ventilation détaillée
1 et 1 loadFacebookPixel()
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 (expression de fonction rapidement invoquée): Le Code is enveloppé dans an iife pour créer une portée privée et éviter de polluer l’espace de noms global. C’est une bonne pratique.
* f (fenêtre), b (document), e (scénario): Ces alias communs sont window, documentet script objets, respectivement.
* v (URL de pixel): L’URL du fichier JavaScript Facebook Pixel.
* n (objet fbq): Cette variable tiendra le fbq Object, qui est l’interface principale pour interagir avec le pixel Facebook.
* t (élément de script): Une variable pour maintenir le créé 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 has been 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