You are helping integrate LinkJolt affiliate tracking into a product that takes payment through Creem (creem.io).

## Context
- Merchant ID: YOUR_MERCHANT_ID
- Platform: Creem
- Tracking Script: https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID
- Webhook: the merchant connects Creem from LinkJolt → Integrations → Creem. With an API key, LinkJolt creates a webhook named "LinkJolt" in their Creem store, subscribed to every event, and keeps its signing secret. By hand, the merchant adds a webhook in Creem (Developers → Webhooks) pointing at https://www.linkjolt.io/api/webhooks/creem?merchant=YOUR_MERCHANT_ID with ALL events selected and pastes its signing secret into LinkJolt. Do NOT invent a different URL or a partial event list.

## How attribution works
When a visitor arrives from an affiliate link (`?tc=...&aff=...`), the tracking script stores the affiliate identity in the browser and exposes it as `window.linkjolt.referral`, a string of the form `tc_<code>_aff_<id>_cid_<click>`.

Creem copies a checkout's `metadata` onto the subscription, and every `subscription.paid` carries it, so the one thing the integration must do is put `window.linkjolt.referral` into `metadata.linkjolt_ref` on the checkout. LinkJolt's webhook reads `metadata.linkjolt_ref` off every paid checkout and subscription payment.

Pick the ONE case that matches how this site sells:

### Case A: Creem payment links on the site (links to creem.io or www.creem.io, paths /payment/... or /product/...)
Nothing beyond the tracking script. The script rewrites every Creem payment link and product link on the page to carry `?metadata[linkjolt_ref]=<window.linkjolt.referral>`, and Creem carries that into the checkout session's metadata. Verify by opening a page with `?tc=TEST&aff=TEST` and inspecting a payment link's href: it must contain `metadata[linkjolt_ref]=tc_TEST_aff_TEST`, like this:

```
https://www.creem.io/payment/prod_123?metadata[linkjolt_ref]=tc_TEST_aff_TEST
```

Shortened Creem links (l.creem.io) and Creem storefronts are NOT rewritten; affiliates selling through those need a discount code (case C) as well.

If the Creem store uses a Creem custom domain (payment links on a subdomain such as pay.yourbrand.com instead of creem.io), add `&creem_domain=pay.yourbrand.com` to the tracking script's URL (`/api/tracking.js?id=...&creem_domain=pay.yourbrand.com`); the script then rewrites every link to that domain too. The LinkJolt Creem tab shows the script with it filled in when it has detected the domain.

### Case B: the site's own code creates the checkout (Creem SDK or API)
Read the referral in the browser and send it to the server with the checkout request, then set it as `metadata.linkjolt_ref` when creating the checkout session:

```
// 1. In the browser: send the referral with your checkout request
const referral = window.linkjolt?.referral || '';

// 2. On your server: add it to the checkout you create
// POST https://api.creem.io/v1/checkouts   (header x-api-key: your Creem API key)
{
  "product_id": "prod_123",
  "success_url": "https://example.com/thanks",
  "metadata": { "linkjolt_ref": "<the referral from step 1>" }
}
```

With Creem's TypeScript SDK:

```typescript
import { Creem } from "creem";
const creem = new Creem({ apiKey: process.env.CREEM_API_KEY });

const checkout = await creem.checkouts.create({
  productId: "prod_123",
  successUrl: "https://example.com/thanks",
  metadata: { linkjolt_ref: referral },   // the value from window.linkjolt.referral
});
// send the customer to checkout.checkoutUrl
```

When the visitor did not come through an affiliate link the referral is an empty string; send it anyway or leave the key out, either way no commission is created. Never rename the key and never put the value anywhere else.

### Case C: discount codes only
The merchant gives each affiliate a Creem discount registered in LinkJolt (LinkJolt creates it in Creem when it holds an API key; otherwise the merchant creates it in Creem and registers its dis_ id or code). No code change: LinkJolt matches the discount on the paid checkout.

---

### Add Tracking Script

Add this to your website's `<head>` tag:

**HTML:**
```html
<script src="https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID" async></script>
```

**Next.js / React:**
```tsx
import Script from 'next/script'

// Add this in your layout.tsx or _app.tsx
<Script
  src="https://www.linkjolt.io/api/tracking.js?id=YOUR_MERCHANT_ID"
  strategy="afterInteractive"
/>
```

## Tracking Script API Reference

```javascript
// Format: "tc_CAMPAIGN123_aff_AFFILIATE456_cid_CLICK789"
window.linkjolt.referral

// Returns: { trackingCode, affiliateId, clickId }
window.linkjolt.getTrackingInfo()

// Returns boolean
window.linkjolt.isAffiliateVisit()

// Browser-side conversion reporting (used by Squarespace / Teachable / BigCommerce paths)
window.linkjolt.trackPurchase(amount, { orderId: 'ORDER_ID' })
```

---

## Test it
1. Open the site with `?tc=TEST&aff=TEST` and confirm `window.linkjolt.referral` is `tc_TEST_aff_TEST` in the console.
2. Start a checkout and confirm the created checkout carries `metadata.linkjolt_ref` (case B: log the object you create; case A: inspect the link's href before clicking).
3. Complete a purchase. The sale appears in LinkJolt → Commissions within a few seconds of Creem marking the checkout completed. A purchase made in Creem's test mode is recorded as a test sale, under Test sales on Commissions, and is never paid out.

## Common issues
- Sale recorded with no affiliate → the checkout had no `metadata.linkjolt_ref` (case B: the referral was empty or never reached the server; case A: the link was not on the page when the script ran, or was built by script AFTER our rewriter; call `window.linkjolt.referral` when building it).
- Renewals not recording → renewals inherit the metadata from the subscription automatically; check the FIRST payment carried it. LinkJolt attributes renewals from its own record of the first payment, so nothing is needed on later payments.
- Trials → a trial start arrives with no money and is recorded as a trial; the first real charge turns it into the commission.
- Refunds and chargebacks → Creem's refund.created and dispute.created events reverse the commission proportionally. Nothing to build.
