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.

Time
about 10 min
You need
Admin access to Content → Design and Stores → Configuration
View as Markdown

Magento 2 (and Adobe Commerce) is tracked via the Google Analytics route: 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.

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.

Step 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.

Tracking code – goes on every pageHTML
<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.

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.

Step 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.

For developers: without Google Analytics

If you don't want to use Google Analytics, a developer can add the 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.

success.phtml · order confirmation onlyPHP
<?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.

Step 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.

Common problems

What you seeCause
Setup can't find the tracking codeThe code is on a different store view from the front page, or the cache hasn't been flushed.
Clicks, but never salesGoogle 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 highA 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.

Missing something?

Write to us – we answer quickly, and the page gets fixed.

Contact support