---
title: Your dataLayer Has No Contract. Write a Tracking Plan as JSON Schema and Test It in CI.
description: A tracking plan in a spreadsheet cannot stop a bad commit. Write it as JSON Schema, let an LLM draft it from the events you already send, and fail the build when the dataLayer breaks the contract.
author: LETSGROW Dev Team
date: 2026-11-28
category: Analytics
tags: ["Tracking Plan", "GA4", "JSON Schema", "Playwright", "LLM"]
url: "https://letsgrow.dev/blog/tracking-plan-as-code-json-schema-datalayer-ci"
---
Most tracking plans are spreadsheets. They list event names, a few parameters, and a column called "Status" that nobody has updated since the GA4 migration. Meanwhile the real tracking plan lives in production: whatever your developers happened to push into `window.dataLayer` this sprint. When the spreadsheet and the site disagree, the site wins, and your reports quietly inherit the mess.

Our position: a tracking plan that is not executable is documentation, not a plan. Write it as JSON Schema, keep it in the same repository as the site, and fail the build when a page pushes an event that breaks it. Then use an LLM for the one job it is good at here: turning the chaos you already have into a first draft of that schema.

::stat-block
- value: "40"
  label: "Characters allowed in a GA4 event name before collection rules bite"
- value: "25"
  label: "Parameters GA4 accepts per event"
- value: "100"
  label: "Characters allowed in a standard GA4 parameter value"
::

## Why the Spreadsheet Always Loses

Analytics breakage is almost never dramatic. Nobody deletes the purchase event. Instead, a component refactor renames `form_id` to `formId`. A new landing page template sends `value` as the string "49.00" instead of a number. A developer adds `generate_lead` to a newsletter form because it seemed close enough. Each change is small, passes code review, and silently splits one metric into two.

You find out weeks later, when a dashboard shows conversions down 30 percent and someone spends two days proving it is a naming change rather than a demand problem. The spreadsheet could not have caught it, because the spreadsheet never runs.

GA4 makes this worse in specific ways. It accepts almost anything you send, case sensitive, and it truncates or drops values that exceed its collection limits rather than rejecting them loudly. Custom dimensions have to be registered against exact parameter names. A typo does not throw an error. It just creates a dimension that is always empty.

## Write the Plan as Schema, Not Prose

JSON Schema is the right format because it is boring, well supported, and validators exist in every language. Ajv is the standard choice in JavaScript. Create one schema file per event in a `tracking/` folder, and a small index that maps event names to schemas.

A good event schema does four things. It pins the event name with a `const`. It lists required parameters, so `generate_lead` without `form_id` fails. It types every value, so `value` must be a number and `currency` must match a three-letter pattern. And it sets `additionalProperties: false`, so undocumented parameters fail instead of drifting in unnoticed. That last rule is the one teams resist and the one that pays off most, because it forces every new parameter through a pull request where someone can ask whether a custom dimension exists for it.

Add GA4's own constraints as schema rules too: `maxLength: 40` on parameter names via `propertyNames`, `maxLength: 100` on string values, and a regex on event names that enforces snake_case. Now your plan encodes both your business rules and the platform's limits in one place.

::compare-table
title: "Spreadsheet Plan vs Tracking Plan as Code"
columns: ["Factor", "Spreadsheet", "JSON Schema in the repo"]
rows:
  - ["Catches renamed parameters", "No", "Yes, on the pull request"]
  - ["Catches wrong value types", "No", "Yes, with a typed schema"]
  - ["Blocks undocumented parameters", "No", "Yes, with additionalProperties false"]
  - ["Change history", "Cell edits nobody reviews", "Git diffs with a reviewer"]
  - ["Enforces GA4 limits", "Only if someone remembers", "Encoded as schema rules"]
  - ["Cost to adopt", "Already exists", "A day to draft, an hour per new event"]
::

## Let an LLM Draft the Schema From What You Already Send

The objection is always the backfill. Nobody wants to hand write schemas for 60 events. You do not have to.

Start by capturing reality. Write a Playwright script that visits your top 30 templates, performs the key interactions, and records every object pushed to `window.dataLayer`. The trick is to wrap `dataLayer.push` with an init script before the page loads, so you log each push with the URL and a timestamp. You can also pull a week of event names and parameter keys from the GA4 BigQuery export if you have it. Either way, you end up with a JSON file of real payloads grouped by event name.

Then hand each group to an LLM with a strict instruction: produce a JSON Schema draft for this event, mark a parameter as required only if it appears in every sample, infer types from the values, flag any parameter whose type varies across samples, and flag names that look like duplicates of each other, such as `formId` and `form_id`. Require structured output so the response is a schema plus a list of findings, not an essay.

The model is good at this because it is pattern recognition over messy samples, and the output is reviewable. It is not the final authority. A human who owns analytics reviews every draft, resolves the duplicates, and decides the canonical names. Expect the first pass to surface problems you did not know about: in our experience, it is rare to capture a site's events and find zero type conflicts.

## Wire It Into CI and Local Development

Validation runs in two places.

In CI, reuse the Playwright capture. On every pull request that touches the site, run the journeys against a preview deploy, collect the dataLayer pushes, and validate each one against its schema with Ajv. Fail the build on any event that has no schema, any missing required parameter, any type mismatch, and any undocumented parameter. Print the exact path that failed, so the developer sees `generate_lead.value: must be number` instead of a vague red X.

In local development, add a lightweight guard to development builds only: wrap `dataLayer.push` and log a console warning when a payload fails validation. Developers see the problem while they build, before CI ever runs. Do not ship the validator to real users. It costs bytes and blocks nothing useful there.

Finally, treat schema changes like API changes. Adding an optional parameter is a minor change. Renaming or removing one is breaking, and the pull request should name the reports and custom dimensions it affects. When the schema changes, register any new custom dimension in GA4 in the same release, so the data has somewhere to land on day one.

::checklist
title: "Ship a Tracking Plan as Code This Week"
items:
  - "Create a tracking folder in the site repo with one JSON Schema file per event"
  - "Capture real dataLayer pushes from your top templates with a Playwright init script"
  - "Have an LLM draft schemas from the samples and list type conflicts and duplicate names"
  - "Review every draft with the analytics owner and pick canonical names"
  - "Set additionalProperties to false and encode GA4 length limits in the schemas"
  - "Validate captured events with Ajv in CI and fail the build on any violation"
  - "Add a development-only console warning for invalid pushes"
  - "Register new custom dimensions in GA4 in the same release as the schema change"
::

Your analytics are only as reliable as the least careful commit that touched a form component. A spreadsheet cannot stop that commit. A failing build can. Capture what you send today, let a model draft the contract, and make the contract run.
