Posts

Session Duration Timer in GTM

Session Duration Timer in Google Tag Manager — A Complete Recipe Using Sandboxed JS Measure seconds spent on your site per session — or across all sessions — with no CSP SHA-256 hashes, no external libraries, and a single importable container JSON. Published · Tags: GTM, Google Analytics 4, Session Duration, Cookies, dataLayer, Custom Template, JavaScript Most GTM engagement-time solutions either lean on GA4's built-in engagement time (which you can't trigger mid-session) or inject inline <script> tags that break under a strict Content Security Policy. This recipe solves both problems. It uses a GTM Sandboxed JavaScript custom template — the Session or User Seconds Duration tag — to run a per-second callLater loop that writes the running count to a first-party cookie and pushes a named dataLayer event at every tick. The result is importable in one JSON file, zero SHA-256 exemptions required. ⬇ Download the GTM Container JSON from Google Drive What's ...

GTM Form Engagement & Abandonment

Image
Form Engagement Waterfall in GTM — Visibility, Field, Submit, and Response GTM Form Engagement Waterfall: Visibility, Field, Submit & Response Four Google Analytics 4 events, one shared event name, and a single contact_step_label parameter — enough to build a true form-abandonment funnel from nothing but Google Tag Manager triggers. Published · Tags: GTM, Google Analytics 4, Form Tracking, Form Abandonment, Element Visibility, dataLayer, Funnel Exploration, GA4 Standing on the shoulders of giants. This recipe is a direct continuation of two earlier posts on this blog. The field-engagement stage below is built entirely on the generic document.addEventListener pattern that Simo Ahava first published in 2013 and that I adapted for GA4 in Listen to Any Browser Event in GTM . The element-identification logic — the coalesce chains that walk up the DOM to find a usable class, id, or label — comes straight from GTM Universal Click – No JavaScript . Neither of those posts touc...

Site Landing & Site Referrer Preservation

Site Landing Page & Site Referrer to Cookies in Google Tag Manager — A Complete Recipe Without Custom JavaScript Persist the user's landing page host, landing page path, and referral source into session or long-lived persistent cookies using only GTM sandboxed Custom Templates. No Custom HTML tags. No new CSP hashes. Published · Tags: GTM, Google Analytics 4, Cookies, Site Referrer, Landing Page, First-Touch Attribution, User Journey, dataLayer, CSP, GA4 Where did this user first land on your site — and what referred them there? Those two pieces of information are foundational to understanding any user journey. But they only exist at the moment of the first page load. Navigate to a second page, return in a new browser session, or submit a form three days after the original visit, and the browser's native referrer is gone and the landing URL has long since changed. Without a persistence layer, you are flying blind. This recipe solves that problem cleanly...

UTM & URL Query String 2 Cookies

UTM & URL Query String to Cookies in Google Tag Manager — A Complete Recipe Without Custom JavaScript Persist any URL query string parameter — UTM values, click IDs, or custom campaign codes — into session or long-lived persistent cookies using only GTM sandboxed Custom Templates. No Custom HTML tags. No new CSP hashes. Published · Tags: GTM, Google Analytics 4, UTM, Cookies, Query String, dataLayer, CSP, GA4, First-Touch Attribution, Lead Attribution UTM parameters tell you exactly how a visitor arrived at your site. But they only exist in the URL on the landing page. The moment a user navigates to a second page, clicks an internal link, or returns to the site in the same browser session, those parameters vanish from the address bar. If your marketing team needs to know which campaign drove a lead form submission that happened three days after the original visit — a common scenario in B2B with longer consideration cycles — you cannot read the UTMs from the URL. ...