The easiest way to add analytics to your app with an AI agent
The easiest way to add analytics with an AI agent is to use a tool the agent can set up alone. Paste one prompt into Claude Code, Cursor, Codex or any agent that can make HTTP requests. It gets an AgentFirstStack key with one request, creates the site, adds the snippet to your layout, checks that events arrive, and runs the setup check until it says ready. At the end you open a claim link to see the dashboard. There's no signup form for you or the agent.
The prompt
This is the prompt behind the home page's "Copy prompt for your agent" button. It sets up analytics and the other three services (tags, an inbox and session replay), and tests everything end to end.
Set up AgentFirstStack for this project, completely, and verify it yourself. It's free, open-source infrastructure built for agents: web analytics, a tag manager with server-side conversion tracking, email inboxes and session replay. There is no signup form: one request gets a key, and you can do every step without me. First, look at what this site uses today (Google Analytics, Google Tag Manager, Plausible, DataFast, ad pixels, any other analytics or replay tool) without changing anything. Then ask me once and wait for my answer: should email that reaches the project's inbox be forwarded to me as each one arrives, or as one daily digest (and at what time, in my time zone)? And if you found other tools: shall you bring their setup and history over, and if you need it to export their data, may you use my browser where I'm signed in? If I can't answer, forward each one as it arrives and bring over whatever needs no help from me. 1. Read https://agentfirststack.com/llms.txt, then follow its "Full setup" section from start to finish. Read each service's manual before you use that service. 2. Key: if this project's .env already has AGENTFIRSTSTACK_API_KEY, use it, read AGENTFIRSTSTACK.md, and run the setup check (step 4) to see what earlier runs set up and what's missing. Otherwise run curl -X POST https://agentfirststack.com/v1/signup and save api_key in .env as AGENTFIRSTSTACK_API_KEY and in the production secrets. One key works for every service. If you support MCP, add the server the manual lists. 3. The full setup, in this order: - Map the site first: its main pages, every main button, link and form, and what counts as a sign-up, lead or purchase here. - Email: an inbox for the project, for any third-party signups the setup needs, forwarded to me the way I chose. - Analytics: the site, a goal for each conversion and main action, server events for what only the backend knows, AI crawler and search bot visits reported from the server (if the site has one), payments if the business takes them, and a weekly email report and alerts for me. - Tags: one container snippet on every page, an event for every main button, link and form (sent to analytics), and the conversions (also sent to the ad platforms this business uses). Preview, then publish. - Replay: linked to analytics, masked by default, asking for consent where the law requires it. 4. Deploy, then test it end to end (the manual's step 8): run the setup test (POST https://agentfirststack.com/v1/setup/test, or afs_test_setup), do its browser plan in a real browser and check that visit (afs_check_test_visit), then run the setup check (GET https://agentfirststack.com/v1/setup, or afs_check_setup) until it says ready. Fix what fails and test again. 5. Apart from that first message, don't stop to ask me things. For ad accounts (Google Ads, Meta, GA4, TikTok): if you have tools for them, use them to find the IDs; otherwise finish everything else and list exactly which IDs and tokens you need from me, and where I find each. 6. Write what you set up in AGENTFIRSTSTACK.md at the repo root (ids, env var names, every goal and event and what triggers it; never key values), add the AgentFirstStack block from the manual to AGENTS.md (so later agent sessions see new features), and commit both. 7. Create a claim link with POST https://agentfirststack.com/v1/claim-link (or the afs_create_claim_link MCP tool) so I can see everything in a dashboard. When you're done, show me the setup check's table of what's set up and the test results table, then what's waiting on me, and give me the claim link. If the site used other tools, say what you brought over from each, and leave them installed until I agree to remove them.
Only want analytics for now? This shorter prompt does analytics alone and checks only that part:
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 and read AGENTFIRSTSTACK.md. 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, served through the first-party proxy config from the same answer (/_a/* on our own domain, with the site's proxy key kept server-side), so ad blockers don't drop it. 4. Create a goal for each conversion (sign-ups, purchases, leads). Send the ones only the backend knows about (paid orders, verified sign-ups) as server events, with afsa.visitorId() passed from the page. 5. Deploy (or tell me to), open the site with ?afsa_debug=1, run verify and fix what it reports. Then run GET https://agentfirststack.com/v1/setup?services=analytics until it says ready, or tell me what's left. 6. If you support MCP, add https://agentfirststack.com/mcp with the key, so you can answer my traffic questions directly. 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 the agent does, step by step
The prompt points the agent to the manual's "Full setup" section. For analytics, that comes down to these calls. Each one ends with something the agent can check.
- Reads the manual. /llms.txt is the overview and the setup procedure; the analytics manual lists every endpoint, tool and field.
- Gets a key, or reuses one. If
.envhasAGENTFIRSTSTACK_API_KEY, it readsAGENTFIRSTSTACK.mdand runs the setup check first. OtherwisePOST https://agentfirststack.com/v1/signupreturnsapi_key. It names the project after the site withPATCH /v1/me, so you recognise it later. - Maps the site before changing it. The main pages, the buttons, links and forms on them, what counts as a sign-up, lead or purchase, and which analytics or pixel tools the site runs today, with their ids and event names (so their history and the new data line up).
- Creates the site.
POST https://analytics.agentfirststack.com/v1/sites {"domain":"example.com"}returns the site id, the snippet andnext_steps. Running it twice returns the same site with"existing": true. - Installs the snippet, through your own domain.
GET /v1/sites/:id/install?framework=nextjs(orhtml,astro,react,wordpress,gtm,tags) returns code for the framework and first-party proxy configs (a Cloudflare Worker route, Next.js, Vercel, Netlify, nginx). Use the proxy: the default snippet loads fromanalytics.agentfirststack.com, which ad blockers' filter lists block, while a path such as/_a/*on your own domain, forwarded with the site's proxy key, isn't singled out that way. There's no custom-domain (CNAME) option yet. - Adds goals and server events. A goal for each conversion and main action. Conversions only the backend sees go in with
POST /v1/sites/:id/events, carryingafsa.visitorId()from the page so they join the visit. - Adds what you'd otherwise forget. AI crawler reporting from the site's server (if it has one), payments (Stripe in one call, or other providers by webhook), and an email destination with a weekly report and alerts for a traffic drop and for no data. These reach you once you claim the project.
- Deploys, then tests.
POST /v1/setup/testsends test traffic through everything the server can reach and returns a browser plan. The agent opens the site with?afsa_debug=1in a real browser (marked as test traffic, kept out of your reports), follows the plan, and checks that visit. - Runs the setup check until it says ready.
GET https://agentfirststack.com/v1/setup(below). - Writes it down and hands over.
AGENTFIRSTSTACK.mdgets the ids and the env var names, never the key;AGENTS.mdgets a short block so later agent sessions know to run the setup check. Then a claim link for you.
The check that says it's done
The setup check reads what each service has received and returns ready, a summary, one row per check with a status and a fix, and next_steps in the order to do them. This is a real answer from a throwaway project we created for the benchmark, right after its first test pageview, trimmed:
curl -s "https://agentfirststack.com/v1/setup?services=analytics" -H "Authorization: Bearer $AGENTFIRSTSTACK_API_KEY"
{
"ready": false,
"summary": "Not ready: 6 missing, 2 warning of 11 checks. Fix them in the order of next_steps, then run this check again.",
"checks": [
{ "id": "analytics.site", "status": "ok", ... },
{ "id": "analytics.receiving", "status": "ok",
"detail": "1 event (1 from browsers, 0 server); the last at 2026-11-06 03:55 UTC." },
{ "id": "analytics.domain_verified", "status": "missing",
"detail": "Not verified yet: verify hasn't found the snippet on the live homepage." },
{ "id": "analytics.goals", "status": "missing", "detail": "No goals yet." },
...
],
"next_steps": [ "... Deploy the snippet, then POST .../verify (MCP analytics_verify_install)", ... ]
}
Statuses are ok, missing, failing, warning (worth a look, doesn't block), waiting_on_owner and unavailable. ready turns true when nothing is missing, failing or unavailable. ?services=analytics checks analytics alone; without it, the check covers the full setup.
What you do
- Paste the prompt.
- Answer one message. With the full setup, the agent asks once how the project inbox should forward email to you, and whether to bring over other tools it found. Then it doesn't stop to ask again.
- Give what only you have. Ad platform IDs and tokens, if you want conversions sent to Google Ads, Meta or TikTok; the agent lists exactly which ones and where you find each. A deploy, if the agent can't deploy for you.
- Open the claim link. Sign in with Google and the project is yours: sites, events, goals and the rest in the dashboard, where you can also invite teammates and connect chat apps such as claude.ai. The link works once and lasts 7 days; the agent can make another. The agent's key keeps working after you claim.
Guides for your agent
The prompt works the same in any agent that can make HTTP requests. These guides cover each one's details, such as adding the MCP server.
- AnalyticsClaude CodeOne prompt, then traffic questions from the terminal.
- AnalyticsCursorAdd analytics with Cursor's agent.
- AnalyticsCodexAdd analytics with OpenAI Codex.
- AnalyticsWindsurfAdd analytics with Windsurf.
- AnalyticsLovableAdd analytics to a Lovable app.
- AnalyticsReplitAdd analytics with Replit Agent.
- Analyticsv0Add analytics to a v0 app.
- AnalyticsBoltAdd analytics to a Bolt app.
Guides for your platform
- AnalyticsNext.jsThe snippet in the root layout, served through a first-party proxy.
- AnalyticsAstroAnalytics for an Astro site.
- AnalyticsWordPressAnalytics for a WordPress site.
- AnalyticsShopifyAnalytics for a Shopify store.
- AnalyticsWebflowAnalytics for a Webflow site.
How long it takes
The API part is short. Measured from a clean shell against production, an agent goes from no account to a site whose test pageview shows in realtime and passes the setup check's receiving check in 6 HTTP requests: 5.9 and 7.3 seconds in two runs. Deploying your site and the agent's own thinking come on top, and depend on your project. The benchmark has the method, and the steps GA4, PostHog and Plausible need, from their docs.
FAQ
Do I need to create an account first?
No. The agent gets a key with one request to POST https://agentfirststack.com/v1/signup. You get access at the end through a claim link and your Google sign-in.
What if my agent can't deploy?
It stops after making the code changes and tells you exactly what to deploy. The checks need the snippet on the live site, so it runs them after you deploy.
Can I keep Google Analytics or Plausible running?
Yes. The agent can import their history and leave them installed. GET /v1/sites/:id/imports/compare checks AgentFirstStack's daily visitors against the old tool's for the days both ran, and the agent removes the old snippet only when you say so.
I only want analytics. Can I skip the rest?
Yes. Use the analytics-only prompt on this page, and have the agent check with GET https://agentfirststack.com/v1/setup?services=analytics, which leaves out tags, mail and replay.
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 nothing is stored in the browser. Persistent mode, which keeps a random id in localStorage, is opt-in per site.
What does it cost?
The hosted service is free, with fair-use limits such as 10 analytics sites per account. The code is open source under AGPL-3.0 (the browser scripts are MIT), so you can also run it on your own Cloudflare account.
Related
- AnalyticsAgent-first analyticsWhat it is, and what to ask of any tool.
- All servicesThe easiest platform for AI agentsRequests and seconds from nothing to tracking, measured.
- AnalyticsGoogle Analytics MCP comparedRead GA4, or let the agent create the analytics.
- Google Ads conversion trackingBrowser tag and server upload, tested before it counts.
- Meta Conversions APIPixel and server, deduplicated by event id.
- All servicesOne MCP server for every serviceAnalytics, tags, mail and replay tools on one key.