Blog

· 3 min read

Automating my tracking with GTM, Claude Code and an MCP server

How I connected Google Tag Manager to Claude Code through Stape's MCP server, configured per project, and turned tracking from a chore into a two-minute task.

Every product I build reaches the same moment. The feature is done, the tests pass, the deploy is green, and then I remember: I haven't set up tracking for any of it.

That realization always sent me back to Google Tag Manager. Create a variable. Create a trigger. Create a tag. Connect them. Open Preview. Realize the selector is wrong. Fix it. Publish. Repeat for the next event.

None of it was hard, but it was slow and manual. Nothing lived in version control, and keeping naming consistent across containers took discipline. It was work that pulled me away from building the product.

Finding a better way

I already spent most of my day in Claude Code, so the question became obvious: what if the agent that writes my features could also set up their tracking?

That's where Stape came in. They maintain an MCP server for Google Tag Manager. MCP (Model Context Protocol) is how agents like Claude Code talk to external tools, and this one exposes the GTM API: listing containers, reading workspaces, creating tags, triggers and variables, and publishing versions.

Stape hosts the server, so there's nothing to install or run. I configure it per project, in a .mcp.json at the root of the repo, so the GTM tools are only available where they're relevant:

{
  "mcpServers": {
    "gtm": {
      "type": "http",
      "url": "https://gtm-mcp.stape.ai/mcp"
    }
  }
}

Authentication is a Google OAuth flow handled by Claude Code: a browser window opens the first time a GTM tool is used, and that's it. When the session expires, I ask Claude to run the server's reauthenticate tool and log in again. After that, my GTM accounts and containers show up like any other tool.

What changed in practice

Now, when a feature ships, I don't open GTM. I describe what I want:

Track clicks on every "Upgrade plan" button as an upgrade_click event, with the plan name as a parameter. Send it to GA4.

Claude Code reads the workspace to see what already exists, creates only what's missing (a click trigger, a variable for the plan name and a GA4 event tag) and tells me exactly what it changed.

The biggest difference is context. Claude Code sees my code and my container in the same conversation. It reads the components, finds the real data-* attributes and class names, and builds triggers that match them. No more guessing selectors.

Two other things improved without me trying:

  • Naming became consistent. I defined a convention once (GA4 - Event - upgrade_click, CE - upgrade_click, DLV - plan_name) and it follows it every time.
  • Audits became trivial. "Which tags fire on All Pages?" or "Which variables are unused?" used to cost me an afternoon. Now each one is a single prompt.

I still keep a few rules, because this is production tracking. The agent only works inside a workspace, never on the live version. I check every change in Preview. And publishing is always a separate step that I ask for explicitly, never a side effect.

Where I landed

GTM is still the source of truth, but I don't operate it by hand anymore. I describe the intent, review the changes and publish. What used to be the most tedious part of shipping now takes about two minutes.

More importantly, tracking stopped being an afterthought. When it costs almost nothing to add, I add it while the feature is still fresh, and the data is better for it.

If you already use Claude Code and still click through GTM, try adding Stape's MCP server to your project. It's one of those small setups that quietly changes how you work.