window.affiliateId or guess at a delay.
Use
affiliate_id_ready when you need the attribution values. Use affiliate_referral_ready when you need to know that tracking has settled, whoever the visitor is.
Listen on window
Both events are dispatched onwindow. They do not bubble to document, so a listener attached to document never fires.
affiliate_id_ready
Fires as soon as the script has both a referral code and a click ID for the visitor. This is the momentwindow.affiliateId becomes trustworthy.
The referral code from the affiliate link — the value of
?ref=, or whichever parameter your program uses.The ID of the recorded click. This is the value you attach to a sale reported from your server.
affiliate_referral_ready instead and read the values there.
affiliate_referral_ready
A plainEvent that signals “the script has finished deciding”. It carries no detail — read the state yourself once it arrives:
When each event fires
Ordering and timing
affiliate_id_ready always fires first, then affiliate_referral_ready.
- On the paths that need no network call — no referral, or a referral already in storage —
affiliate_referral_readyis delayed by roughly 100 ms. That gap exists so code loaded alongside anasyncsnippet still has time to attach its listener. - On a fresh click,
affiliate_referral_readyfires immediately after the click is recorded, which can be several hundred milliseconds into the page depending on the network. - If the click request fails,
affiliate_referral_readystill fires, and the stored referral is cleared first. Your listener will seenullvalues.
Never miss the event
The safe pattern is to handle both orderings: check the current state once, and attach a listener for the case where the script has not finished yet.Cases where neither event fires
The script exits before dispatching anything when it cannot find a program to track against — that is, when the snippet has nodata-program-id attribute and there is no programId query parameter on the URL. If your listeners never run, check the snippet first. Turning on debug mode prints the program ID the script resolved.
Names reserved by Referly
The tracking script listens foraffiliate_referral_ready itself: that is how it decides whether to load your program’s popups and banners for a referred visitor. Do not dispatch either event yourself and do not reuse the names for your own events, or you will trigger Referly behaviour at the wrong moment.
Related
JavaScript API reference
The functions the script exposes and how to call them.
Globals
The values the script puts on the window.
Attribution
How a click becomes a credited sale.
Debug mode
Watch the script’s decisions in the console.