Improve cross-domain tracking and add "Always" script category
Move linker config into gtag('config') call with accept_incoming and
decorate_forms for reliable cross-domain linking. Capture _gl parameter
in sessionStorage to preserve attribution across navigation. Add
referrer-based attribution replay fallback so GA4 reports source/medium
instead of (not set) when UTMs are absent. Add "Always" script category
that executes without waiting for consent.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -65,6 +65,7 @@ var AttConsent = (function() {
|
||||
wp_set_consent('statistics', categories.analytics ? 'allow' : 'deny');
|
||||
wp_set_consent('marketing', categories.marketing ? 'allow' : 'deny');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,6 +89,28 @@ var AttConsent = (function() {
|
||||
if (a.ut) { params.campaign_term = a.ut; }
|
||||
if (a.uo) { params.campaign_content = a.uo; }
|
||||
|
||||
// If no UTMs/gclid, derive source/medium from referrer so GA4
|
||||
// doesn't report (not set). Important for cross-domain tracking
|
||||
// where the _gl param may have been lost before consent was granted.
|
||||
if (!a.us && !a.gc && a.r) {
|
||||
try {
|
||||
var ref = new URL(a.r);
|
||||
var linkerDomains = config.linker_domains || [];
|
||||
var isLinkerDomain = linkerDomains.some(function(d) {
|
||||
return ref.hostname === d || ref.hostname.endsWith('.' + d);
|
||||
});
|
||||
// Only set referral attribution if referrer is from a different
|
||||
// host and NOT a configured cross-domain linker domain (those
|
||||
// should be seamless continuations, not referrals).
|
||||
if (ref.hostname !== window.location.hostname && !isLinkerDomain) {
|
||||
params.campaign_source = ref.hostname;
|
||||
params.campaign_medium = 'referral';
|
||||
}
|
||||
} catch (e) {
|
||||
// Invalid referrer URL - skip.
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(params).length > 0) {
|
||||
gtag('set', params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user