> How the SDK captures partner referrals, shows the referral badge, carries referrals to other checkouts, and stores a visitor ref with pf_ref.

Source: https://plandalf.com/docs/developers/referrals-from-code

Developers

# Referrals and visitor refs

When someone arrives through one of your partners' links, the SDK records the referral with no code from you, and every Plandalf checkout on your site credits that partner. This page covers the code for everything around it: reading the referral, the badge, checkouts Plandalf doesn't render, and visitor refs. Setting up the partner program is in [Affiliates](https://plandalf.com/docs/affiliates/start).

## Referral capture

1.  ### The visitor clicks a partner link
    
    Partner links carry the partner's code, by default as `?via=CODE`.
    
2.  ### The SDK swaps the code for a token
    
    It asks Plandalf for a referral token (`rt_…`) and keeps it in the browser and in a `pf_rt` cookie until the program's attribution window ends.
    
3.  ### Checkouts carry it
    
    `present()`, `mount()` and your buttons send the token with the checkout, so the sale is credited to the partner.
    

## Read the referral

`plandalf.referral()` returns the page's referral handle. Await it for the referral, or `null` if there isn't one.

```js
const referral = await plandalf.referral();
if (referral) {
  console.log(referral.partner.name, referral.code, 'until', referral.expires_at);
}
```

Loop over `.events` to hear about the referral and the badge. The handle replays what already happened on this page, so a late listener doesn't miss anything.

```js
for await (const event of plandalf.referral().events) {
  console.log(event.type); // captured, changed, expired, shown, held-out, click, dismiss
}
```

The same events are DOM events on `window` (`plandalf:referral`, `plandalf:referral-changed`, `plandalf:badge-click` and so on) and work with `plandalf('on', …)`. See [Events](https://plandalf.com/docs/developers/events#sdk-events). The `purchase` event includes `referral` with the partner's code and name.

## Show the referral badge

The badge names the partner who sent the visitor. Put an element where you want it:

```html
<div data-plandalf-referral data-plandalf-variant="note" data-plandalf-theme="auto"></div>
```

Or from code, or as its own tag:

```js
plandalf.referral({ target: '#badge', variant: 'pill', template: 'Recommended by {partner}' });
```

```html
<plandalf-referral variant="bar"></plandalf-referral>
```

The badge shows nothing when there's no referral, when the program's badge is off, after the visitor dismisses it, and for visitors in the badge holdout group (who still count for the partner). `plandalf.from(badgeElement)` returns the page's referral handle.

Attribute

Values

What it does

`data-plandalf-decorate`

Present on a link: when the visitor has a referral, the SDK appends `pf_rt=<token>` to the link's URL on click, whatever its host. Links to hosts in `referral.decorate` are decorated without it.

`data-plandalf-referral`

Marks an element as the place for the referral badge. The SDK appends a `<plandalf-referral>` badge inside it when the visitor arrived through a partner link.

`data-plandalf-referral-template`

On a `data-plandalf-referral` element: badge wording with a `{partner}` token.

`data-plandalf-theme`

`light` `dark` `auto`

On a `data-plandalf-referral` element: the badge colours.

`data-plandalf-variant`

`pill` `note` `bar` `float`

On a `data-plandalf-referral` element: the badge style. Default `pill`.

## Stripe links and other checkouts

For a Stripe Payment Link or another site's checkout, carry the token yourself.

-   `decorate(url)` adds `pf_rt=<token>` to a URL, and Stripe's `client_reference_id` on `buy.stripe.com` links. Without a referral it returns the URL unchanged.
-   `data-plandalf-decorate` on a link does the same when it's clicked.
-   `metadata()` returns `{ plandalf_referral: 'rt_…' }` (or `{}`) to put on a Stripe Checkout Session your server creates.

```js
const referral = plandalf.referral();
buyLink.href = referral.decorate('https://buy.stripe.com/YOUR-LINK');

const res = await fetch('/create-checkout-session', {
  method: 'POST',
  body: JSON.stringify({ metadata: referral.metadata() }),
});
```

To decorate every link to a host automatically, list it on the script tag with `data-config-referral-decorate="shop.example.com"`. If your server records the sale itself, send the token as `referral_token` to [`POST /api/v1/affiliate/sales`](https://plandalf.com/docs/api/affiliate-sales).

## Change how capture works

The program's settings decide the link parameter and attribution window. These script tag attributes override them on one site (the same fields work as `referral: {…}` in `plandalf('init', …)` or `plandalf('configure', …)`):

Attribute

What it does

`data-config-referral-params`

Link parameters that carry a partner code, comma-separated. Default `via`.

`data-config-referral-decorate`

Hosts whose links get `pf_rt` added on click.

`data-config-referral-cookie`

Another cookie name to write the token to, as well as `pf_rt`.

`data-config-referral-global`

A `window` global to set to the token, for code that read an older tracker's global.

`data-config-referral-metadata-key`

The key `metadata()` uses. Default `plandalf_referral`.

`data-config-referral-consent`

`required` waits for consent before reading links or storage.

With consent required, call this when the visitor accepts (or `false` when they decline):

```js
plandalf('consent', true);
```

To turn capture off on a page, start the SDK with `plandalf('init', { apiBase: 'https://YOUR-ORG.plandalf.dev', referral: false })`.

## Visitor refs (pf\_ref)

A visitor ref is your own id for a person, such as their email address or CRM id. It isn't a partner referral. Deadline timers use it so one person keeps one deadline timer on every device. See [Deadline timers from code](https://plandalf.com/docs/developers/promos-from-code#visitors).

-   Links set it with `?pf_ref=`, for example `?pf_ref={{subscriber.email}}` in an email.
-   Code sets it with `plandalf('setRef', 'ada@example.com')`, which works before the SDK loads. `plandalf('setRef', null)` clears it.
-   `plandalf('getRef')` reads it, and `plandalf('getIdentity')` returns the whole record: `anonymous_id`, `ref`, `first_seen_at` and `last_seen_at`.

The SDK keeps it in the browser across visits. If your form tool can only put the email in another parameter, such as `?email=`, add `data-config-ref="email"` to the script tag. Without that setting, the SDK also reads the older `?ref=` parameter.

`plandalf.getAnonId()` returns the browser's anonymous id, which the SDK makes on the first visit and keeps.

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
