# Installation DataLayer Prestashop

### Partie code

Tout d'abord insérez le code suivant dans le fichier <mark style="color:yellow;">`head.tpl`</mark> de votre thème, juste avant l'intégration du script de <mark style="color:green;">**Google Tag Manager**</mark>

<pre class="language-javascript"><code class="lang-javascript">&#x3C;script>
<strong>    window.dataLayer = window.dataLayer || [];
</strong>&#x3C;/script>
</code></pre>

En suite dans le fichier <mark style="color:yellow;">`templates/checkout/order-confirmation.tpl`</mark>, a la fin du block 'page\_content\_container' , ajoutez le code suivant en modifiant si besoin le nom des variables.

{% hint style="info" %}
Attention la fonction stringToPrice permet de transformer les prix de type string fournis par smarty en nombre pour que analytics puisses calculer les revenus
{% endhint %}

```javascript
{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 <mark style="color:green;">**Google Analytics**</mark> est bien lié (cf : [Connecter Google Analytics](/developpement-web/seo-and-marketing/google-tag-manager/connecter-google-analytics.md))


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.la-ligne-web.com/developpement-web/conversions-ads/installation-datalayer-prestashop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
