[object Object]

← back to Dw Domain Fleet

GA4: wire gtagSnippet() into head() for per-site GA4 injection

41a3f8ffb06c8656df896fa62cd8b5b9a2aeec74 · 2026-08-17 11:21:17 -0700 · Steve Abrams

Add gtagSnippet(mid) helper that emits the GA4 gtag loader script.
Wire it into head() reading cfg.ga4 (measurement ID from site JSON).
When cfg.ga4 is absent/empty, renders nothing — backward-compatible,
all 41 existing fleet sites unaffected.

Sites commercialwallcovering + hollywoodwallcovering will get their
G-IDs injected once the SA account-level Editor grant unblocks property
creation (see TK-10631 / pending-approval/TK-10631-ga4-dw-fleet-two-sites-deploy.md).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files touched

Diff

commit 41a3f8ffb06c8656df896fa62cd8b5b9a2aeec74
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Aug 17 11:21:17 2026 -0700

    GA4: wire gtagSnippet() into head() for per-site GA4 injection
    
    Add gtagSnippet(mid) helper that emits the GA4 gtag loader script.
    Wire it into head() reading cfg.ga4 (measurement ID from site JSON).
    When cfg.ga4 is absent/empty, renders nothing — backward-compatible,
    all 41 existing fleet sites unaffected.
    
    Sites commercialwallcovering + hollywoodwallcovering will get their
    G-IDs injected once the SA account-level Editor grant unblocks property
    creation (see TK-10631 / pending-approval/TK-10631-ga4-dw-fleet-two-sites-deploy.md).
    
    Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---
 shared/render.js | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/shared/render.js b/shared/render.js
index fc8483f..8380cdc 100644
--- a/shared/render.js
+++ b/shared/render.js
@@ -452,6 +452,14 @@ function script(cfg) {
  * site's own URL. Product pages pass the SKU's canonical home on the main
  * store so shared SKUs never compete as duplicate content (panel SEO must-do).
  */
+function gtagSnippet(mid) {
+  if (!mid) return '';
+  return `<!-- Google Analytics 4 -->
+<script async src="https://www.googletagmanager.com/gtag/js?id=${mid}"></script>
+<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','${mid}');</script>
+<!-- /Google Analytics 4 -->`;
+}
+
 function head(cfg, title, desc, path, canonicalOverride, bodyClass) {
   const url = 'https://' + cfg.domain + (path || '');
   const canonical = canonicalOverride || url;
@@ -466,7 +474,7 @@ function head(cfg, title, desc, path, canonicalOverride, bodyClass) {
 <meta property="og:url" content="${esc(url)}">
 <meta name="twitter:card" content="summary_large_image">
 <meta name="twitter:title" content="${esc(title)}"><meta name="twitter:description" content="${esc(desc)}">
-<style>:root{--top-fg:#fff}</style>
+${gtagSnippet(cfg.ga4)}<style>:root{--top-fg:#fff}</style>
 <script>(function(){var s=localStorage.getItem('${cfg.slug}_theme');if(s)document.documentElement.dataset.theme=s;})();</script>
 <style>${css(cfg)}</style>
 </head><body${bodyClass ? ` class="${esc(bodyClass)}"` : ''}>`;

← 187f55c Revert "AdSense: env-gated loader (ADS_ENABLED=1) in shared  ·  back to Dw Domain Fleet  ·  GA4: inject gtag via render.js head() + ga4 IDs for commerci aebcb81 →