Skip to content

Fix gtag queue to use arguments object for proper replay - #68

Merged
andrewh merged 1 commit into
mainfrom
claude/motel-playground-analytics-data-984s24
Aug 4, 2026
Merged

Fix gtag queue to use arguments object for proper replay#68
andrewh merged 1 commit into
mainfrom
claude/motel-playground-analytics-data-984s24

Conversation

@andrewh

@andrewh andrewh commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Summary

Fixed the gtag queue implementation to push the arguments object instead of plain arrays. This ensures compatibility with gtag.js's command replay mechanism, which only processes queued entries that are the arguments object.

Key Changes

  • Extracted gtag queue logic into a new makeGtagQueue() function that properly captures and pushes the arguments object to dataLayer
  • Updated test assertions to verify queued commands are arguments objects, not plain arrays
  • Updated test to convert arguments objects to arrays for comparison using spread operator
  • Added documentation explaining the gtag queue contract and why the arguments object shape matters

Implementation Details

The gtag.js library has specific behavior: it only replays commands from dataLayer if they were pushed as the arguments object. Plain arrays are ignored during replay, which would cause the analytics tag to load without sending any hits. This change ensures the queue is shaped correctly for proper command replay when gtag.js loads.

https://claude.ai/code/session_01DLfiWrE8kmpqMM913UPHCr

Google Analytics reported the tag as detected on the Pages site but never
recorded any events. initGoogleAnalytics installed an arrow-function gtag that
pushed a rest-parameter array onto window.dataLayer. gtag.js only replays queued
entries that are the arguments object; plain arrays are ignored, so the js and
config commands and every subsequent event were dropped after the tag script
loaded. Nothing was ever sent to /g/collect.

- web/telemetry.mjs: replace the arrow function with makeGtagQueue, a named
  function that pushes its arguments object, matching the canonical gtag
  snippet. The existing window.gtag reuse path and the rest of the GA setup are
  unchanged.
- scripts/telemetry-test.mjs: assert every queued dataLayer entry is an
  arguments object, so a rewrite back to an array push fails the suite, and
  spread the config command before comparing it against the expected argument
  list.
- docs/telemetry.md: record the queue-shape requirement and the detected-but-
  silent failure mode it causes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DLfiWrE8kmpqMM913UPHCr

Copilot AI 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.

🟢 Ready to approve

The change is narrowly scoped, aligns the queue with gtag.js’s expected replay shape, and includes matching test and documentation updates.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR fixes the Google Analytics gtag bootstrap queue in web/telemetry.mjs so queued commands are pushed to window.dataLayer as the arguments object (matching what gtag.js replays), rather than as plain arrays that can be ignored during replay.

Changes:

  • Replaced the inline (...args) => dataLayer.push(args) queue with a makeGtagQueue(win) implementation that pushes arguments.
  • Updated the telemetry smoke test to assert queued dataLayer entries are Arguments objects and to compare content by spreading into arrays.
  • Documented the gtag queue contract and why the arguments shape matters for replay.
File summaries
File Description
web/telemetry.mjs Uses a dedicated makeGtagQueue() that pushes arguments objects to dataLayer for gtag.js replay compatibility.
scripts/telemetry-test.mjs Strengthens assertions to validate dataLayer entry shape and adapts comparisons for Arguments objects.
docs/telemetry.md Adds documentation describing the gtag queue contract and replay implications.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

@andrewh
andrewh merged commit dd592e7 into main Aug 4, 2026
5 checks passed
@andrewh
andrewh deleted the claude/motel-playground-analytics-data-984s24 branch August 4, 2026 19:53
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.

3 participants