📊Installation DataLayer Prestashop

Partie code

Tout d'abord insérez le code suivant dans le fichier head.tpl de votre thème, juste avant l'intégration du script de Google Tag Manager

<script>
    window.dataLayer = window.dataLayer || [];
</script>

En suite dans le fichier templates/checkout/order-confirmation.tpl, a la fin du block 'page_content_container' , ajoutez le code suivant en modifiant si besoin le nom des variables.

Attention la fonction stringToPrice permet de transformer les prix de type string fournis par smarty en nombre pour que analytics puisses calculer les revenus

{literal}
    <script>

    function stringToPrice(string){
        let prixString = `${string}`;
        let prixSansEuro = prixString.replace(" €", "").replace(",", ".");
        let prixNum = parseFloat(prixSansEuro);
      return prixNum;
    };
    // Send transaction data with a pageview if available
    // when the page loads. Otherwise, use an event when the transaction
    // data becomes available.
    dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
    dataLayer.push({
      event: "purchase",
      ecommerce: {
          transaction_id: '{/literal}{$order.details['reference']}{literal}',  // Transaction ID. Required for purchases and refunds.
          value: stringToPrice('{/literal}{$order.totals.total['value']}{literal}'),  // Total transaction value (incl. tax and shipping)
          items: [
            {/literal}
              {foreach from=$order.products item=product}
                {literal}
                  {
                    item_name: '{/literal}{$product['product_name']}{literal}',     // Name or ID is required.
                    item_id: '{/literal}{$product['product_reference']}{literal}',
                    price: stringToPrice('{/literal}{$product['price']}{literal}'),
                    quantity: stringToPrice('{/literal}{$product['product_quantity']}{literal}'),
                  },
                  {/literal}
              {/foreach}
            {literal}
          ]
        }
      }
    );
    </script>
  {/literal}

Google Tag Manager

Vérifiez que Google Analytics est bien lié (cf : Connecter Google Analytics)

Last updated