Skip to content
Faisal Mahmud TusherDigital marketing, SEO & analytics
with Faisal Tusher

AI in Marketing › AI Applications in Marketing

Checking AI-Written Tracking Code Before It Ships

A routine for using AI to draft data layer pushes, tags, triggers and regular expressions, then proving them in preview against the tracking plan and consent.
Tutorialadvanced10 min readUpdated

Why generated tracking code needs proof

Tracking code is a contract between three things: the tracking plan, which says what each event is called, what parameters it carries, where it fires and which consent category it depends on; the site, which pushes messages into the Data Layer; and the Tag Manager container, which reads those messages and decides what to send. Code that breaks the contract usually still runs. The container does not know that booking_confirmed should have been book_appointment, that a parameter called customer_email should not exist, or that a page path pattern also matches the staff area.

A model writing that code fills every gap with the most plausible continuation, as the guide to how language models generate text explains. Where the plan's names are not in front of it, it invents names that look like the conventions it has seen. Where the markup is not in front of it, it guesses at selectors and URLs. Where its training text was older than the current documentation, it reproduces older patterns. The output is a draft to be proved, not a setup to be pasted. The academy exercise on proving a sewing machine retailer's generated revenue query and sample tag practises proving one such draft, a fabric sample request tag whose names, postcode parameter and loose page pattern all departed from its plan row, and previewing the rebuild with consent accepted and then refused.

The failures that matter are quiet, and the routine below is built to surface each one.

FailureWhat it looks likeWhere the routine catches it
Invented namesAn event or parameter the plan never defined, so reports split or stay emptyChecking every name against the plan before preview
Personal data in valuesAn email address, name or phone number copied into a parameter or a page pathReading every value's source and testing with invented details
Over-matching patternA trigger or regular expression that also fires on test, staff or unrelated pagesTesting the pattern against pages that should not match
Under-matching patternA trigger that misses a template, a language version or a query stringTesting against every page the plan's location column names
Consent ignoredA tag that sends data when the visitor refused, or a custom script that sets its own storageRunning preview with consent granted and refused
Wrong timingA push before the values exist, or a tag that reads a variable on the wrong eventReading the data layer state on the firing event

Before you start

  • The tracking plan row for the event: its name, parameters with types, trigger condition, location, destinations and consent category. If no row exists, write it first, using the measurement plan for the business question the event answers.
  • The data layer specification, if the site has one, as the data layer guide describes: what is pushed, when, with which keys and value types.
  • The template markup of the pages involved, saved from the template or from a test page filled with invented values, plus a list of the site's URLs that the trigger should and should not match.
  • An approved AI tool. One your organisation permits for this work. Follow the data minimisation tutorial for what may go into it.
  • Edit access to a workspace in the container and a role in the GA4 property that can open DebugView.
  • The container's change rules, as set out in the container governance guide, and the team's container publish checklist.

The steps

  1. Write the request from the tracking plan row

    Start from the plan, not from a description in your head. Copy the row into the request: the event name exactly as written, each parameter with its type and where its value comes from, the condition that should fire it and the conditions that must not, the pages it applies to and the consent category. Say what you want back: a data layer push for the developer, the variables and trigger for the container, and the tag, using a built-in template where one exists rather than custom HTML, as the tags, triggers and variables guide advises. Ask the model to list every assumption it had to make and every name it used that was not in the request.

  2. Give the model the plan's names and the page's real markup, never customer data

    Paste the template's markup or the relevant fragment, the site's existing data layer conventions and the GA4 event and parameter names the plan uses. Without real markup the model guesses at element identifiers and page paths; without the plan's names it invents its own. Leave out anything a customer produced: no real confirmation pages, no form submissions, no exported events. Replace real values with invented ones of the same shape. The model needs the structure to write the code; it does not need the data that will later flow through it.

  3. Read the code line by line against the plan

    Run nothing yet. Read the generated push, variables, trigger and tag one line at a time and mark each as matching the plan, differing from it or unclear. Check where each value comes from: a data layer key, a page element, the address bar, a cookie. Check that the push happens at the moment the plan describes, after the booking or order is confirmed rather than on the button click, which the data layer guide treats as part of the contract. Compare the model's explanation with what the code actually does; the explanation can describe the code the model meant to write. In any custom HTML or custom JavaScript, look for anything beyond the stated purpose: extra requests, storage, or a third-party script loaded from an address nobody recognises.

  4. Check every event and parameter name against the plan

    List every event name, parameter name and data layer key the code uses and set it beside the plan row. Each must match the plan exactly, including case and separators, because a variable that reads a key with a different spelling returns nothing and a tag that sends a new parameter name creates a new, empty column in reports. Where the plan follows GA4's documented names for an event, the code should too; the GA4 events and parameters guide explains how recommended and custom event parameters differ. Any name the model added that the plan does not hold is either removed or proposed as a change to the plan, never shipped by default.

  5. Check what personal data the code could send

    For every value the code sends, ask what it could contain on a real page. A parameter filled from a form field, the page title, the full address or a confirmation message can carry a name, an email address or a phone number. The data layer guide's rule is that personal data does not belong in the data layer, and the guide to keeping personal data out of analytics lists the routes it takes, from query strings to page titles. Remove any value whose source can hold personal details, and where the plan needs an identifier, use the internal one the plan names. Then fill in the test form with obviously invented details so that, if anything leaks, the preview in a later step shows it.

  6. Test the trigger and any regular expression against pages that should and should not match

    Take the list of URLs gathered before you started and split it in two: pages where the tag must fire and pages where it must not, such as staff screens, test bookings, other templates and language versions. Check the trigger's conditions, and any regular expression in a trigger or a regex table, against every entry in both lists. A generated pattern is often written for the example in the prompt and is either too loose, matching the staff area that shares a path, or too tight, missing the query string a payment provider adds. Where a blocking rule already exists for internal traffic or a staging hostname, use it as an exception rather than writing a new one.

  7. Run it in preview and DebugView with consent granted and refused

    Load the workspace in preview and follow the debugging and preview tutorial: find the event in the timeline, confirm the tag fired on it and not on others, read the resolved values in the Tags and Variables tabs, and read the data layer state on the firing event. Then confirm arrival in DebugView and, in the browser's network panel, what actually left the page. Do it twice: once after accepting the consent banner and once after refusing it. The consent mode guide explains that Google's tags adjust to the consent state on their own, while other vendors' tags need consent settings in the container that hold them until the required types are granted; the preview panel shows when a consent requirement held a tag back. A tag that behaves the same under both choices needs a second look. If the tag does not fire, the tag not firing guide works through the causes.

  8. Review, name and publish through the container's change rules

    Generated code goes through the same door as any other change. Name the tag, trigger and variables to the container's conventions, write the workspace description, and hand it to whoever holds publish rights for review, as the container governance guide sets out. Say in the description that the code was drafted with an AI tool, so the reviewer reads custom script with that in mind. Work through the container publish checklist before the version goes live, and check the event on the live site afterwards, since production templates, banners and caching can differ from what preview saw.

  9. Record the change and the prompt beside the tracking plan

    Update the tracking plan row with the implementation status and the version that shipped it. Keep the request, the model's output, the changes made during review and the test results with the row or in the container's documentation. The saved request, with its corrections, is a better starting point for the next event than a fresh prompt. Where the team keeps an inventory of AI uses, add this one, so a later audit knows which tracking was drafted with a model.

Generated tracking code is one case of the wider rule in the AI for analysis and reporting guide: anything a model produces that feeds a number is checked before the number is used. The applications hub places this routine among the other AI tasks a marketing team runs. Before the generated push or tag ships, the AI-written code review checklist turns this routine and its companions into one tickable gate. The academy capstone on a year of AI in production for a children's shoe retailer practises writing that gate and applying it to a fitting guide tag that went live without a preview, inside a plan that also launches a chat assistant and tests three agency proposals for ad automation.

A cycle repair chain checks a generated booking event (illustrative)

Illustrative example — not a real result

Jurnavik Cycle Repairs checks a generated booking-confirmed push and trigger

Jurnavik Cycle Repairs, an invented chain of 7 bicycle workshops with online booking, asks an AI tool for a push and a trigger for a confirmed service booking. The tracking plan row names the event service_booked with 4 parameters: workshop_id, service_type, value and currency, consent category analytics.

The analyst pastes the plan row and the confirmation template with invented values. The model returns a push, a data layer variable for each key, a custom event trigger and a GA4 event tag. The line-by-line read and the checks find 3 problems:

FindingWhereFix
A fifth parameter, booking_ref_type, that the plan never definedPush and tagRemoved; proposed to the plan owner, who declined it
customer_email filled from the form's email fieldPushRemoved; the plan's rule bans personal data in parameters
A page path pattern matching the confirmation page and 3 staff booking screensTriggerReplaced with the custom event trigger on the push alone, with the internal traffic exception

Preview with consent granted shows the tag firing once on the confirmation with 4 parameters; with consent refused, the GA4 tag adjusts as documented and a third-party pixel the model added is held back by its consent setting. The team removes the pixel, which nobody had asked for. The publisher reviews the workspace against the checklist, and the plan row records the version, the prompt and the 3 fixes. A live check the next morning finds 12 bookings recorded, each carrying all 4 parameters and no email address.

Common mistakes

  • Asking from memory instead of the plan. Without the plan's names in the request, the model supplies its own and the reports split.
  • Pasting a real customer's page as context. The template with invented values gives the model everything it needs; customer details do not belong in a prompt.
  • Treating "it fired" as "it is right". Firing proves a trigger matched once; names, values, patterns and consent are separate checks.
  • Testing only the page that should match. Over-matching triggers are found by testing the pages that should stay quiet.
  • Testing with consent granted only. The refused branch is where generated custom script most often misbehaves.
  • Accepting custom HTML where a template exists. Ask for the template; review every line of any script that remains.
  • Skipping the container's review because the code came quickly. Generated changes go through the same publish rules as any other.
  • Not recording the prompt and the fixes. The next event starts from scratch and repeats the same mistakes.

Frequently asked questions

If the generated tag fires in preview, is it ready to publish?

No. Firing proves the trigger matched on the page you tested; it does not prove the event and parameter names match the tracking plan, that no value carries personal data, that the trigger stays quiet on pages it should ignore, or that the tag respects a refused consent choice. Those are separate checks, and preview is where some of them are run, not the whole of the proof.

Can I paste a real order confirmation page into the AI tool so it sees the markup?

Paste the template, not a page a customer produced. A rendered confirmation page carries names, addresses and order details that have no place in a prompt. Save the template's markup, or a test page filled with invented values, and give the model that together with the tracking plan's names, which is all it needs to write the code.

Is it safer to ask the AI tool for a custom HTML tag or for a template-based setup?

Ask for the template-based setup wherever a built-in or gallery template exists for the job. Templates run in a sandbox and declare their permissions, so they are easier to review than arbitrary script, and the site's tag manager guides advise them over custom HTML. Keep custom HTML for the rare case with no template, and have someone who can read the script review every line before it goes into the container.

Terms used on this page

Frameworks

Where this page is used

Part of these learning paths

Sources

  1. Preview and debug containers — Google Tag Manager Help
  2. The data layer — Google Tag Platform
  3. [GA4] About events — Google Analytics Help
  4. Consent mode — Google Tag Platform

Related