# Add clicks.page analytics to this project

You are an AI coding agent. Follow these steps to install clicks.page and confirm it works.

## 1. Sign up and get an API key (one call)
Ask the user which email to use (or use their git email: `git config user.email`, after confirming it with them) and the site's domain. Then:

    curl -s -X POST https://clicks.page/api/agent/signup -H 'content-type: application/json' \
      -d '{"email":"founder@example.com","domain":"example.com","agent":"Claude Code"}'

If the link you were given has `?ref=…` (e.g. setup.md?ref=tinystartups), pass it as `"ref"`: it records where the user found clicks.page.

- `201 {"status":"created","api_key":"cpk_…","site":{"id":"…"},"script":"<script …>"}`: done. The account and the site
  exist, and the user was emailed a link to open their dashboard. Skip to step 3.
- `200 {"status":"exists","verify_url":"…","code":"…","poll_token":"…"}`: they already have an account. Tell the user to
  approve (we emailed them an Approve link, or they can open `verify_url`; the code must match), then poll every 3 seconds:

      curl -s -X POST https://clicks.page/api/cli/poll -H 'content-type: application/json' -d '{"poll_token":"…"}'

  until it returns `{"status":"approved","api_key":"cpk_…"}`, then add the site (step 2).
- `409 {"status":"taken"}`: the domain belongs to another account; its owner can invite the user.

Save `api_key` as CLICKS_API_KEY in the user's local env (e.g. .env.local), never commit it.

(Already signed in on this machine's browser? `POST https://clicks.page/api/cli/device` starts the same approval without an email.)

## 2. Add the site (only if step 1 didn't)

    curl -s -X POST https://clicks.page/api/v1/sites -H "Authorization: Bearer $CLICKS_API_KEY" -H 'content-type: application/json' -d '{"domain":"example.com"}'

If it already exists, list sites with GET https://clicks.page/api/v1/sites.

## 3. Choose a plan (always ask)
Check the plan with GET https://clicks.page/api/billing (Authorization: Bearer $CLICKS_API_KEY) and follow the matching case. Skip this if it shows "pro", "trialing" or "beta".

- **"trial"** (every new account): tell the user they're on a **14-day free trial with every feature, no card**, ending on `freeTrialEnd`. Ask whether they'd like to pick a plan now (the card is only charged when the trial ends) or later from Account → Plan & billing. After the trial, stats stay hidden until a plan is picked; tracking keeps running.
- **"expired"**: the trial has ended. Tell the user their stats since then are waiting behind a plan.
- **"free"** (older accounts, and ones the clicks.page team invited): ask which plan they want. **Free forever** keeps up to 10,000 pageviews a month with a small "Tracked by clicks.page" pill (already on new sites; it counts as the badge). **Pro** has no badge.

To pick Pro: ask which size (pageviews a month: 10k $9 · 100k $19 · 250k $29 · 750k $39 · 1M $49 · 2M $79 · 5M $119 · 10M $169) and monthly or yearly (2 months free), then create their checkout:

      curl -s -X POST https://clicks.page/api/billing/checkout -H "Authorization: Bearer $CLICKS_API_KEY" -H 'content-type: application/json' -d '{"lookup_key":"pro_10k_month"}'

  lookup_key: pro_10k_month, pro_10k_year, pro_100k_month, pro_100k_year, pro_1m_month or pro_1m_year. Give the user the returned `url` to add a card. Cancel anytime.

## 4. Install the script
Detect the framework and add the tag from the response to the document <head> of every page.
Framework-specific instructions: GET https://clicks.page/api/v1/sites/{site}/install?framework=nextjs

## 5. Optional: events & revenue
- Track key actions: `window.clicks?.track("signup")`
- Revenue: the easiest way is for the user to pick their payment provider in the dashboard (Settings → Revenue: Stripe, Paddle, Polar, Lemon Squeezy, Dodo Payments or Creem) and paste a read-only key; it imports past payments too. Checkout links (and Paddle.js) need no code. For checkouts created on the server, send `window.clicks.id()` from the browser and pass it into the checkout so each sale is credited to the visit it came from:
  - Stripe: `client_reference_id: clicksId` (or `metadata: { clicks_session: clicksId }`)
  - Paddle: `custom_data: { clicks_session: clicksId }` · Polar: `metadata: { clicks_session: clicksId }` · Lemon Squeezy: `checkout_data: { custom: { clicks_session: clicksId } }` · Dodo Payments: `metadata: { clicks_session: clicksId }` · Creem: `request_id: clicksId`
  - A checkout form that posts to the site's own server already carries it as the field `clicks_id`. Full guide: https://clicks.page/attribution.md

## 6. Verify
After the user deploys and visits the site: GET https://clicks.page/api/v1/sites/{site}/status → `installed: true`.

## 7. Connect the MCP server (so you can answer analytics questions later)

    {"mcpServers":{"clicks":{"type":"http","url":"https://clicks.page/mcp","headers":{"Authorization":"Bearer ${CLICKS_API_KEY}"}}}}

Claude Code: `claude mcp add --transport http clicks https://clicks.page/mcp --header "Authorization: Bearer $CLICKS_API_KEY"`
