track.js
Complete reference for the tracking script – attributes, JavaScript API, what is stored in the browser, consent and errors.
track.js is Affilyflow's tracking script for webshops and websites. It stores the click when a customer lands from an affiliate link and reports the sale when the customer reaches the receipt.
Add the script
Add the line to <head> on every page – including the receipt page.
<script src="https://app.affilyflow.com/t/DIN_BUTIK.js" async></script>/t/DIN_BUTIK.js is the store's own script: the same track.js, with the store's settings from Setup (Google Analytics, VAT, shipping, consent) added by our server. Change a setting in Setup and it applies on your site within minutes – the line never changes. The older line still works, with the settings as attributes:
<script src="https://app.affilyflow.com/track.js" data-store="DIN_BUTIK" async></script>DIN_BUTIK is your store ID. The exact line with your ID is under Setup in the store dashboard – copy it from there.
Use async: the script must never hold the page back. The exception is the no-code lead trigger (data-lead-url), where the line must be without async – see Leads without code.
https://app.affilyflow.com/s.js is the same file as track.js under a neutral name. Both addresses work, now and in the future. See Neutral addresses and your own proxy.
Attributes
| Attribute | Required | Meaning |
|---|---|---|
data-store | Yes | Your store ID. Sent with every sale and must match the click's store, otherwise the sale is rejected as store_mismatch. |
data-consent="required" | No | Nothing is stored in the browser until the visitor has consented to marketing. See Consent. |
data-lead-url | No | The thank-you page of a lead program. When the page matches, a lead is reported automatically. See Leads without code. |
data-lead-form | No | A lead when a form has been submitted: auto or a list like bricks,cf7. See Leads from a form. |
data-lead-forms | No | Only with data-lead-form: the forms that count, e.g. bricks:zfocwa,cf7:42. Without the attribute, all count. |
data-cookie-domain | No | The domain the cookie is set on (e.g. shop.com). Without it, the script picks the broadest domain the browser allows. |
data-sale="ga4" | No | Read the purchase from Google Analytics 4 (dataLayer or gtag). See Google Analytics. |
data-sale-vat | No | Only with data-sale="ga4": the VAT percentage included in the Google Analytics amounts (e.g. 25). |
data-sale-shipping | No | Only with data-sale="ga4": incl if shipping is always included in value, excl if it never is. Without it, the script guesses. |
data-last-click-wins="off" | No | Turns Last click wins off. Without the attribute it is on. |
data-cross-domains | No | Your own other domains, comma-separated (e.g. app.company.com). The script adds the click ID to links to them. With /t/YOUR_STORE.js, set them under Setup instead. See Your own domains and sign-up. |
What the script stores
When a customer lands with ?afid= in the URL, the script stores the click ID both as a cookie and in localStorage, so it survives either one being cleared. If the site cleans the URL before the script runs (e.g. a router with "clean URLs"), afid is also read from the URL the page was requested with – the browser's navigation entry. A redirect without the parameter never yields a false value.
The affiliate link carries the same click ID in the alias ?afy=. If afid is missing – some ad-blocker filter lists remove it on every site, because Apple uses the same name – afy is read the same way. If both are in the URL, afid wins.
| Key | Content |
|---|---|
af_click | The click ID from ?afid= |
af_store | The store ID from data-store |
af_aff | Only for legacy direct links (?aff= + ?store=) |
af_t | Cookie only: the time of the click in seconds. Same lifetime as af_click. |
af_ln | Only when another affiliate network had the customer's most recent click: the network's name, e.g. awin. See Last click wins. |
af_q | localStorage only, and only briefly: a sale on its way to Affilyflow or waiting to be sent again. See Getting the sale through. |
The script also reads the af_srv cookie but never sets it. It is only set by the store's own server (see Tracking from your server) and means the server set the click cookie. With af_srv=1 the script never rewrites af_click or af_t – Safari would shorten a cookie written by JavaScript to about 7 days, even if the server set it first. localStorage is still written as a backup.
- Lifetime: at most 90 days from the click. The cookie's
max-ageis always what is left of the 90 days. - Return visits never extend the window. On every visit the cookie and localStorage are written again with the remaining time, so a deleted cookie is restored from localStorage – but the timestamp is always the click's.
- localStorage stores the value as
{ v, t }– the value and the time of the click. A value older than 90 days is deleted when read. - The cookie is set with
path=/andSameSite=Lax, and withSecureon https. - Domain: the cookie is set on the broadest domain the browser allows (
shop.comrather thanwww.shop.com), so the click follows the customer to a receipt on another subdomain. The script finds it with a short-lived test cookie (_af_d) that is deleted immediately. On an IP address orlocalhostthe exact host is used. - Memory: the click ID from the URL is also kept in the page's memory, so a sale on the same page works even when storage is blocked (e.g. a private window).
af_tlets the store's server renew the cookie on a return visit without extending it beyond 90 days from the click.
Only Affilyflow click IDs
Only an afid (or afy) that looks like ours (c_ followed by 4–64 characters: letters, digits, _ and -) is stored. If the store also uses another network with a parameter called afid, the foreign value can never overwrite an Affilyflow click.
Coverage
When a customer lands with a click ID, the script reports it to Affilyflow – and again when the click is stored in the browser, and if the customer actively declines marketing. The report only contains the click ID, the store ID and the consent state – nothing about the visitor. The landing report itself stores nothing in the browser, so it is also sent while the script waits for consent.
This is what Tracking coverage under Setup in the store dashboard is built on: how many clicks from affiliates reached a page with the script, were stored and became a sale. See Testing and troubleshooting.
affilyflow.conversion()
Report a sale from JavaScript. The call only works once track.js is loaded on the page.
affilyflow.conversion({ order: '1008', total: 499, currency: 'DKK' });| Field | Type | Meaning |
|---|---|---|
order | text | The order number. Used to stop duplicates. |
total | number or text | The amount commission is calculated on: goods after discount, excluding VAT and shipping. Text such as "1.249,00" is parsed, see Amounts as text. |
currency | text | Currency code, e.g. DKK or EUR. Upper-cased. Without it, the store's settlement currency is used. |
store | text | The store ID. Only used if the browser doesn't already have it from data-store. |
coupon | text or list | The customer's discount code(s). Several in one text are separated by commas. See Personal discount codes. |
items | list | Optional: the line items, { title, qty, price, product_id, sku, category, brand } – category as text ("Men > Shoes") or segments. Used for commission by category. GA4's fields (item_name, item_id, item_category…) work too. |
How the call behaves:
- Without an affiliate click in the browser nothing is sent – and no error is logged. A normal purchase without an affiliate is not an error. The exception is a personal discount code.
- The sale is sent as a
POSTwith the fields in the body – the order number and amount are never in the URL. The request is kept alive even if the page navigates away right after, and if it fails, it is sent again. See Getting the sale through. - With
data-consent="required"the call waits until consent is settled and is sent only then. - For a lead program: use
affilyflow.lead()– see Leads from your own code.
Personal discount codes
With coupon, a sale can be attributed to a personal discount code the store has given an affiliate:
affilyflow.conversion({ order: '1008', total: 499, currency: 'DKK', coupon: 'ANNA10' });- With a click the sale is sent as always, with the code included. A valid click always wins over the code. If the click has expired or is unknown, a personal code can still attribute the sale.
- Without a click the script fetches a list of one-way fingerprints (SHA-256) of the store's personal codes and compares. Only if a code is on the list, and there is an order number, is the sale sent – with the codes that matched. A general code or no code sends nothing.
- The code needs no storage in the browser. With
data-consent="required"a sale with a code waits at most 3 seconds for consent; without consent the code can still attribute the sale.
Codes are compared without spaces and in upper case. A general code for all affiliates earns no commission on its own – only the link does. The sale line has the same option with data-affilyflow-coupon, and with data-sale="ga4" the coupon of the Google Analytics purchase is read.
<script>
(function () {
var tries = 0;
(function send() {
if (!window.affilyflow) { if (tries++ < 50) setTimeout(send, 200); return; }
affilyflow.conversion({ order: '1008', total: 499, currency: 'DKK' });
})();
})();
</script>Amounts as text
A number is used as is. Text that is a valid machine number is also used as is. Other text is cleaned: everything except digits, dot, comma and minus is removed, and the last separator is the decimal mark. If there is only one separator with exactly three digits after it, it is a thousands separator.
Sent as total | Read as |
|---|---|
499 | 499 |
"1249.5" | 1249.5 |
"249.125" | 249.125 |
"1.249,00" | 1249 |
"1.249,-" | 1249 |
"499,00 kr." | 499 |
"kr. 499" | 499 |
"1,234.56" | 1234.56 |
Rejected placeholders
If the template's placeholder is still in order, the sale is sent without an order number – a placeholder must never take up an order number, or every later sale would be rejected as a duplicate. These values count as placeholders (case-insensitive):
DIN_ORDRE_ID,DIT_ORDRE_ID,ORDRE_ID,ORDER_ID,YOUR_ORDER_ID,ORDERIDXX,XXXetc. (two or more X)- Anything in
{{ … }},[ … ],< … >or% … %– a template variable that wasn't filled in
The server applies the same rule.
Getting the sale through
A sale must not be lost because a connection drops for a moment, or because the customer closes the receipt too quickly:
- The request: the script sends the sale with
fetchas aPOSTto/e/c, with the fields as JSON in the body andkeepalive, so the request arrives even if the page navigates away immediately. The body is sent as text, so the browser doesn't need a CORS preflight first. - The response is read: a
2xxresponse or a final4xx(e.g.404for an unknown click) means the sale is done. A5xx,408,429or a network error means it must be sent again. - Sent again: with an order number – and when the script may store data in the browser – the sale is put in
af_qin localStorage before it is sent. If the request fails, the sale is sent again on the customer's next page view on your site: at most 3 attempts in total within 24 hours. After that it is deleted. - Without an order number the sale is sent once and never again – the server can't recognise a duplicate without an order number.
- Older browsers: without
fetch,navigator.sendBeaconis used with the same body. If the browser can't keep a request alive when the page closes, a request still in flight is sent withsendBeacon. Without either (very old browsers) an invisible image request with the fields in the URL is used.
Sending again is safe: the server never counts the same order number twice in the same program and answers duplicate.
Duplicate guard in the browser
The server never counts the same order number twice in the same program. The browser also remembers what it has sent, so a reloaded receipt doesn't send again:
- With an order number: while the sale is on its way, it is in
af_q. Once the server has answered, the order number is kept in localStorage (af_sent) for 30 days. - Without an order number: kept in sessionStorage (
af_sent_s) as page and amount – the same receipt with the same amount is sent once per tab.
affilyflow.consent()
The store's own consent solution can control the script directly. Only relevant with data-consent="required".
affilyflow.consent(true); // store the click from the URL and send waiting sales
affilyflow.consent(false); // delete af_click, af_store, af_aff, af_t, af_ln, af_srv, af_sent and af_qIf the visitor says yes again on the same page, the click from the URL is stored again.
Consent
With data-consent="required" the script stores nothing in the browser – no cookie, no localStorage or sessionStorage, no test cookie – until marketing consent is given. Until then the click only lives in the page's memory and is not used. Without the attribute the script behaves as before.
<script src="https://app.affilyflow.com/track.js" data-store="DIN_BUTIK" data-consent="required" async></script>The script reads consent itself from:
| Source | What counts as yes |
|---|---|
affilyflow.consent(true) | The call |
window.affilyflowConsent | The value true |
| Shoporama's banner | shoporamaConsent.isAllowed('marketing') |
| Cookie Information | The category cookie_cat_marketing |
| Cookiebot | Cookiebot.consent.marketing |
| IAB TCF v2 | Publisher consent to purposes 1 and 7 (or GDPR doesn't apply) |
| DanDomain Classic's banner | CookiePolicy.allowTrackingCookies is true (or cookiesRequireConsent is false) |
| Smartweb/HostedShop's banner | The cookie_consent cookie contains MARKETING |
| Google Consent Mode v2 | ad_storage: 'granted' in dataLayer (consent default or update) |
- A yes works whether the banner answers before or after the script. Cookie Information's, Cookiebot's, Shoporama's, Complianz's, SEOPress's and WP Consent API's events and Consent Mode updates in
dataLayerare listened to throughout. DanDomain's and Smartweb's banners reload the page after the choice, so the answer is ready on load.window.affilyflowConsentis checked on load and for the first 10 seconds, and TCF is used when its API is present within those 10 seconds. - A no only deletes on an active action (Cookie Information's, Cookiebot's, Complianz's, SEOPress's and WP Consent API's events, TCF's
useractioncomplete, a Consent Modeupdatewithad_storage: deniedoraffilyflow.consent(false)) – never because the banner hasn't read its own cookie yet. - If you set
window.affilyflowConsentlater than 10 seconds after load, callaffilyflow.consent(true)instead.
See Consent and cookies for the setup.
Neutral addresses and your own proxy
Ad blockers like to remove files and calls with "track" in the name. So the script sends its calls to neutral addresses next to itself:
| Address | Used for |
|---|---|
/e/l | Landing and coverage |
/e/c | The sale (same as /api/track/conversion) |
/e/k | The list of fingerprints of the store's personal discount codes |
/s.js | The script itself – the same file as /track.js |
The sale is sent as a POST to /e/c, so a proxy must forward both GET and POST with the body – our recipes do. The old addresses keep working. If the store serves the script from its own domain through a proxy – e.g. https://shop.com/af/s.js forwarded to https://app.affilyflow.com/ – all calls go through the store's domain too, so an ad blocker only sees the store's own addresses. Snippets for nginx, Apache and Next.js are in Tracking from your server.
Last click wins
If you also use other affiliate networks, you should only pay one network per sale: the one that had the customer's most recent affiliate click. If the customer lands from another network's link after a click from Affilyflow, we step back:
- The script remembers the network's name in
af_ln(cookie and localStorage, at most 90 days from the landing). A new click from Affilyflow deletes it again. - The receipt still reports the purchase, but with the network's name. Affilyflow records it without commission and without a fee – no sale is created with us – so you can see how many orders another network had the most recent click on.
- Another network's most recent click also beats a personal discount code.
- If the customer lands with both our click and another network's marker in the same URL, our click wins.
The script recognises the same markers as the WooCommerce plugin: Partner-Ads (pacid, pa-partnerid), Tradedoubler (tduid), Adtraction (at_gd), Awin (awc), impact.com (im_ref, irclickid, irmp), CJ (cjevent), Rakuten (ranSiteID), Admitad (admitad_uid) and links tagged utm_medium=affiliate from a source other than Affilyflow. Upper and lower case in the parameter name don't matter.
It is on by default. Turn it off under Setup (Last click wins) or with data-last-click-wins="off" on the script line. The setting under Setup works without new code with the /t/DIN_BUTIK.js script; an older tag with track.js needs the attribute. With data-consent="required", af_ln is only stored after consent, like everything else.
Leads from a form
For lead programs the script can report a lead by itself when a form has been submitted – not when the button is clicked, but when the form tool reports that the submission succeeded. In Setup, Find your forms finds the forms on your site, and you choose which ones count. With your store's own script (/t/YOUR_STORE.js) the setting is included automatically – nothing changes on your site.
| Tool | Event the script listens to | Key |
|---|---|---|
| Bricks | bricks/form/success | bricks:<data-element-id> |
| Contact Form 7 | wpcf7mailsent | cf7:<form id> |
| WPForms | wpformsAjaxSubmitSuccess (jQuery) | wpforms:<id> |
| Gravity Forms | gform_confirmation_loaded (jQuery) | gravity:<id> |
| Elementor Pro | submit_success (jQuery) | elementor:<form_id> |
| Fluent Forms | fluentform_submission_success | fluent:<id> |
| HubSpot (embedded form) | onFormSubmitted | hubspot:<formId> |
<script src="https://app.affilyflow.com/track.js" data-store="YOUR_STORE" data-lead-form="bricks" data-lead-forms="bricks:zfocwa" async></script>- The lead is only reported with an affiliate click in the browser (or a personal discount code), and consent works as for sales.
- The same submission is reported at most once, even if the script is on the page twice.
- If the form sends people to a thank-you page instead of staying on the page, use the thank-you page. Typeform and plain forms without one of the tools have no event the script can see – use the thank-you page or your server.
Leads from your own code
If your developer has a place in the code where a sign-up is completed, the lead can be reported from there:
window.affilyflow && window.affilyflow.lead({ id: 'SIGNUP_ID' });idis optional but recommended: the same id is never counted twice. Use your own sign-up number – never an email address.valueandcurrencyare optional (e.g. an estimated value). The commission is always the program's fixed amount per lead.- Without an id the lead gets a new number, and the same page reports at most one lead per 10 seconds.
Leads without code
For lead programs the script can report a lead by itself when the customer reaches the thank-you page. Set the thank-you page in Setup (under Advanced) and it is included in your store's own script – or put the address in data-lead-url on the full tag and remove async, so the lead is sent before the thank-you page possibly redirects.
<script src="https://app.affilyflow.com/track.js" data-store="DIN_BUTIK" data-lead-url="/thanks"></script>/thanksmatches/thanksand/thanks/…, but not/thanksgiving.- A full URL (
https://shop.com/thanks) is read as its path. - If parameters are given (
/thanks?type=lead), all of them must be in the page's URL. - The front page alone (
/) only matches when parameters are given too. - The lead is reported once per session and only with an affiliate click in the browser.
- Each click gives at most one lead, unless the program has Several leads per click turned on. A rejected lead doesn't count.
- To report leads from your server instead (across devices, with qualification and rejection), see Leads and subscriptions.
Console errors
track.js only writes to the browser console when a sale from the sale line or Google Analytics could not be reported:
| Message | Cause |
|---|---|
Affilyflow: data-affilyflow-order must be the order number – the sale was not recorded. | The sale line's order number is missing or a placeholder. |
Affilyflow: data-affilyflow-total must be the amount (goods excl. VAT and shipping) – the sale was not recorded. | The sale line's amount is empty or 0. |
Affilyflow: the purchase event has no amount – the sale was not recorded. | The Google Analytics purchase had no amount. |
What each response from the server means is in Status codes. See Testing and troubleshooting to test the whole chain with the test link.
Safari and iPhone
Safari (ITP) deletes cookies and localStorage set from JavaScript after about 7 days without a visit to the site. track.js rewrites its storage on every visit, but cannot and should not get around the limits. Only a cookie set by the store's own server – such as the WooCommerce plugin or your own server – lives longer. This applies to every network, not just Affilyflow.
Legacy direct links
Links with ?aff= (or ?aff_id=) and ?store= instead of ?afid= still work. The script counts the click and stores af_aff and af_store. data-store always wins over ?store= in the URL, so a store that uses ?store= itself doesn't get its sales rejected.
Stripe Payment Links
If the page has links to buy.stripe.com, the script adds the click id to them as client_reference_id – on load and again the moment a link is clicked, so links rendered later get it too. Only when a click is stored, and never on top of a client_reference_id you set yourself. The Stripe webhook then sees the sale without any server code.
Your own domains and sign-up
For stores where the customer signs up in an app on another domain and pays later – typically a SaaS product with Stripe.
Your own domains. With data-cross-domains="app.company.com" (or the domains under Setup → Stripe when you use /t/YOUR_STORE.js), the script adds the click ID as ?afid= and ?afy= to links to those domains and their subdomains – on load, on links rendered later, and at the moment of the click. The script on the other domain stores the click as always. If your code changes page itself, affilyflow.decorate(url) gives the same address with the click on it. Only with a stored click and consent, never on links to the page itself.
Sign-up. <form data-affilyflow-signup> on the form where the customer signs up – the script finds the email field itself (data-affilyflow-signup="fieldname" if it has a special name). Or call affilyflow.identify(email) once the account is created. If the call may come before the script has loaded, use the queue: (window.affilyflowQueue = window.affilyflowQueue || []).push(['identify', email]).
The script sends only the click ID, the store and the sha256 of the normalised email (lower case, everything after + removed, Gmail without dots) – never the email itself. Nothing is sent without a stored click, without consent (with data-consent="required"), or when another network has the customer's latest click. When Stripe reports the payment without a click ID, the webhook finds the click through the buyer's email.
Missing something?
Write to us – we answer quickly, and the page gets fixed.