Magento 2
Install our free Magento 2 module and paste the connect code – clicks and sales are tracked from your own server. New: test it on staging first.
- Setup
- about 15 minneeds server access
- Tracking via
- Our free module – from your server (new)
- You need
- Server access (or a developer), Magento’s cron and admin access to Stores → Configuration
What the integration reports
- SalesYes – once the invoice is paid
- RefundsYes – credit memos, full and partial
- Personal discount codesYes – automatically
On Magento 2 and Adobe Commerce you use our module, Affilyflow_Tracking. It remembers the affiliate click as a cookie from your own server and reports the sale from the server once the invoice is paid – so it isn't lost to an ad blocker or a cookie banner that blocks scripts. You don't touch the theme or the order confirmation page.
If you have no developer and no server access, you can use the route without the module with Google Analytics 4 instead.
Before you start
- Magento Open Source or Adobe Commerce 2.4.4 or later (2.4.x) and PHP 8.1 or later.
- Access to the server (SSH) or a developer who can run
bin/magento. - Magento cron must be running (
bin/magento cron:install). The module delivers sales from its own cron group,affilyflow. If your server only runs named groups withcron:run --group=…, add--group=affilyflow. - Outgoing HTTPS from the server to
app.affilyflow.com. - Your connect code. It is under Program → Setup once you have chosen Magento 2.
- The module is written for Luma and Hyvä. Other themes (Breeze and the like) need a test.
Step 1: Install the module
- Download the module from store.affilyflow.com/downloads/affilyflow-magento.zip. It's free.
- Unzip it in the Magento root folder. It lands in
app/code/Affilyflow/Tracking/. - Run the commands in the Magento root folder:
bin/magento module:enable Affilyflow_Tracking && bin/magento setup:upgradeIf the store runs in production mode, also run:
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
bin/magento cache:flushsetup:upgrade adds two columns to sales_order and quote (affilyflow_afid, affilyflow_last_network) and creates the table affilyflow_delivery – the queue sales are sent from.
With Composer
The repository is private. Write to info@affilyflow.com and we'll give you access and the address to use instead of <REPOSITORY_URL>. Then:
composer config repositories.affilyflow-magento vcs <REPOSITORY_URL>
composer require affilyflow/module-tracking
bin/magento module:enable Affilyflow_Tracking && bin/magento setup:upgradeStep 2: Connect the module
Go to Stores → Configuration → Affilyflow → Affiliate tracking, paste the connect code under Connection → Connect code and press Save Config. The module checks the code with us and shows Connected to your store. A code we don't recognise is not saved.
The status panel at the top then answers in one line: Ready, Everything works or Needs your attention with the one thing to fix (for example that cron isn't running, or that the server can't reach us).
If the store has several websites, each can be connected on its own: switch the scope to the website and paste the code there. To disconnect, clear the field and save.
Step 3: Test
Step 3 in Setup has two rows:
- The module connection. Press Test the module connection. The self-test asks the module to call us and shows whether it was recognised as your store, and which version you run.
- A full sale. Press Open test link (or Test tracking in the status panel in Magento, which opens the live test) and place a test order in the same browser. Use for example Check / Money order, and create the invoice under Sales → Orders → Invoice. The module reports the sale once the invoice is paid – via Magento cron, usually within a couple of minutes. The live test shows the click, the order number and the amount.
A test purchase gives no commission and no fee. An order that is never invoiced is never reported – nor is an unpaid bank transfer. If the self-test fails, the causes are in Testing and troubleshooting.
What the module does
- The click. When a customer lands with
?afid=(or the alias?afy=) from an affiliate link, your server sets the cookieaf_click(withaf_tandaf_srv) for 90 days – also in Safari, where cookies written from JavaScript are cut to about 7 days. Pages with?afid=are never stored in the full page cache (built-in or Varnish). The program's own cookie period is enforced on our server. - Whole pages only. A click is only stored when the shop is opened as a whole page – not when another site loads it in an iframe, as an image or from a script.
- The order. The click is saved on the order when the customer places it (storefront, REST checkout or GraphQL) – never from the admin, cron or the command line. The cart remembers the click as well, so an order a payment provider creates from its server gets it too.
- The sale. When an invoice of the order is paid, the sale is queued and sent by cron. Not when the order is placed: an unpaid bank transfer or a failed payment is never reported.
- The base. Per order line: the goods after discounts, excluding VAT and shipping, in the order's currency. Each line carries the SKU, category path and brand, so commission by category works.
- Delivery with retries. Each call is sent from the queue with the response read – never in the customer's request. Network errors and server errors are retried after 1 min, 5 min, 30 min, 2 h, 12 h and 24 h. The outcome is added as a comment on the order.
- The tracking code. The module adds the tracking code to every page itself (with a CSP whitelist for
app.affilyflow.com), so Setup can see the installation and measure where clicks are lost. It can be switched off under Tracking. The module reports the sale either way, and the script is not on the order confirmation, so the purchase is never reported twice. - Cookie Restriction Mode. With Magento's cookie restriction on and no consent from the visitor, the module sets no cookie. By default the click is kept in the visitor's session until they accept; this can be switched off.
- Heartbeat. Once a day the module checks in with its versions, so we can tell a quiet store from a lost connection.
Refunds and cancellations
A credit memo is reported as a partial refund and reduces commission and fee in proportion. When the whole order is refunded, the sale is rejected and the affiliate is notified. Cancelling the rest of a partly invoiced order reports the cancelled part the same way. A refund waits until its sale has been delivered.
Personal discount codes
A personal discount code gives commission, also without a click. The module fetches the list of your personal codes as one-way fingerprints (SHA-256) and sends an order without a click only when one of its codes is on the list – and only that code. Orders with general codes or no code never leave your shop.
Last click wins
If the customer last came through another network's link, the order isn't reported to us. The module recognises Partner-Ads, Tradedoubler, Adtraction, Awin, impact.com, CJ, Rakuten, Admitad and links marked utm_medium=affiliate (on by default under Tracking). The winner is in the cookie afw_last_network and on the order. The module only decides about Affilyflow – another network's own module that reports from the server can't be stopped by it.
What data the module sends
Click ID, order number, order value excluding VAT, the base, currency, order lines (name, quantity, price excluding VAT, line total, product ID, SKU, categories, brand), discount codes and two one-way fingerprints (SHA-256) of the customer's email – as typed and normalised – which are only compared with the affiliate's own email to catch self-referrals. For a new click only the click ID and your store are sent. Names, addresses, email addresses, phone numbers and payment data are never sent. All calls go over HTTPS from your server to app.affilyflow.com.
Lead programs
The module reports purchases, not leads. A lead program uses the tracking code and the lead setup in Setup – see Leads and subscriptions.
Update and uninstall
Update: unzip the new zip over the old one (or run composer update affilyflow/module-tracking) and run bin/magento setup:upgrade – in production mode also the three commands from step 1.
Uninstall: clear the connect code and save, run bin/magento module:disable Affilyflow_Tracking, delete app/code/Affilyflow/Tracking (or run composer remove affilyflow/module-tracking), and run bin/magento setup:upgrade. The columns and the queue table stay in the database until you drop them.
Without the module: the Google Analytics route
If you can't install the module, Magento can be 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. In Setup you choose the route without the module under Magento 2.
The snippets are in Program → Setup, where your store ID is already filled in. Here, DIN_BUTIK stands for your store ID.
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.
<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.
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 the route without the module – there is nothing for you to configure.
For developers: the receipt line
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.
<?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 without the module
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 without the module
| 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. |
Missing something?
Write to us – we answer quickly, and the page gets fixed.