
# Track the whole funnel

Kerdly follows a customer from the ad click to the last renewal or refund, and credits every step to
the ad that started it. Some steps are recorded by the SDK on its own, some need one call from you,
and some come straight from the stores. This page says which is which, for a website, an app, and
a landing page that sends people to an app.

## The funnel at a glance

"Website" means `@kerdly/web` plus your server; "App" means `@kerdly/expo` or the native iOS and
Android SDKs.

| Step | Website | App | Your code |
|---|---|---|---|
| **Click** | The ad network's click id or UTM tags in the landing URL, read from the visit | Store install referrers, Apple Ads token, Meta Install Referrer, deep links: read by the SDK | No |
| **Visit** | `visit` recorded by the SDK on each page load | Not used; the install is the app's first step | Single-page apps: `kerdly.visit()` per route |
| **Click on an app link** | `data-kerdly-link` on the link, or `kerdly.click(url)` | | Mark the links |
| **Identify** | `kerdly.identify(userId)` after sign-in | `Kerdly.setUserId(userId)` after sign-in | Yes, one line |
| **Install** | | Recorded by the SDK on first launch, then matched to its click | No |
| **Registration** | Optional: a custom server event | `trackRegistration({ method })` | Yes |
| **Trial** | Server event `start_trial` | `trackStartTrial(...)` | Yes |
| **Subscribe** (first paid charge) | Server event `subscribe`, or `trial_conversion` after a trial | `trackSubscribe(...)`; the stores confirm it | Yes |
| **One-time purchase** | Server event `purchase` | `trackPurchase(...)`; the stores confirm it | Yes |
| **Link the purchase** | Your server sends the `user_id` you identified | `setAppAccountToken` + `notifyPurchase` | Yes |
| **Renewal** | Server event `renewal` | From the App Store and Google Play | Website only |
| **Cancel** (auto-renew off) | Not needed for reports; see [Cancellations](#cancellations) | From the App Store and Google Play | No |
| **Refund** | Server event `refund` | From the App Store and Google Play | Website only |

If you use RevenueCat, see [With RevenueCat](#with-revenuecat) for where to connect it. For a
step-by-step setup that matches how you sell, pick a guide: [Sell on your website](https://kerdly.com/docs/guides/web-checkout.md),
[App with App Store and Google Play billing](https://kerdly.com/docs/guides/store-app.md),
[App with web payments](https://kerdly.com/docs/guides/app-web-payments.md) or [Web-to-app funnel](https://kerdly.com/docs/guides/web-to-app.md).

## The ids that tie it together

Every step is joined to the one before it by an id. If you pass these four ids, the whole chain
holds.

| Id | Made by | Carries the chain from | To |
|---|---|---|---|
| **Visitor id** | The web SDK (cookie `kerdly_vid`) | The ad click and visit | The sign-in on the site, and the app install when the visitor taps an app link |
| **User id** | You: your own account id | `identify` on the site, `setUserId` in the app | Every server event and every app event after sign-in |
| **Install id** | The app SDK | The install and its click | Every app event and linked purchase |
| **Account token / transaction id** | The store (`appAccountToken`, Play `obfuscatedAccountId`, transaction ids) | The purchase in the app | Renewals, cancellations and refunds the store reports later |

Use the **same user id everywhere**: on the website, in the app, in RevenueCat, and in your
server's events. A user who signs in on the site and later in the app is then one person.

## Setup 1: an app that sells through the stores

Tallybird's iOS and Android apps sell a weekly and an annual subscription through the App Store and
Google Play.

**1. Click and install: nothing to write.** Configure the SDK; it records the install and the
signals that match it to its ad: the Apple Ads token, the Play install referrer, the Meta Install
Referrer, deep links and, when the user allows tracking, the advertising id.

```ts
Kerdly.configure({ key: Platform.select({ ios: IOS_KEY, android: ANDROID_KEY })! });
```

**2. Identify and register.**

```ts
// after sign-up or sign-in
Kerdly.setUserId(user.id);
Kerdly.trackRegistration({ method: 'apple' }); // only on sign-up
```

**3. Trial, subscribe, purchase.** Send them from your purchase flow as they happen. They reach Kerdly
in seconds, before the store's own report, and ad networks can be told about them right away.

```ts
Kerdly.trackStartTrial({ productId: 'tallybird_pro_annual', value: 0, currency: 'USD', transactionId });
Kerdly.trackSubscribe({ productId: 'tallybird_pro_weekly', value: 7.99, currency: 'USD', transactionId });
```

**4. Link the purchase to the install.** This is what lets the store's later messages (renewals,
cancellations, refunds) find the install and its ad.

```ts
Kerdly.setAppAccountToken(accountToken); // the token your purchases carry
Kerdly.notifyPurchase(purchase);         // after each completed purchase
```

See [Link purchases to the install](https://kerdly.com/docs/sdks/expo.md#link-purchases-to-the-install) for both
platforms' shapes and a Superwall example.

**5. Renewals, cancellations and refunds: nothing to write.** Kerdly reads them from the stores:
[App Store Server Notifications and reports](https://kerdly.com/docs/get-started/app-store.md) and
[Google Play's notifications and reports](https://kerdly.com/docs/get-started/google-play.md). Turn on the real-time
notifications so they arrive within minutes.

## Setup 2: a website that sells on the web

Tallybird also sells a web plan through Stripe on `tallybird.example`.

**1. Click and visit: nothing to write.** Add the [web SDK](https://kerdly.com/docs/sdks/web.md#install). Each page load
records a visit with the landing URL, so the ad click id (`fbclid`, `ttclid`, `gclid`, …) and UTM
tags come with it. A single-page app calls `kerdly.visit()` on each route change.

**2. Identify after sign-in.** This links the visitor, and the ad that brought them, to your user id.

```ts
kerdly.identify(user.id);
```

If the visitor buys without an account, send `visitor_id: kerdly.visitorId()` to your server with
the checkout instead, and put it on the server event.

**3. Money events from your server, or from Paddle or Stripe.** On Paddle or Stripe, the quickest way is
to [connect the provider](https://kerdly.com/docs/guides/web-checkout.md#4-paddle-or-stripe-connect-it-no-server-code):
Kerdly reads every charge, renewal and refund from it, and your checkout only names the buyer with
the user id from step 2. Otherwise your server, which knows a charge really happened, sends each
one to `POST https://app.kerdly.com/api/v1/s2s/events` with the Web app's server key, from your
payment provider's webhook.

| Your payment event | Kerdly event | Key props |
|---|---|---|
| A trial starts | `start_trial` | `product_id`, `value: 0`, `currency`, `subscription_id` |
| First paid charge of a new subscription | `subscribe` | `transaction_id`, `value`, `net_value`, `currency`, `store`, `subscription_id` |
| First paid charge after a trial | `trial_conversion` | the same as `subscribe` |
| Each later charge | `renewal` | the same, with the subscription's `subscription_id` |
| A one-time purchase | `purchase` | `transaction_id`, `value`, `net_value`, `currency`, `store`, `product_kind`, `quantity` |
| A refund | `refund` | `transaction_id` of the charge it refunds, `value` refunded, `currency` |

```js
// Stripe webhook handler (Node), one event per paid invoice; adapt the field names to your API version
await fetch("https://app.kerdly.com/api/v1/s2s/events", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.KERDLY_SERVER_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    events: [{
      event_id: invoice.charge,              // unique per event; a retry is a harmless duplicate
      name: isFirstInvoice ? "subscribe" : "renewal",
      ts: invoice.status_transitions.paid_at * 1000,
      user_id: customer.metadata.user_id,    // the id you passed to kerdly.identify
      props: {
        transaction_id: invoice.charge,
        subscription_id: invoice.subscription,
        product_id: price.lookup_key,        // e.g. "tallybird_web_annual"
        value: invoice.amount_paid / 100,
        net_value: netAfterFees,
        currency: invoice.currency.toUpperCase(),
        store: "stripe",
        product_kind: "subscription",
      },
    }],
  }),
});
```

- `transaction_id` plus `store` identify a charge, so a refund finds the charge it reverses and the
  same charge is never counted twice.
- `subscription_id` ties a subscription's charges together, so each renewal is credited to the ad
  that brought the first one.
- `value` is what the customer paid; `net_value` is what you keep after the provider's fees and
  taxes, in the same currency. Reports count `net_value`; ad networks are sent `value`.
- Send `"environment": "sandbox"` for test-mode charges.

The fields are listed in full under
[Report purchases from your server](https://kerdly.com/docs/sdks/web.md#report-purchases-from-your-server).

## Setup 3: a landing page that sends people to the app

Tallybird runs Meta ads to a landing page with "Get the app" buttons.

1. **Click and visit**: the [web SDK](https://kerdly.com/docs/sdks/web.md) on the landing page, as in Setup 2.
2. **Click on an app link**: add `data-kerdly-link` to every store or Kerdly link. The SDK records
   the click and adds the visitor id to the link (`kerdly_v`), so the install can continue the
   visitor's chain. A Kerdly link keeps the visitor id through the store; a plain store URL loses
   it on iOS, and Kerdly then matches the install probabilistically instead.
3. **Install onward**: the app follows Setup 1. The install is matched to the visit, and the visit
   carries the ad click.
4. **Optional, accounts on both**: if people sign in on the site too, call `kerdly.identify` there
   and `Kerdly.setUserId` in the app with the same user id.

If your backend creates the account (say at sign-up through your API) and wants to tie it to an
install or a web visitor itself, it can send `POST /v1/s2s/identify` with the server key:

```json
{ "identities": [{ "user_id": "user_8f2k1", "install_id": "<Kerdly.getInstallId()>" }] }
```

Each identity names a `user_id` and a `visitor_id` or an `install_id`.

## With RevenueCat

Where you connect RevenueCat depends on who takes the money:

- **App Store and Google Play purchases** already reach Kerdly from the stores (Setup 1). Keep
  RevenueCat for your paywall; don't make it a second revenue source for those stores.
- **Web purchases** (RevenueCat Web Billing, or Stripe through RevenueCat) count in the project's
  **Web app**. Connect RevenueCat under the Web app's **Settings → Connections** and paste its
  webhook URL into RevenueCat; past purchases are backfilled. The full setup is in
  [App with web payments](https://kerdly.com/docs/guides/app-web-payments.md).

- Use your Kerdly user id as RevenueCat's `appUserID`, and call `Kerdly.setUserId` with the same id.
- On the web, set the RevenueCat subscriber attribute `kerdly_visitor_id` to `kerdly.visitorId()`
  (through RevenueCat's SDK or REST API), so web purchases find their visit.
- In the app, keep `notifyPurchase` (Setup 1, step 4): it links the store transaction to the
  install whatever processes the purchase.

## Cancellations

A cancellation (the user turns auto-renew off) ends nothing by itself: the subscription runs to the
end of the period and then doesn't renew.

- **Apps**: Kerdly reads auto-renew changes, billing problems and expirations from the App Store and
  Google Play notifications, and shows them in retention and renewals. Nothing to send.
- **Website**: Kerdly's reports work from charges: a subscription that stops sending `renewal`
  events has ended. You don't need to send cancellations. If you want them recorded alongside your
  other events, send a custom server event such as `subscription_cancel` with the
  `subscription_id`; it is stored but doesn't change revenue.

## What reaches your ad networks

With postbacks turned on (**Settings → Postbacks**), Kerdly sends the events you map, by default
trials, first subscriptions and purchases, to Meta, TikTok and the other networks, server to server.
Each send uses the same event id as any copy the app or the pixel sent, so the network counts it
once.

## Checklist

- [ ] SDK configured once, at startup (app) or on every page (web).
- [ ] App links marked with `data-kerdly-link`.
- [ ] Same user id in `identify`, `setUserId`, RevenueCat and server events.
- [ ] `trackStartTrial` / `trackSubscribe` / `trackPurchase` in the app's purchase flow.
- [ ] `setAppAccountToken` and `notifyPurchase` for every store purchase.
- [ ] App Store and Google Play connected, with real-time notifications on.
- [ ] Website: Paddle or Stripe connected with every checkout naming the buyer, or every charge,
      renewal and refund sent from your server with `transaction_id`, `store` and `subscription_id`.
- [ ] Each app's and the website's **Onboarding** page shows its steps done.

---

Source: https://kerdly.com/docs/sdks/funnel. Index of all docs: https://kerdly.com/llms.txt
