# Google Tag Manager

> Set up tracking from Google Tag Manager with our tag template – with Google Consent Mode built in and without touching your site's code.

If your store uses Google Tag Manager (GTM), you can set up Affilyflow from there with our tag template instead of adding the script to your site's code. The template loads your store's own script – `app.affilyflow.com/t/DIN_BUTIK.js` with your settings from Setup – waits for consent in Google Consent Mode and can report the purchase too.

## What you need

- Access to edit and publish your container in Google Tag Manager.
- Your store ID. It is shown in **Program → Setup** – it is the name in the script address, `app.affilyflow.com/t/DIN_BUTIK.js`.
- The template: [affilyflow-gtm.tpl](https://store.affilyflow.com/downloads/affilyflow-gtm.tpl).

## 1. Import the template

1. Download [affilyflow-gtm.tpl](https://store.affilyflow.com/downloads/affilyflow-gtm.tpl).
2. Go to **Templates** in your container and press **New** under **Tag Templates**.
3. Open the **⋮** menu at the top right, choose **Import** and select the file.
4. Press **Save**. The template is now called **Affilyflow**.

Under **Permissions** you can see what the template may do: load `https://app.affilyflow.com/t/…`, read consent for `ad_storage` and `analytics_storage`, and call `affilyflow.conversion` and `affilyflow.consent`. GTM enforces it – the template can do nothing else.

## 2. Tracking on every page

1. Go to **Tags**, press **New** and choose **Affilyflow** under **Tag Configuration**.
2. Enter your store ID under **Store ID** and choose **Tracking script on every page**.
3. Choose the trigger **All Pages**.
4. Save the tag and publish the container.

If the script is already in your site's code, remove it once the tag is published. Otherwise it loads twice.

## 3. Consent

Under **Consent** you choose when the script may load:

| Choice | What happens |
|---|---|
| **Only after consent in Google Consent Mode** (default) | The script only loads once `ad_storage` is granted – or both `ad_storage` and `analytics_storage`, if you choose that. If the customer says yes later on the same page, it loads straight away. An active no on the page deletes the click. |
| **Always – the script waits for consent itself** | The script always loads but stores nothing before a yes. Requires **Cookie banner** to be set to **Wait for consent** in Setup. The script then reads Consent Mode, Cookiebot, Cookie Information and the other banners we know itself – and [personal discount codes](https://www.affilyflow.com/en/docs/store/personal-discount-codes) also work for customers who haven't answered. |

- The default assumes your cookie banner sets Google Consent Mode (a `default` and an `update`). Cookiebot, Cookie Information, Usercentrics, OneTrust and CookieYes do, for example. GTM treats consent that was never set as granted. If your banner doesn't set Consent Mode, use the other choice and **Wait for consent** in Setup.
- Either way the click is only stored on a yes. If a customer lands from an affiliate link and only answers on a later page, the click ID from the address is gone. That's true of every script that respects consent.
- More in [Consent and cookies](https://www.affilyflow.com/en/docs/store/consent-and-cookies).

## 4. The sale

There are two ways. The same order number only counts once, but choose one of them, so the amount is always calculated the same way.

**A. From Google Analytics 4.** If your store has GA4 e-commerce with `purchase` in the dataLayer, choose **Automatically from Google Analytics** under **How the sale is reported** in Setup. The script reads the purchase, and our server deducts VAT and shipping according to your settings. No extra tag is needed. See [Google Analytics 4](https://www.affilyflow.com/en/docs/reference/google-analytics).

**B. A purchase tag.** Create one more tag with **Affilyflow** and **Report a purchase**, and let your purchase event trigger it – for example the custom event `purchase`. Fill in the fields with variables. You create a variable such as `{{DLV - ecommerce.value}}` under **Variables → New → Data Layer Variable** with the name `ecommerce.value`.

| Field | Example | Meaning |
|---|---|---|
| Order number | `{{DLV - ecommerce.transaction_id}}` | The order number the customer sees. |
| Order value | `{{DLV - ecommerce.value}}` | The goods after discount. |
| Shipping included in the order value | `{{DLV - ecommerce.shipping}}` | Only if the value includes shipping. |
| VAT included in the order value, in percent | `25` | Only if the value includes VAT. |
| Currency | `{{DLV - ecommerce.currency}}` | Empty = your billing currency. |
| Discount code | `{{DLV - ecommerce.coupon}}` | Only [personal discount codes](https://www.affilyflow.com/en/docs/store/personal-discount-codes) earn commission without a click. |

Commission is calculated on the goods without VAT and shipping: (value − shipping) ÷ (1 + VAT/100). A purchase of 724.00 with 49.00 shipping and 25 % VAT gives (724 − 49) ÷ 1.25 = 540.00. Both `724.00` and `724,00` are read correctly.

The purchase tag follows the same consent choice as the tag on every page.

## 5. Test

1. Press **Preview** in GTM and open your store in Tag Assistant.
2. Go to **Test tracking** in Setup and press **Open test link** in the same browser. Say yes to marketing in your banner if it asks.
3. In Tag Assistant, **Affilyflow** should be listed among the tags fired. Place a test order.
4. The live test in Setup shows the test click and the purchase with order number and amount. Check that the amount is the goods after discount, without VAT and shipping.

If the click doesn't come through, check that consent was given and that the tag fires on **All Pages**. The template writes its errors to the console in preview, for example when the order number is missing. More in [Testing and troubleshooting](https://www.affilyflow.com/en/docs/store/testing-and-troubleshooting).

## About the template

- The template is for web containers. We don't have a template for server-side GTM yet – to report the sale from your server, see [Server-side tracking](https://www.affilyflow.com/en/docs/store/server-side-tracking).
- The template isn't in Google's Community Template Gallery yet, so GTM doesn't show updates to it. If we change it, it says so here, and you import the new file over the old one.
- Changes in Setup – for example consent or the Google Analytics route – take effect within a few minutes without touching GTM, because the script fetches your settings.
- The template is for sales programs. If you have a lead program (for example sign-ups), use the script from Setup – it knows your lead page, the template doesn't.

Source: https://www.affilyflow.com/en/docs/store/google-tag-manager
