X Enterprises
Composables

useReferralTracking

Detects referral codes from URL query parameters, persists them to a cookie, and exposes helpers to read, check, and clear the tracked referral.

useReferralTracking

Detects referral codes from URL query parameters and persists them in cookies so attribution survives page navigation. SSR-safe — cookie operations are guarded by import.meta.server checks. The referral param name and cookie name are configurable via app.config.ts under xAffiliate.

Usage

const {
  referralCode,
  referralSource,
  detectReferral,
  clearReferral,
  hasReferral,
  getReferralCode,
} = useReferralTracking()

Returns

KeyTypeDescription
referralCodestring | nullThe currently tracked referral code, or null if none.
referralSourcestring | nullThe UTM source parameter if present in the URL, or null.
detectReferral()voidRead the referral code from URL query params (using xAffiliate.referralParam) and persist it to the cookie. Falls back to the existing cookie value if no param is present.
clearReferral()voidRemove the referral code from state and delete the cookie.
hasReferral()booleanReturns true if a referral code is currently tracked.
getReferralCode()string | nullReturns the current referral code, or null.

Configuration

app.config.ts keyDefaultDescription
xAffiliate.referralParam"ref"URL query parameter name to read the referral code from.
xAffiliate.cookieName"x_affiliate_ref"Cookie name used to persist the referral code.
xAffiliate.cookieDays30Cookie expiration in days.

AI Context

composable: useReferralTracking
package: "@xenterprises/nuxt-x-affiliate"
use-when: >
  Capturing and persisting referral codes when users arrive via a referral
  link. Call detectReferral() in a plugin or layout on every page load —
  it reads the URL query param (default: "ref"), stores it in a cookie,
  and updates referralCode reactively. Use getReferralCode() or referralCode
  when submitting signup forms to associate the new user with their referrer.
  Call clearReferral() after a successful signup or conversion to avoid
  double-attribution.
Copyright © 2026