Google Analytics
Let track.js read the purchase from Google Analytics 4 with data-sale="ga4" – no code on the receipt page.
If your site already sends the purchase to Google Analytics 4 – via Google Tag Manager (dataLayer) or gtag – track.js can read the order number and amount from there. Nobody needs to know the template's field names, and there is nothing to add to the receipt.
Turn it on
Choose Automatically from Google Analytics under Setup. With the store's own script (/t/DIN_BUTIK.js), our server adds the settings, so the line never changes. With the older track.js line, the settings are attributes:
<script src="https://app.affilyflow.com/track.js" data-store="DIN_BUTIK" data-sale="ga4" data-sale-vat="25" async></script>| Attribute | Value |
|---|---|
data-sale="ga4" | Read the purchase from Google Analytics 4. Without it, Google Analytics is ignored entirely. |
data-sale-vat | The VAT percentage included in the Google Analytics amounts. 25 if the amounts include Danish VAT; leave it out if they exclude VAT. |
data-sale-shipping | incl if shipping is always included in value, excl if it never is. Without it, the script guesses. |
Formats that are read
The purchase event is read in all three forms:
dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: '1008',
value: 675,
shipping: 49,
currency: 'DKK',
items: [{ item_id: '317', price: 375, quantity: 2 }]
}
});gtag('event', 'purchase', { transaction_id: '1008', value: 1000, currency: 'DKK' });A purchase event in dataLayer without an ecommerce object is read too – the fields are then taken directly from the event.
- Events already in
dataLayerwhen the script loads are read immediately. - Events pushed later are caught straight away – also when Tag Manager loads after track.js. In addition,
dataLayeris checked every second for 60 seconds. - Other events (
view_item,add_to_cart…) are ignored. - A purchase without
transaction_idis ignored. - Each order number is reported once per page; the browser's and server's duplicate guards stop the rest.
Fields used
| Field | Used for |
|---|---|
transaction_id | The order number. Required. |
value | The order amount. |
shipping | Shipping, subtracted when it is included in value. |
tax | The VAT/tax. When the event has it (Magento, PrestaShop, Shopware and most GA4 modules send it), the base is calculated from it instead of a flat rate – so mixed rates and tax-free exports come out right. tax: 0 is never taken as proof of 0 % tax. |
items[].price and items[].quantity | The sum of the item lines (quantity is 1 if missing). Used when value is missing, and to guess whether shipping is included. |
currency | The currency. If missing, the first item's currency is used, otherwise DKK. |
coupon and items[].coupon | The customer's discount codes. A personal discount code the store has given an affiliate earns commission, even without a click. |
tax is used when the event carries it (see the table above); otherwise VAT is taken out with data-sale-vat.
How the amount is calculated
- The starting point is
value. If it is 0 or missing, the item lines' sum is used. - Shipping is subtracted when
shippingis above 0 and eitherdata-sale-shipping="incl"is set, or (withoutexcl)valueis larger than the item lines' sum – then shipping is included invalue. - With
data-sale-vatit is divided by 1 + VAT/100. - The result is rounded to two decimals. If it isn't above 0, nothing is reported, and the console shows
Affilyflow: the purchase event has no amount – the sale was not recorded.
value | shipping | Item lines | Setting | Reported |
|---|---|---|---|---|
| 675 | 49 | 750 | VAT 25 | 540 |
| 799 | 49 | 750 | VAT 25 | 600 |
| 724 | 49 | 750 | VAT 25, shipping incl | 540 |
| 1000 | – | – | VAT 25 | 800 |
| 540 | – | – | no VAT | 540 |
The server recalculates
Besides the amount, the script sends Google Analytics' raw figures with the sale: value, shipping, tax and the item lines' sum. If the store has saved its Google Analytics setup under Setup in the dashboard (VAT and shipping), the server recalculates the amount with it – using exactly the same rule as above.
- A change to VAT or shipping in Setup applies from the next sale. The script doesn't need to be redeployed.
- When the event has
tax, the server calculates the goods excluding VAT from it instead of a flat rate – following the platform's shipping rule: on Magento 2 the shipping in Google Analytics is excluding VAT, on PrestaShop and Shopware 6 including. Setup saves the rule when the platform is chosen. - The server's amount is only used if it is above 0 and not above what Google Analytics itself reported. Otherwise the script's amount is used.
- Without a saved Google Analytics setup, the script's amount from the attributes is used.
- Only sales from the Google Analytics route are recalculated. Sales from the sale line,
affilyflow.conversion()and the integrations are untouched.
The live test under Setup shows the amount that arrived, so you can see whether VAT and shipping are right. See Testing and troubleshooting.
Consent and clicks
The Google Analytics route follows the same rules as the rest of track.js:
- Without an affiliate click in the browser nothing is sent – unless the purchase has a personal discount code.
- With
data-consent="required"the sale waits until the visitor has consented to marketing. See Consent and cookies.
Missing something?
Write to us – we answer quickly, and the page gets fixed.