Menu

User Source Tracking

Feature Introduction

Understanding where your users come from is crucial for effective marketing and growth tracking.

When users visit your application with UTM parameters or other tracking parameters (e.g., http://localhost:3000/?utm_source=nexty), the system automatically captures these parameters and stores them in browser cookies:

cookie

When users login for the first time, the values recorded in cookies are saved to the referral field in the user table, and the cookie is deleted simultaneously. If no tracking parameters were present when the user first visited (i.e., no cookies exist), the referral field defaults to direct, indicating organic or direct traffic.

referral

Customization and Extensions

Modifying Tracking Parameters

To customize which URL parameters are tracked, modify the referralParams array in your middleware.ts. Parameters are checked in order, with the first matching parameter taking priority:

middleware.ts
const referralParams = ['utm_source', 'ref', 'via', 'aff', 'referral', 'referral_code'];

By default, referral cookies are session-based and expire when the browser closes. To persist source information across browser sessions, configure a custom expiration time:

middleware.ts
intlResponse.cookies.set('referral_source', referralValue, {
  maxAge: 7 * 24 * 60 * 60 // 7 天
});