Last active
April 14, 2026 15:46
-
-
Save ceaksan/e7aae3e2a5cd272dae7365f739ee1e8f to your computer and use it in GitHub Desktop.
GTM Custom HTML: Purchase source enrichment — Reads dnm_src/dnm_cid via storageHelper (cookie-first, localStorage fallback) at purchase, pushes first_touch_source/medium/campaign/click_id to dataLayer as GA4 event-scope custom dimensions. Does NOT affect attribution model. Requires storageHelper companion gist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * GTM Custom HTML Tag — Purchase source enrichment | |
| * File: gtm-purchase-enrichment.js | |
| * | |
| * What it does: | |
| * When a purchase event fires, reads dnm_src / dnm_cid via storageHelper | |
| * and pushes custom event parameters to dataLayer for the GA4 | |
| * purchase tag to pick up. | |
| * | |
| * Requires: | |
| * storageHelper (see companion gist) must be loaded BEFORE this tag. | |
| * Use GTM Tag Sequencing to ensure correct order. | |
| * | |
| * Parameters sent to GA4: | |
| * first_touch_source → e.g. "google" | |
| * first_touch_medium → e.g. "cpc" | |
| * first_touch_campaign → e.g. "TR_branded_max" | |
| * first_touch_click_id → e.g. "gclid:Cj0KCQjw..." | |
| * | |
| * IMPORTANT: | |
| * These parameters are sent as event-scoped custom dimensions. | |
| * They must be defined in GA4 Admin → Custom definitions (Event scope). | |
| * They do NOT affect the attribution model — used for reporting only. | |
| * | |
| * Trigger: | |
| * In GTM, fire this tag BEFORE your GA4 purchase tag | |
| * (use Tag Sequencing or bind to your purchase trigger). | |
| */ | |
| (function () { | |
| /* --- 1. Read from storageHelper (cookie-first, localStorage fallback) */ | |
| var rawSource = storageHelper.get("dnm_src"); // "source|medium|campaign|content|term" | |
| var rawCid = storageHelper.get("dnm_cid"); // "gclid:Cj0K..." or "fbclid:Ab12..." | |
| /* --- 2. Parse -------------------------------------------------- */ | |
| var parts = rawSource ? String(rawSource).split("|") : []; | |
| var source = parts[0] || "(not set)"; | |
| var medium = parts[1] || "(not set)"; | |
| var campaign = parts[2] || "(not set)"; | |
| // parts[3] = content, parts[4] = term — extend if needed | |
| var clickId = rawCid ? String(rawCid) : "(not set)"; | |
| /* --- 3. DataLayer push ----------------------------------------- */ | |
| // GA4 purchase tag reads these values. | |
| // Add them as "Event Parameters" in your GA4 purchase tag in GTM. | |
| window.dataLayer = window.dataLayer || []; | |
| window.dataLayer.push({ | |
| event: "dnm_purchase_enriched", | |
| first_touch_source: source, | |
| first_touch_medium: medium, | |
| first_touch_campaign: campaign, | |
| first_touch_click_id: clickId, | |
| }); | |
| /* --- 4. Debug log ---------------------------------------------- */ | |
| if (window.location.search.indexOf("dnm_debug=1") !== -1) { | |
| console.group("[DNM] Purchase source enrichment"); | |
| console.log("source :", source); | |
| console.log("medium :", medium); | |
| console.log("campaign :", campaign); | |
| console.log("click_id :", clickId); | |
| console.groupEnd(); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment