Refport
SDKs

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.

Manual installation

Add the browser tracker to the <head> of your site when you want a no-build, framework-agnostic install:

<script
  src="https://cdn.jsdelivr.net/npm/refport-js@0.4.0/dist/auto.iife.js"
  defer
  data-query-param="refp_id"
  data-cookie-name="refp_id"
></script>

The script runs automatically, captures refp_id from the landing URL, stores it in a first-party cookie, and exposes window._refport for verification.

For shared cookies across subdomains, add a cookie domain:

Use a leading dot for shared attribution cookies: .example.com, not example.com. This is the setting to use when visitors can move between example.com and app.example.com before converting.

<script
  src="https://cdn.jsdelivr.net/npm/refport-js@0.4.0/dist/auto.iife.js"
  defer
  data-query-param="refp_id"
  data-cookie-name="refp_id"
  data-cookie-domain=".yoursite.com"
></script>

Verify the install by opening your site with ?refp_id=test, checking window._refport in the browser console, and confirming the refp_id cookie exists.

Script attributes

AttributeDescription
data-query-paramURL query parameter to capture
data-query-paramsJSON array or comma-separated query parameters
data-cookie-nameCookie name
data-cookie-domainCookie domain, such as .yoursite.com
data-cookie-pathCookie path
data-max-ageCookie max age in seconds
data-same-siteCookie SameSite value: Strict, Lax, or None
data-secureSet to true or false to override the Secure flag
data-clean-urlSet to false to keep the query parameter in the URL
data-cookie-optionsJSON object with cookie options like expiresInDays

Bundler installation

npm install refport-js
# or
pnpm add refport-js
# or
yarn add refport-js

Usage

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 null

If 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 | null

reset(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:

FieldTypeDescription
trackedbooleantrue if a new click ID was captured from the URL
clickIdstring | nullThe active click ID (from URL or existing cookie)
source"url" | "cookie" | nullWhere the click ID came from

Options

All options are optional. Defaults match Refport's server-side expectations.

OptionTypeDefaultDescription
cookieNamestring"refp_id"Cookie name to read/write
paramNamestring"refp_id"URL query parameter name to look for
maxAgenumber7776000 (90 days)Cookie lifetime in seconds
pathstring"/"Cookie path
domainstringCookie domain (e.g. .yoursite.com for subdomain sharing)
sameSite"Strict" | "Lax" | "None""Lax"SameSite cookie attribute
securebooleanauto-detectedSets the Secure flag; defaults to true on HTTPS
cleanUrlbooleantrueRemove 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" });

Keep the leading . in production, for example .example.com, so the tracking cookie is shared across the apex domain and subdomains.

TypeScript types

import type { RefportTrackingOptions, RefportTrackingResult } from "refport-js";

On this page