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
| Key | Type | Description |
|---|---|---|
referralCode | string | null | The currently tracked referral code, or null if none. |
referralSource | string | null | The UTM source parameter if present in the URL, or null. |
detectReferral() | void | Read 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() | void | Remove the referral code from state and delete the cookie. |
hasReferral() | boolean | Returns true if a referral code is currently tracked. |
getReferralCode() | string | null | Returns the current referral code, or null. |
Configuration
app.config.ts key | Default | Description |
|---|---|---|
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.cookieDays | 30 | Cookie 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.
