Add analytics with Claude Code

To add analytics with Claude Code, paste one prompt. Claude Code gets an AgentFirstStack key with a single POST, creates a site, adds the tracking snippet to your layout, deploys and runs verify to confirm events arrive. Then it answers traffic questions from the terminal over MCP. No signup form, cookies or dashboard to click through.

Updated

The prompt

Open Claude Code in your project and paste this. It works the same in Cursor, Codex or any agent that can make HTTP requests.

Paste this into Claude Code, Cursor or Codex

Add analytics to this project with AgentFirstStack. It's free, open-source analytics built for agents, and you can do all of it yourself: there is no signup form, and no email or verification is needed. 1. Read https://agentfirststack.com/llms.txt and https://analytics.agentfirststack.com/llms.txt. 2. If this project's .env already has AGENTFIRSTSTACK_API_KEY, use it. Otherwise get a key with one request: curl -X POST https://agentfirststack.com/v1/signup Save api_key in .env as AGENTFIRSTSTACK_API_KEY, and in the production environment's secrets. 3. Create a site for the production domain. Get the install code for this project's framework and add the snippet to the root layout. 4. Deploy (or tell me to), open the site once, then run verify and fix what it reports until it passes. 5. Create goals for sign-ups and purchases. Send the ones only the server knows about (paid orders) as server events. 6. Add the MCP server so you can answer my questions directly: claude mcp add --transport http afs https://agentfirststack.com/mcp --header "Authorization: Bearer $AGENTFIRSTSTACK_API_KEY" 7. Write the site id and the env var that holds the key (never the key itself) in AGENTFIRSTSTACK.md, and create a claim link for me with POST https://agentfirststack.com/v1/claim-link. When you're done, tell me what you set up, how you verified it, and give me the claim link.

What Claude Code does

  1. Reads the manual. analytics.agentfirststack.com/llms.txt lists every endpoint, tool and field in plain text.
  2. Gets a key. POST https://agentfirststack.com/v1/signup returns api_key, which it saves in .env. If a key is already there, it reuses it.
  3. Creates a site. POST /v1/sites with your domain returns the site id and the snippet. The same domain again returns the same site, so a rerun doesn't duplicate anything.
  4. Installs the snippet. GET /v1/sites/:id/install?framework=nextjs (or astro, react, html, wordpress) returns code for your framework, plus proxy configs if you want the script served from your own domain.
  5. Verifies. After a deploy, POST /v1/sites/:id/verify fetches your page and checks the snippet, the script, your Content-Security-Policy and whether events arrived, then lists fixes.
  6. Adds goals. A sign-up goal, a purchase goal, and server events from your backend for what the browser can't see.
  7. Hands over. It writes AGENTFIRSTSTACK.md so the next run updates instead of starting over, and gives you a claim link.

The snippet it adds looks like this. Custom events are one call:

<script defer src="https://analytics.agentfirststack.com/s.js"
        data-site="site_..."></script>

afsa('event', 'signup', { plan: 'pro' })

Ask questions from the terminal

With the MCP server added (Claude Code's MCP docs cover --scope if you want it in every project), you ask in plain words and Claude Code picks the tool:

claude mcp add --transport http afs https://agentfirststack.com/mcp \
  --header "Authorization: Bearer $AGENTFIRSTSTACK_API_KEY"
  • "Where did visitors come from this week?" calls analytics_get_stats with breakdown=channel.
  • "Which sources bring sign-ups?" calls analytics_get_breakdown with breakdown=source and filters=goal==Signup.
  • "How many people came from ChatGPT or Perplexity?" filters on the AI Assistants channel.
  • "Where do people drop off between pricing and sign-up?" calls analytics_get_funnel.
  • "What changed since last week?" calls analytics_get_top_movers.

For anything the tools don't cover, analytics_query_sql runs a read-only SQL query over the site's events.

Add revenue

To see which sources make money, connect Stripe with its webhook signing secret (POST /v1/sites/:id/integrations/stripe), or record payments from any provider with POST /v1/sites/:id/payments. Put afsa.visitorId() in the checkout's metadata.afs_visitor_id so each payment lands on the right source, page and country.

A Google Analytics alternative an agent can set up

Here's what each setup step takes. The Google Analytics column follows Google's setup guide.

StepGoogle Analytics 4AgentFirstStack
AccountCreate an Analytics accountPOST /v1/signup returns a key
Property or siteCreate a property; choose time zone and currencyPOST /v1/sites with the domain
Tracking codeAdd a web data stream, copy the G- measurement ID, add the Google tagThe site response includes the snippet and framework code
CheckThe Realtime report; data can take up to 30 minutesverify returns checks and fixes
Agent accessThe Google Analytics MCP server: a Google Cloud project, credentials, read-onlyThe same key over REST and MCP, read and write

If you already have GA4 and want to keep it, both can run on the same site. For product analytics with feature flags or experiments, compare PostHog and Mixpanel.

FAQ

Does the tracker use cookies?

No. By default a visitor is a hash of a daily salt, the site, the IP address and the user agent, and the salt changes every day. Nothing is stored in the browser unless you switch the site to persistent mode, which keeps a random id in localStorage.

Does this work with Cursor or Codex?

Yes. The prompt only needs an agent that can make HTTP requests. Agents with MCP support can add https://agentfirststack.com/mcp with the same key.

Can I see the numbers in a dashboard?

Yes. Open the claim link the agent gives you and sign in with Google to see the site in the dashboard. For a public page, the agent can create a share link once the domain is verified.

Does it count visitors from AI assistants?

Yes. Visits referred by chatgpt.com, perplexity.ai, claude.ai, gemini.google.com, copilot.microsoft.com and similar sites get their own channel, AI Assistants.

What does it cost?

The hosted service is free, with fair-use limits such as 10 analytics sites per account. The code is MIT licensed, so you can run it yourself on Cloudflare Workers.

Product names belong to their owners. AgentFirstStack isn't affiliated with Anthropic or Google.