@afflyin/sdk
v0.0.1
Published
Official Afflyin SDK — load the affiliate tracking pixel in the browser and post conversions from your server.
Maintainers
Readme
@afflyin/sdk
Official SDK for Afflyin affiliate tracking. Two thin pieces:
- Browser — a loader that injects Afflyin's hosted tracking pixel (the pixel is served from Afflyin's CDN, never bundled, so attribution fixes ship centrally without you re-deploying).
- Server — a typed client for posting conversions from your backend.
Works in any JS app; the examples below use Next.js.
npm install @afflyin/sdkBrowser: load the pixel
The pixel auto-captures ?ref= from the landing URL and stores a first-party
attribution cookie. Load it once, app-wide.
// app/layout.tsx
'use client';
import { useEffect } from 'react';
import { loadAfflyin } from '@afflyin/sdk';
export function AfflyinPixel() {
useEffect(() => {
loadAfflyin({ apiKey: process.env.NEXT_PUBLIC_AFFLYIN_API_KEY! });
}, []);
return null;
}At checkout, read the tracking id (or referral code) and persist it on the order so you can attribute the conversion server-side:
import { getTrackingId, getReferralCode } from '@afflyin/sdk';
const trackingId = getTrackingId() ?? getReferralCode();
// save `trackingId` with the order you createTip: if you only persist one thing, persist the referral code. Afflyin attributes from the bare
?ref=code even when cookies and click beacons were blocked — so sending it back at conversion makes attribution survive almost anything.
Server: post the conversion
Call this from a route handler, server action, or payment webhook — at the
moment your business rule says the order converted. It's idempotent on
orderId, so retries are safe.
// app/api/afflyin/route.ts
import { createAfflyinClient } from '@afflyin/sdk/server';
const afflyin = createAfflyinClient({
apiKey: process.env.AFFLYIN_API_KEY!, // server-only secret
});
export async function POST(req: Request) {
const order = await req.json();
const result = await afflyin.trackConversion({
trackingId: order.afflyinRef, // the tracking id / ref code you persisted
orderId: order.id,
orderValue: order.total,
currency: order.currency,
customerEmail: order.email,
});
return Response.json({ attributed: result.success });
}Signed postbacks (optional)
If your program enabled require_signed_postbacks, pass the signing secret and
every conversion is HMAC-signed automatically:
const afflyin = createAfflyinClient({
apiKey: process.env.AFFLYIN_API_KEY!,
signingSecret: process.env.AFFLYIN_SIGNING_SECRET!,
});The /server entry targets the Node runtime (uses node:crypto for signing).
In Next.js, ensure the route is not on the Edge runtime when signing.
API
| Export | Where | Purpose |
| --- | --- | --- |
| loadAfflyin(opts) | browser | Inject the hosted pixel (idempotent, SSR-safe). |
| getTrackingId() | browser | Read the _aff_trk / afflyin_tracking_id cookie. |
| getReferralCode() | browser | Read the captured referral code. |
| getReferralCodeFromUrl(search?) | browser | Parse ?ref=/?r=/… from a query string. |
| createAfflyinClient(opts) | server | trackConversion() + trackClick(). |
License
MIT
