JavaScript (Browser)
refport-js captures the refp_id click ID from the URL and persists it in a cookie for server-side attribution.
refport-js is a tiny, framework-agnostic browser library that handles the first step of referral tracking: reading the refp_id parameter from the landing URL and storing it in a cookie so it's available throughout the session.
Installation
npm install refport-js
# or
pnpm add refport-js
# or
yarn add refport-jsUsage
init(options?)
Call init() once when your app loads. It checks the current URL for a refp_id query parameter, writes it to a cookie if found, and optionally cleans the parameter from the URL.
import { init } from 'refport-js';
const result = init();
console.log(result.clickId); // the captured (or existing) click ID, or nullIf a cookie already exists (return visit), init() returns it without overwriting.
getClickId(cookieName?)
Reads the current click ID from the cookie without triggering any side-effects. Useful in components that need the click ID after init() has already run.
import { getClickId } from 'refport-js';
const clickId = getClickId(); // string | nullreset(options?)
Deletes the tracking cookie. Call this after a successful lead or sale event if you want to clear attribution state.
import { reset } from 'refport-js';
reset();Return value of init()
init() returns a RefportTrackingResult object:
| Field | Type | Description |
|---|---|---|
tracked | boolean | true if a new click ID was captured from the URL |
clickId | string | null | The active click ID (from URL or existing cookie) |
source | "url" | "cookie" | null | Where the click ID came from |
Options
All options are optional. Defaults match Refport's server-side expectations.
| Option | Type | Default | Description |
|---|---|---|---|
cookieName | string | "refp_id" | Cookie name to read/write |
paramName | string | "refp_id" | URL query parameter name to look for |
maxAge | number | 7776000 (90 days) | Cookie lifetime in seconds |
path | string | "/" | Cookie path |
domain | string | — | Cookie domain (e.g. .yoursite.com for subdomain sharing) |
sameSite | "Strict" | "Lax" | "None" | "Lax" | SameSite cookie attribute |
secure | boolean | auto-detected | Sets the Secure flag; defaults to true on HTTPS |
cleanUrl | boolean | true | Remove refp_id from the browser URL after capture |
Example with custom domain
If your app spans multiple subdomains (e.g. www.yoursite.com and app.yoursite.com), share the cookie across all of them:
init({ domain: '.yoursite.com' });TypeScript types
import type { RefportTrackingOptions, RefportTrackingResult } from 'refport-js';