How do affiliate links really work

When accessing an affiliate link to a theme demo page (the one with the envato header), we can see in the url the parameter &ref=USERNAME.

Then, on the envato header we can see the same pattern.
The envato logo links to a URL with &ref=USERNAME as well as the button “Item details”.
Not quite sure how does the “Buy now” button works, as it seems to be using the Javascript click event to bring you to the themeforest cart (

So my questions are:

  • Does the “Buy now” button also transfers the affiliate ?
  • What if the theme demo has a button in it to buy the theme within the demo file? In that case the link won’t have the ref param in it. Will it still count as an affiliate link?
  • If it works by using only cookies, why is the ref parameter necessary in the envato logo and “Item details” buttons?

I know this might be a bit technical, but the page detailing how the affiliates work doesn’t provide much information and only talks about “cookies”.