
Security News
Ruby's Bundler 4.0.18 Extends Cooldown to bundle lock and bundle cache
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.
@affitor/tracker
Advanced tools
Official JavaScript/TypeScript SDK for Affitor affiliate tracking. A Promise-based wrapper around the Affitor tracker script, inspired by @stripe/stripe-js.
npm install @affitor/tracker
The SDK provides two entry points for different use cases:
| Entry Point | Import | Best For |
|---|---|---|
@affitor/tracker | import { loadAffitor } from '@affitor/tracker' | Any JS/TS application |
@affitor/tracker/react | import { AffitorProvider, useAffitor } from '@affitor/tracker/react' | React / Next.js apps |
@affitor/tracker — Core SDKloadAffitor(programId, options?)Loads the Affitor tracker script and returns a Promise that resolves to the tracker instance.
loadAffitor multiple times returns the same Promisenull on the serverimport { loadAffitor } from '@affitor/tracker';
const affitor = await loadAffitor('59');
| Option | Type | Default | Description |
|---|---|---|---|
env | 'production' | 'uat' | 'local' | 'production' | Environment preset (selects the tracker script URL) |
debug | boolean | false | Enable debug mode (verbose console logs, cookie verification) |
scriptUrl | string | — | Custom script URL (overrides env preset) |
env | Script URL |
|---|---|
'production' | https://api.affitor.com/js/affitor-tracker.js |
'uat' | https://uat-affitor-cms.vanilla-ott.com/js/affitor-tracker-uat.js |
'local' | http://localhost:1337/js/affitor-tracker-local.js |
// Production (default)
const affitor = await loadAffitor('59');
// Local development with debug
const affitor = await loadAffitor('29', { env: 'local', debug: true });
// UAT
const affitor = await loadAffitor('29', { env: 'uat' });
// Custom URL (overrides env)
const affitor = await loadAffitor('29', { scriptUrl: 'https://my-cdn.com/tracker.js' });
Once loaded, the affitor instance provides these methods:
| Method | Description |
|---|---|
signup(customerKey, email) | Track a signup event. Takes the user's ID and email as positional args. Returns a Promise. |
trackLead(data) | Deprecated. Alias for signup(). Accepts { email, user_id }. Use signup() instead. |
trackTest(data?) | Send a test event to verify tracking is working. |
redirectToCheckout(params) | Redirect the user to the Affitor-powered checkout page. |
| Property | Type | Description |
|---|---|---|
customerCode | string | null | The affiliate customer code from cookie |
affiliateUrl | string | null | The affiliate referral URL from cookie |
hasAffiliateAttribution | boolean | Whether the current visitor was referred by an affiliate |
debugMode | boolean | Whether debug mode is enabled |
programId | number | The advertiser program ID |
import { loadAffitor } from '@affitor/tracker';
async function handleSignup(email: string, password: string) {
// 1. Create the user account
const { data } = await supabase.auth.signUp({ email, password });
// 2. Track the signup — awaits script load, guaranteed to fire
if (data.user) {
const affitor = await loadAffitor('59');
await affitor?.signup(data.user.id, data.user.email);
}
// 3. Navigate to dashboard
router.push('/dashboard');
}
import { loadAffitor } from '@affitor/tracker';
async function handleUpgrade() {
const affitor = await loadAffitor('59');
affitor?.redirectToCheckout({
price: 19.99,
programId: 59,
});
}
@affitor/tracker/react — React HooksThe React entry point provides two patterns: Provider + hook and standalone hook.
AffitorProvider + useAffitor()Best when your whole app needs access to the tracker state (e.g. showing referral badges).
import { AffitorProvider, useAffitor } from '@affitor/tracker/react';
// 1. Wrap your app (layout.tsx or _app.tsx)
export default function RootLayout({ children }) {
return (
<AffitorProvider programId="59">
{children}
</AffitorProvider>
);
}
// 2. Read tracker state in any component
function ReferralBadge() {
const affitor = useAffitor();
if (!affitor?.hasAffiliateAttribution) return null;
return <span>Referred by a partner!</span>;
}
AffitorProvider Props| Prop | Type | Required | Description |
|---|---|---|---|
programId | string | number | Yes | Your Affitor program ID |
debug | boolean | No | Enable debug mode |
scriptUrl | string | No | Custom script URL |
useAffitor()Returns AffitorInstance | null. Returns null while the SDK is loading.
Important:
useAffitor()is best for reading tracker state in the UI (e.g.hasAffiliateAttribution,customerCode). For critical tracking events likesignup, useawait loadAffitor()directly instead — see Which approach should I use? below.
useLoadAffitor() (Standalone)Use this when you don't need a provider — the hook loads the SDK on mount.
import { useLoadAffitor } from '@affitor/tracker/react';
function MyComponent() {
const affitor = useLoadAffitor('59', { debug: true });
return (
<div>
{affitor?.hasAffiliateAttribution && <p>Partner referral detected</p>}
</div>
);
}
signup, redirectToCheckout)Always use await loadAffitor() directly. This guarantees the script is loaded before the event fires. Critical events must never be silently skipped.
// GOOD — guaranteed to fire
const affitor = await loadAffitor('59');
await affitor?.signup(userId, email);
// BAD — affitor could be null if script still loading
const affitor = useAffitor();
await affitor?.signup(userId, email); // silently skipped if null
Use useAffitor() or useLoadAffitor(). If the value is null momentarily while loading, the UI simply doesn't render that part yet. No data is lost.
// GOOD — fine for UI, gracefully handles loading state
const affitor = useAffitor();
return affitor?.hasAffiliateAttribution ? <Badge /> : null;
| Use Case | Approach | Why |
|---|---|---|
signup on registration | await loadAffitor() | Must not be lost — awaits script load |
redirectToCheckout | await loadAffitor() | Must not be lost — awaits script load |
| Show referral badge in UI | useAffitor() | OK to show nothing while loading |
| Display customer code | useAffitor() | OK to show nothing while loading |
Check hasAffiliateAttribution for conditional UI | useAffitor() | OK to show nothing while loading |
If you're currently using the <script> tag approach:
<script src="https://api.affitor.com/js/affitor-tracker.js"
data-affitor-program-id="59"></script>
<script>
// Must check if loaded, use queue fallback, handle timing manually
if (window.affitor) {
window.affitor.trackLead({ email, user_id });
} else {
window.affitorQueue = window.affitorQueue || [];
window.affitorQueue.push(['trackLead', { email, user_id }]);
}
</script>
import { loadAffitor } from '@affitor/tracker';
// No timing issues — awaits script load automatically
const affitor = await loadAffitor('59');
await affitor?.signup(userId, email);
No more:
if (window.affitor) checksaffitorQueue.push)(window as any) type casting in TypeScriptloadAffitor(programId, options?)| Parameter | Type | Description |
|---|---|---|
programId | string | number | Your Affitor program ID |
options.env | 'production' | 'uat' | 'local' | Environment preset |
options.debug | boolean | Enable debug mode |
options.scriptUrl | string | Custom script URL (overrides env) |
Returns: Promise<AffitorInstance | null>
AffitorInstance| Method / Property | Type | Description |
|---|---|---|
signup(customerKey, email) | (customerKey: string, email: string) => Promise<void> | Track a signup event |
trackLead(data) | (data: TrackLeadData) => void | Deprecated. Alias for signup(). |
trackTest(data?) | (data?: TrackTestData) => void | Send test event |
redirectToCheckout(params) | (params: RedirectToCheckoutParams) => void | Redirect to checkout |
customerCode | string | null | Affiliate customer code |
affiliateUrl | string | null | Affiliate referral URL |
hasAffiliateAttribution | boolean | Has affiliate attribution |
debugMode | boolean | Debug mode enabled |
programId | number | Program ID |
TrackLeadData| Field | Type | Required | Description |
|---|---|---|---|
email | string | Yes | User's email |
user_id | string | Yes | Your app's user ID (critical for payment attribution) |
additional_data | Record<string, unknown> | No | Extra metadata |
TrackTestData| Field | Type | Required | Description |
|---|---|---|---|
step_id | string | No | Step identifier (default: 'pageview') |
message | string | No | Test message |
user_id | string | No | User ID |
RedirectToCheckoutParams| Field | Type | Required | Description |
|---|---|---|---|
price | number | No | Price amount |
programId | string | number | No | Override program ID |
MIT
FAQs
Affitor affiliate tracking SDK for JavaScript applications
We found that @affitor/tracker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.

Security News
During a UK cyber test, a Mythos 5 agent used sockpuppets, social engineering, and prompt injection to try to get a maintainer to merge malware.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.