Skip to main content

Tracking Script Reference

Script attributes, runtime methods, automatic integrations, and events

Table of Contents

Tracking Script Reference

Install snippet

<script>
  (function (w, r) {
    w._abq = w._abq || [];
    w[r] =
      w[r] ||
      function () {
        (w[r].q = w[r].q || []).push(arguments);
      };
  })(window, "affiliatebase");
</script>
<script
  async
  src="https://cdn.jsdelivr.net/npm/@relay-capital/affiliatebase-tracking@1.0.9/dist/track.min.js"
  data-account-id="YOUR_ACCOUNT_ID"
></script>

Script attributes

AttributeRequiredNotes
data-account-idYesAccount id (data-account alias also supported)
data-api-urlNoOverride API base URL (useful for local dev)
data-debugNotrue enables client logs
data-affiliatebase-paramsNoComma-separated custom attribution params
data-paramsNoAlias for data-affiliatebase-params
data-domainsNoComma-separated domains for cross-domain referral tagging
data-request-timeout-msNoRequest timeout (default 8000)
data-respect-dntNotrue skips tracking requests when DNT is enabled

URL parameter behavior

Priority:

  1. ?referral=<REFERRAL_ID> (preloads existing referral id)
  2. ?via=<TOKEN> (canonical token parameter)
  3. Custom params from data-affiliatebase-params (for example ?partner=<TOKEN>)

Runtime methods

affiliatebase("convert", { email: "customer@example.com" });
affiliatebase("identify", { email: "customer@example.com" }); // alias

affiliatebase("source", "affiliate_token");
affiliatebase("track", "affiliate_token"); // alias

affiliatebase("ready", () => console.log("loaded"));

affiliatebase("debug", true);
affiliatebase("set_debug", true); // alias

affiliatebase("reset");
affiliatebase("clear"); // alias

affiliatebase("referral"); // returns referral id
affiliatebase("tracked"); // returns whether attribution tracking completed
affiliatebase("state"); // returns full attribution state

Global state

window.AffiliateBase.referral;
window.AffiliateBase.affiliate;
window.AffiliateBase.campaign;
window.AffiliateBase.coupon;
window.AffiliateBase.tracked;
window.AffiliateBase.version;

window.affiliatebase_referral; // legacy alias getter

Use window.AffiliateBase.referral or affiliatebase("state") for new integrations. The legacy alias remains available for older snippets, but new Stripe Checkout code should submit the generated window.AffiliateBase.referral value to your server.

Automatic integrations

Forms

Add data-affiliatebase to a form to auto-inject a hidden referral input when attribution exists. If the active referral includes an AffiliateBase coupon, the script also injects a hidden coupon input.

<form data-affiliatebase action="/signup" method="POST">
  ...
</form>

Optional custom hidden field name:

<form data-affiliatebase data-affiliatebase-param-name="referral_id">
  ...
</form>

Stripe Buy Buttons and Pricing Tables

The script sets client-reference-id on standard Stripe Buy Button and Pricing Table elements after a referred visitor is present.

<stripe-buy-button buy-button-id="buy_btn_xxx" publishable-key="pk_live_xxx"></stripe-buy-button>
<stripe-pricing-table pricing-table-id="prctbl_xxx" publishable-key="pk_live_xxx"></stripe-pricing-table>

For standard https://buy.stripe.com/... and https://link.payment/... links, the script appends client_reference_id=<generated_referral_id> after a referred visitor is present. For custom Stripe-hosted or redirected Payment Link URLs, add data-affiliatebase to the anchor as an explicit opt-in where AffiliateBase owns client_reference_id.

<a href="https://buy.stripe.com/...">Buy</a>

<a data-affiliatebase href="https://link.payment/...">Buy</a>

Cross-domain tagging

With data-domains="example.com,checkout.example.com", matching external links are tagged with referral=<referral_id>.

Browser events

  • AffiliateBase.initialized
  • AffiliateBase.tracked
  • AffiliateBase.reset

Example:

window.addEventListener("AffiliateBase.tracked", (event) => {
  console.log(event.detail.referral);
});

Local development

Point the script at your local API:

<script
  async
  src="/track.js"
  data-account-id="YOUR_ACCOUNT_ID"
  data-api-url="http://localhost:3000"
  data-debug="true"
></script>