# Magento 2

> Tracking on Magento 2 and Adobe Commerce via Google Analytics 4 – the tracking code in HTML Head and Magento's own Google Analytics module. No module from us yet.

Magento 2 (and Adobe Commerce) is tracked via the [Google Analytics route](https://www.affilyflow.com/en/docs/reference/google-analytics): the tracking code sits on every page, and Magento's own Google Analytics module sends the purchase to Google Analytics 4 from the order confirmation – with order number, amount, tax and shipping. The tracking code reads that, and we calculate commission on the goods excluding VAT and shipping.

> [!NOTE]
> We don't have a Magento module yet. This guide requires the store to have Google Analytics 4 switched on – and Google Analytics sends nothing when the customer declines statistics cookies, so that purchase is lost. The guide is written from Magento's documentation and source code and has not yet been verified on a real Magento store: run a test purchase before you rely on it.

## Before you start

- Admin access to **Content → Design** and **Stores → Configuration**.
- The snippets from **Program → Setup** in Affilyflow, where your store ID is already filled in. In this guide, `DIN_BUTIK` stands for your store ID.
- Setup usually recognises Magento on its own. If it is not selected, choose **Magento 2** under *Other systems*.

## 1. The tracking code on every page

Go to **Content → Design → Configuration**, click **Edit** next to your store view, open **HTML Head**, and paste the code into the **Scripts and Style Sheets** field. Save, and flush the cache under **System → Cache Management**.

```html title="Tracking code – goes on every page"
<script src="https://app.affilyflow.com/t/DIN_BUTIK.js" async></script>
```

If the store has several store views, add the code to the one customers shop in – or at Website level, so it applies to all of them.

> [!WARNING]
> If the store runs **Content Security Policy** in "restrict" mode, `app.affilyflow.com` must be allowed as a script source (`script-src`), otherwise the script won't run on the checkout and order confirmation pages. Adobe itself notes that JavaScript in this field needs a CSP allowance. Ask your developer if in doubt.

With a Hyvä or other custom theme, the field may be disabled. In that case your developer adds the line to the theme's head instead.

## 2. Switch on Google Analytics 4

Go to **Stores → Configuration → Sales → Google API → Google Analytics** (Magento's own `Magento_GoogleGtag` module, available since 2.4.5), set **Enable** to Yes, enter your **Measurement ID** (G-…) and save.

The module sends `purchase` to Google Analytics 4 on the order confirmation with `transaction_id` (the order number you see in the admin), `value`, `tax`, `shipping` and `currency`. If you already use a third-party GA4 module (Amasty, WeltPixel, Mageplaza …), that is enough – most of them send the same event.

**How we calculate:** Magento sends shipping *excluding* VAT, and `tax` as all of the VAT (including the VAT on shipping). The base is therefore `value − tax − shipping` = the goods excluding VAT and shipping, after discount. Setup stores this rule on the store when you choose Magento – there is nothing for you to configure.

> [!NOTE]
> Magento's cookie restriction is respected: without consent to statistics, Google Analytics isn't loaded and `purchase` never fires. We can't see that purchase.

### For developers: without Google Analytics

If you don't want to use Google Analytics, a developer can add the [sale line](https://www.affilyflow.com/en/docs/reference/sale-line) to the theme's `Magento_Checkout::success.phtml`. The block on the confirmation page doesn't hold the order itself (only `getOrderId()`), so the order is loaded through the checkout session. The amount is the order total without shipping, and `data-affilyflow-vat` removes the VAT – 25 is the Danish rate, change it to yours.

```php title="success.phtml · order confirmation only"
<?php $afOrder = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Checkout\Model\Session::class)->getLastRealOrder(); ?>
<div hidden data-affilyflow-order="<?= $block->escapeHtmlAttr($afOrder->getIncrementId()) ?>" data-affilyflow-total="<?= $block->escapeHtmlAttr(number_format((float) $afOrder->getGrandTotal() - (float) $afOrder->getShippingInclTax(), 2, '.', '')) ?>" data-affilyflow-vat="25" data-affilyflow-currency="<?= $block->escapeHtmlAttr($afOrder->getOrderCurrencyCode()) ?>"></div>
```

The line has not yet been verified on a real store – test it with a test purchase and check the amount in the live test. Use one route only: the line *or* Google Analytics – otherwise the same purchase can be reported twice.

## 3. Test

Go to the **Test tracking** step in Setup, press **Open test link** and place a test order in the tab that opens – accept statistics cookies in the banner. Stay in Setup – the live test shows the test click and the purchase with order number and amount, and asks whether the amount is right. A test purchase gives no commission and no fee. A purchase without a click from a link is not counted.

If the sale doesn't appear: check that Google Analytics 4 is switched on, that you accepted cookies, and that the tracking code is on the order confirmation page too (CSP). See also [Testing and troubleshooting](https://www.affilyflow.com/en/docs/store/testing-and-troubleshooting).

## Common problems

| What you see | Cause |
|---|---|
| Setup can't find the tracking code | The code is on a different store view from the front page, or the cache hasn't been flushed. |
| Clicks, but never sales | Google Analytics 4 isn't switched on, the customer declined statistics cookies, or CSP blocks the script on the order confirmation. |
| The amount in the live test is too high | A third-party GA4 module sends shipping including VAT. Write to us – we will adjust the shipping rule for the store. |

## Refunds

Magento doesn't report refunds or credit memos to us. You need to reject a refunded purchase yourself within the approval period – see [Approval and refunds](https://www.affilyflow.com/en/docs/store/approval-and-refunds).

Source: https://www.affilyflow.com/en/docs/store/magento
