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.
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.
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
- Reads the manual. analytics.agentfirststack.com/llms.txt lists every endpoint, tool and field in plain text.
- Gets a key.
POST https://agentfirststack.com/v1/signupreturnsapi_key, which it saves in.env. If a key is already there, it reuses it. - Creates a site.
POST /v1/siteswith your domain returns the site id and the snippet. The same domain again returns the same site, so a rerun doesn't duplicate anything. - Installs the snippet.
GET /v1/sites/:id/install?framework=nextjs(orastro,react,html,wordpress) returns code for your framework, plus proxy configs if you want the script served from your own domain. - Verifies. After a deploy,
POST /v1/sites/:id/verifyfetches your page and checks the snippet, the script, your Content-Security-Policy and whether events arrived, then lists fixes. - Adds goals. A sign-up goal, a purchase goal, and server events from your backend for what the browser can't see.
- Hands over. It writes
AGENTFIRSTSTACK.mdso 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_statswithbreakdown=channel. - "Which sources bring sign-ups?" calls
analytics_get_breakdownwithbreakdown=sourceandfilters=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.
| Step | Google Analytics 4 | AgentFirstStack |
|---|---|---|
| Account | Create an Analytics account | POST /v1/signup returns a key |
| Property or site | Create a property; choose time zone and currency | POST /v1/sites with the domain |
| Tracking code | Add a web data stream, copy the G- measurement ID, add the Google tag | The site response includes the snippet and framework code |
| Check | The Realtime report; data can take up to 30 minutes | verify returns checks and fixes |
| Agent access | The Google Analytics MCP server: a Google Cloud project, credentials, read-only | The 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.
Related
- AnalyticsGoogle Analytics MCP comparedRead GA4, or let the agent create the analytics.
- AnalyticsPostHog MCP comparedA product platform, or web analytics an agent sets up alone.
- AnalyticsMixpanel MCP comparedQuery a Mixpanel org, or start from nothing.
- Server-side conversion trackingMeta, Google Ads, TikTok and GA4, counted once.
- Google Tag Manager MCP comparedEdit GTM, or let the agent run the tag manager.
- All servicesOne MCP server for every serviceAnalytics, tags, mail and replay tools on one key.
- MailGmail MCPInboxes an agent creates for itself.
- MailAgentMail alternativeFree inboxes for agents, with wait-for-email.
- MailEmail inboxes for AI agentsSign up for services and read the codes.
- ReplayMicrosoft Clarity MCPSession replay an agent reads as text.
- ReplaySession replay for AI agentsFind out why visitors drop off.