JavaScript Tag vs. Server API: Which Tracking Method Should You Use?

ReferralMath

3 min read
JavaScript Tag vs. Server API: Which Tracking Method Should You Use?

ReferralMath offers two ways to track affiliate referrals: a drop-in JavaScript tag and a server-to-server REST API. Both are production-ready. The right choice depends on your stack, your accuracy requirements, and how much control you need.

JavaScript Tag: The Quick Start

The JavaScript tag is a single script you add to your site. It handles everything client-side.

<script src="https://tag.referralmath.com/tag.js"
  data-program="your-program" async></script>

What It Does

  • Reads the referral code from the URL query parameter (e.g., ?ref=abc)
  • Sets a first-party cookie to persist attribution across sessions
  • Tracks impressions when the page loads
  • Tracks clicks when the referral link is first visited
  • Fires conversion events via a JavaScript call when a purchase or signup happens

Pros

  • Zero backend work — just paste the script tag
  • Works with any stack — static sites, WordPress, Shopify, SPAs
  • Automatic cookie management — handles attribution window for you
  • Live in minutes — no deployment needed beyond adding the tag

Cons

  • Ad blockers can prevent the script from loading
  • Client-side only — if your conversion happens server-side (e.g., webhook from Stripe), you can't fire it from the browser
  • Less control over deduplication and validation

REST API: The Reliable Path

The REST API lets you report conversions directly from your backend.

curl -X POST https://api.referralmath.com/t/conversion \
  -H "X-API-Key: your_key" \
  -d '{"type":"payment","referral_code":"abc","amount":49.99}'

What It Does

  • Server-to-server communication — no browser involved
  • You control when conversions are reported — after payment confirmation, after trial conversion, etc.
  • Automatic deduplication — the API prevents double-counting
  • API key authentication — secure and tamper-proof

Pros

  • Ad-blocker proof — runs entirely server-side
  • Higher accuracy — you report conversions only when you've confirmed them
  • Full control — validate, enrich, and filter events before sending
  • Works with webhooks — integrate with Stripe, Paddle, or any payment processor

Cons

  • Requires backend work — you need to write code to call the API
  • You manage the referral code — your app needs to capture and store the referral code from the URL
  • Cookie attribution is your responsibility — the API doesn't set cookies

Which Should You Choose?

ScenarioRecommended
Marketing site with a signup formJavaScript Tag
SaaS with server-side payment processingREST API
E-commerce with client-side checkoutJavaScript Tag
High-value B2B deals where accuracy is criticalREST API
You want to launch today with zero backend changesJavaScript Tag
You need to track offline or delayed conversionsREST API

Use Both

Many ReferralMath customers use both methods together:

  1. JavaScript tag for click tracking and cookie-based attribution
  2. REST API for conversion reporting from the backend

This gives you the best of both worlds: automatic click tracking with browser cookies, plus reliable server-side conversion attribution.

The tag captures the referral code and stores it in a cookie. When the user converts, your backend reads the cookie (or the referral code you stored in your database) and reports the conversion via the API.

Getting Started

Both methods are available on all plans. Check out the integration docs or start your free trial to try them out.