import { deleteCookie, getCookie, setCookie } from "./cookie"; import type { RefportTrackingOptions, RefportTrackingResult } from "./types"; import { getUrlParam, removeUrlParam } from "./url"; const DEFAULTS = { cleanUrl: true, cookieName: "refp_id", // 90 days maxAge: 7_776_000, paramName: "refp_id", path: "/", sameSite: "Lax" as const, }; export function init( options: RefportTrackingOptions = {} ): RefportTrackingResult { const config = { ...DEFAULTS, ...options }; const secure = config.secure ?? (typeof location !== "undefined" && location.protocol === "https:"); const paramValue = getUrlParam(config.paramName); if (paramValue) { setCookie(config.cookieName, paramValue, { domain: config.domain, maxAge: config.maxAge, path: config.path, sameSite: config.sameSite, secure, }); if (config.cleanUrl) { removeUrlParam(config.paramName); } return { clickId: paramValue, source: "url", tracked: true }; } const cookieValue = getCookie(config.cookieName); if (cookieValue) { return { clickId: cookieValue, source: "cookie", tracked: false }; } return { clickId: null, source: null, tracked: false }; } export function getClickId(cookieName?: string): string | null { return getCookie(cookieName ?? DEFAULTS.cookieName); } export function reset( options: Pick = {} ): void { deleteCookie(options.cookieName ?? DEFAULTS.cookieName, { domain: options.domain, path: options.path ?? DEFAULTS.path, }); } export { createEmbed } from "./embed"; export type { RefportEmbedInstance, RefportEmbedOptions, RefportTrackingOptions, RefportTrackingResult, } from "./types";