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
Add data-sale="ga4" to the script on every page. The exact line with your settings is under Setup in the store dashboard.
<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. |
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. |
tax is not read. 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 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.
- 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.
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.
- With
data-consent="required"the sale waits until the visitor has consented to marketing. See track.js.
Missing something?
Write to us – we answer quickly, and the page gets fixed.