Skip to content

fix(worker): make the Google Ads conversion tag env-driven instead of a dead placeholder - #52

Closed
nish3451 wants to merge 11 commits into
mainfrom
fix/google-ads-conversion-tag
Closed

fix(worker): make the Google Ads conversion tag env-driven instead of a dead placeholder#52
nish3451 wants to merge 11 commits into
mainfrom
fix/google-ads-conversion-tag

Conversation

@nish3451

@nish3451 nish3451 commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

What

The funnel's only Google Ads conversion measurement was dead by construction:

  1. public/brief-requested.html hardcoded the gtag loader with the AW-XXXXXXXXX placeholder — a tag that can never record a conversion.
  2. public/brief-requested.js fired the conversion event to the same placeholder (AW-XXXXXXXXX/YYYYYYYYYYYYYYYYYYY).
  3. Even if real values had been pasted in, the Worker's production CSP (script-src 'self' https://static.cloudflareinsights.com) blocks googletagmanager.com scripts and gtag's beacon endpoints, so the tag could never load or send under the served CSP.
  4. Nothing failed when the placeholder shipped, so the dead state was silent.

Fix

The tag is now generated by the Worker at request time from env values and only ever emitted when it can actually work:

  • src/worker.js reads GOOGLE_ADS_CONVERSION_ID + GOOGLE_ADS_CONVERSION_LABEL, validates both with strict patterns (AW- + digits; 10+ char alphanumeric label), and only then:
    • injects the gtag loader into the /brief-requested HTML response, and
    • generates /brief-requested.js with the real config + conversion event.
  • The gtag CSP allowances (googletagmanager.com script; googleads.g.doubleclick.net / googleadservices.com / google-analytics.com / stats.g.doubleclick.net connect; googleadservices.com img) are applied only to the /brief-requested response via a page-scoped CSP — every other page keeps the strict CSP. With either value missing or malformed, the page ships with no tag at all rather than a dead one.
  • Placeholders are removed from the committed public/ files (brief-requested.js is a documented no-op until configured).
  • CI guards in scripts/check-site.mjs refuse: any placeholder in public/ or src/worker.js, any hardcoded gtag in the served HTML, any static conversion firing in brief-requested.js, and any drift of the worker's env wiring.
  • specs/003-wellness-clinic-launch/tracking-setup.md updated: activation is now wrangler secret put GOOGLE_ADS_CONVERSION_ID / GOOGLE_ADS_CONVERSION_LABEL (.dev.vars for dev) instead of pasting into HTML.

Activation (console-side, cannot be done in code)

Creating the "Brief requested" conversion action still requires the Google Ads console (the spec documents this as account-side work): create the action, copy the AW-… ID and label, wrangler secret put both, deploy, then verify with a live test submission before any spend.

Validation

  • npm run check — passed (incl. new placeholder/guard checks)
  • npm test — 85 tests pass, incl. 3 new: no tag while unconfigured; correct injection + page-scoped CSP when configured; nothing emitted for partial/malformed config
  • npm run check:render-blocking (real Chromium, production CSP) — all 6 pages PASS; /brief-requested no longer triggers the gtag placeholder console error
  • npx wrangler deploy --dry-run — clean

Summary by CodeRabbit

  • New Features

    • Added configurable Google Ads conversion tracking for the brief-requested confirmation page.
    • Tracking is added only when valid conversion settings are configured; incomplete or invalid settings leave tracking disabled.
    • Security permissions for tracking are limited to the confirmation page.
  • Bug Fixes

    • Removed hardcoded placeholder conversion tracking from public pages.
  • Documentation

    • Updated setup guidance to cover required conversion settings.

… a dead placeholder

The funnel's only Google Ads conversion measurement was dead by
construction: brief-requested.html hardcoded the gtag loader with a
placeholder conversion id, brief-requested.js fired the event to the same
placeholder, and the production CSP blocked googletagmanager.com entirely
— so even a real id pasted in would never load or record.

The tag is now generated by the worker at request time from
GOOGLE_ADS_CONVERSION_ID / GOOGLE_ADS_CONVERSION_LABEL, emitted only on
/brief-requested when both are configured and well-formed, with gtag's
CSP allowances scoped to that one noindex page's response. With either
value missing or malformed, the page ships with no tag at all. CI now
refuses any placeholder or hardcoded gtag in public/ or src/worker.js, so
the dead-by-construction shape cannot return; the tracking spec documents
the secret puts that activate the tag once the console-side conversion
action exists.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@nish3451, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 61fe1741-390c-4276-8d0a-4d03d3d8df64

📥 Commits

Reviewing files that changed from the base of the PR and between 33eb558 and c6a5d0d.

📒 Files selected for processing (1)
  • scripts/check-site.mjs
📝 Walkthrough

Walkthrough

Google Ads conversion tracking now uses validated Worker environment values. The Worker conditionally injects tracking and scoped CSP rules for /brief-requested. Static checks, runtime tests, and setup documentation enforce the new configuration model.

Changes

Google Ads conversion tracking

Layer / File(s) Summary
Worker injection and security headers
src/worker.js
The Worker validates conversion credentials, generates tracking markup, rewrites /brief-requested, and applies a page-specific CSP only when configuration is valid.
Static asset and configuration contract
public/brief-requested.html, public/brief-requested.js, specs/003-wellness-clinic-launch/tracking-setup.md
Static conversion tags and placeholders were removed. Documentation now requires Worker secrets or .dev.vars values.
Static guards and runtime tests
scripts/check-site.mjs, scripts/test-agent-worker.mjs
Checks reject static or placeholder tracking. Tests cover valid, missing, partial, and malformed configuration.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Request
  participant Worker
  participant Environment
  participant Browser
  Request->>Worker: Request /brief-requested
  Worker->>Environment: Read conversion ID and label
  Environment-->>Worker: Return configuration
  Worker->>Browser: Serve rewritten HTML and scoped CSP
  Browser->>Worker: Request generated conversion script
  Worker-->>Browser: Return gtag loader and conversion event
Loading

Possibly related PRs

  • nish3451/TinyStudio.io#8: Changes the same tracking files and Google Ads flow from hardcoded tags to Worker-injected configuration.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: making the Google Ads conversion tag environment-driven instead of using a placeholder.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/google-ads-conversion-tag

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 33eb558ce5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/worker.js
// (see googleAdsConversion below); when they are not, the page ships with
// the strict CSP and no tag at all.
const GOOGLE_ADS_CSP =
"default-src 'self'; img-src 'self' data: https://www.googleadservices.com; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; script-src 'self' https://static.cloudflareinsights.com https://www.googletagmanager.com; connect-src 'self' https://cloudflareinsights.com https://www.googletagmanager.com https://googleads.g.doubleclick.net https://www.googleadservices.com https://www.google-analytics.com https://stats.g.doubleclick.net; base-uri 'self'; frame-ancestors 'none'; form-action 'self'";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Allow Google Ads pixels under img-src

When gtag sends a conversion or view-through beacon as an image request to googleads.g.doubleclick.net (and, for some Google Ads flows, www.google.com), this policy blocks it: placing DoubleClick only in connect-src does not authorize image loads, while img-src permits only www.googleadservices.com. This can silently discard conversion signals even though the tag loads successfully, corrupting the paid-search experiment; add every Google Ads image-beacon origin to img-src and exercise the configured Worker response in the browser check.

Useful? React with 👍 / 👎.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 60dda491da

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/worker.js
Comment on lines +1404 to +1405
const assetResponse = await env.ASSETS.fetch(request);
if (assetResponse.ok) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle conditional 304s before injecting the tag

When a browser revalidates a previously cached, unconfigured /brief-requested page after the Ads secrets are enabled, the preserved static-asset ETag can make this fetch return 304. Because a 304 has ok === false, execution falls through and repeats the same conditional asset request, returning a strict-CSP 304 and causing the browser to reuse the cached tag-free HTML. Returning visitors—and potentially the post-activation verification browser—therefore remain untracked until their cache is bypassed; fetch the source HTML without request validators or prevent the unconfigured confirmation response from being cached.

Useful? React with 👍 / 👎.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d9e326287

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/worker.js
Comment on lines +1398 to +1401
return withSecurityHeaders(
new Response(googleAdsScript(ads), {
headers: { "Content-Type": "text/javascript;charset=UTF-8" }
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark the generated conversion script non-cacheable

When the Google Ads ID or label is rotated after a visitor has loaded this endpoint, this generated 200 response has neither Cache-Control nor a config-specific URL, so a browser cache may reuse JavaScript containing the previous send_to value. The newly injected HTML can then load gtag for the new ID while the unchanged /brief-requested.js attributes the conversion to the old action; return this env-derived script with Cache-Control: no-store (and apply equivalent cache protection to the generated HTML).

Useful? React with 👍 / 👎.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 29919a5e63

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/worker.js
url.pathname === "/brief-requested" || url.pathname === "/brief-requested.html";
const isBriefRequestedScript = url.pathname === "/brief-requested.js";

if (ads && request.method === "GET" && (isBriefRequestedPage || isBriefRequestedScript)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Load the Google tag before the confirmation page

When a paid-search visitor lands on / or /audit with a gclid/wbraid, this condition excludes that landing response, so Google's conversion linker never gets a chance to persist the click identifier; htmlRedirect() then clears the query before navigating to /brief-requested. In browsers where third-party attribution is unavailable, the resulting event cannot be tied back to the campaign, causing the paid-search experiment to undercount conversions. Load the base Google tag/conversion linker on the landing pages while keeping the conversion event restricted to the confirmation page, or otherwise preserve the click identifier through the flow.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@nish3451

Copy link
Copy Markdown
Collaborator Author

Refreshed onto current main and re-verified (lane-1 run, 2026-08-11).

Branch merged latest origin/main (ee50e17, docs-only) — no conflicts. Verified locally at the new head:

  • npm test — full suite green (check / headings / sitemap / worker / UI / contract; 93 tests, 0 failures), including the three Google Ads conversion tests:
    • no tag ships while env unconfigured
    • tag + scoped CSP injected on /brief-requested only when configured
    • partial/malformed config emits nothing
  • npm run check:render-blocking — all six pages PASS, including brief-requested
  • wrangler deploy --dry-run — bundle OK

Ready to merge. Remaining step (console-side, per tracking-setup.md): create the Brief requested conversion action in Google Ads and wrangler secret put the ID/label — the tag then goes live with no code change.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@nish3451

Copy link
Copy Markdown
Collaborator Author

Superseded by #136: this branch went stale and conflicts with current main (favicon commits touched the same files). The identical verified fix is re-applied onto fresh main in #136, with the same tests green (npm test 95/0, render-blocking PASS, deploy dry-run OK). Reopen if you'd rather merge this branch after a refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant