Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/analytics/ga4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ export function initGoogleAnalytics(): void {
if (!id || (!isGA4ProductionHost(host) && !isGA4DebugHost(host))) return;
try {
window.dataLayer = window.dataLayer || [];
window.gtag = function gtagShim(...args: unknown[]) {
window.dataLayer!.push(args);
// MUST push `arguments`, not a rest-args array — gtag.js's command
// interpreter only processes Arguments objects in dataLayer; plain
// arrays are ignored (container registers but no hits ever flush).
window.gtag = function gtagShim() {
window.dataLayer!.push(arguments);
};
window.gtag('js', new Date());
window.gtag('config', id, {
Expand Down
8 changes: 8 additions & 0 deletions test/ga4-analytics.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ test('config disables automatic page_view and ads/identity features', () => {
assert.match(facade, /debug_mode: isGA4DebugHost/, 'localhost must run debug_mode');
});

test('dataLayer shim pushes arguments — plain arrays are ignored by gtag.js', () => {
// gtag.js's command interpreter only processes Arguments objects; a
// (...args) => push(args) shim silently drops every command.
const facade = codeOnly('src/analytics/ga4.ts');
assert.match(facade, /push\(arguments\)/);
assert.doesNotMatch(facade, /push\(args\)/);
});

test('gtag script loads async from googletagmanager.com only', () => {
const facade = src('src/analytics/ga4.ts');
assert.match(facade, /script\.async\s*=\s*true/);
Expand Down
Loading