Use this when the checkout opens over your own page — LemonSqueezy's overlay, opened from a product link or a button, with no server code of your own.
There is no server call to attach data to, so the ids go on the checkout URL instead. The tag has already set them by the time anyone can click.
<script src="https://app.lemonsqueezy.com/js/lemon.js" defer></script>
<button id="buy">Buy now</button>
<script>
document.getElementById("buy").addEventListener("click", function () {
// Read at CLICK time, not when the page was built. On a first visit the
// tag may not have run yet when the HTML is parsed.
var vid = window.truestat && window.truestat.visitorId;
var vs = window.truestat && window.truestat.sessionId;
var url = "https://yourstore.lemonsqueezy.com/buy/VARIANT-UUID";
if (vid) url += "?checkout[custom][ts_vid]=" + encodeURIComponent(vid);
if (vs) url += "&checkout[custom][ts_vs]=" + encodeURIComponent(vs);
window.LemonSqueezy.Url.Open(url);
});
</script>The bracket syntax is LemonSqueezy's, not ours. checkout[custom][ts_vid]
is how their checkout URL nests a value; it arrives on the order as
custom_data.ts_vid, which is the same place the
Checkout API puts it.
A plain product link works too
If you are not using lemon.js at all and simply link to the store, the same
parameters apply — but you have to build the link when it is clicked rather
than writing it into the HTML:
<a id="buy" href="https://yourstore.lemonsqueezy.com/buy/VARIANT-UUID">Buy</a>
<script>
document.getElementById("buy").addEventListener("click", function (e) {
var vid = window.truestat && window.truestat.visitorId;
if (vid) e.currentTarget.href += "?checkout[custom][ts_vid]=" + encodeURIComponent(vid);
});
</script>What this cannot do
It depends on your page's JavaScript running. A customer with scripts blocked reaches the same checkout with no ids attached, and that payment arrives unattributed. The sale is never lost — only its source.
If your server creates the checkout, prefer the Checkout API: the ids are read from a cookie the browser cannot forge, and nothing depends on client-side code.