如何为10个转发至同一主机的域名配置通用Google Analytics代码
Hey there! Let’s get your single GA snippet working seamlessly across all 10 masked domains pointing to your host. This is totally achievable with a few simple tweaks to the standard code—no need to create separate GA properties for each domain.
Core Approach
The key is to make the GA snippet dynamically recognize the actual domain users are visiting (instead of just your host’s domain) and configure tracking to handle cross-domain sessions (if needed) while segmenting traffic by each domain.
Step 1: Modify the GA4 Snippet (Recommended, since UA is sunset)
Here’s the adjusted code that adapts to any of your 10 domains. Replace G-YOUR_TRACKING_ID with your actual GA4 ID:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR_TRACKING_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); // Grab the domain users actually see in their address bar // Handles both direct visits and iframe-based masked forwarding const currentDomain = window.top ? window.top.location.hostname : window.location.hostname; // Configure GA to use the dynamic domain gtag('config', 'G-YOUR_TRACKING_ID', { 'cookie_domain': 'auto', // Ensures cookies work reliably across all your domains 'hostname': currentDomain, // Tracks the real visited domain in GA reports // Optional: Map a custom dimension to segment traffic by domain 'custom_map': { 'dimension1': 'visited_domain' } }); // Send the domain value with every page view event gtag('event', 'page_view', { 'visited_domain': currentDomain }); </script>
Step 2: Set Up Custom Dimension (Optional but Useful)
To easily filter and analyze traffic per domain in GA4:
- Go to your GA4 property > Admin > Custom definitions > Create custom dimension
- Name it
Visited Domain, set the scope to Event (or User if you want to track user-level domain association), and set the event parameter tovisited_domain(matches the parameter in the snippet above)
Step 3: Whitelist All Domains in GA4
Make sure GA doesn’t filter out traffic from your masked domains:
- Go to your GA4 property > Admin > Data streams > Select your web stream
- Under Configure tag settings > Show all > Hostname
- Add all 10 of your masked domains to the list of allowed hostnames (e.g.,
domain1.com,domain2.net, etc.)
For Universal Analytics (UA) Users (Legacy)
If you’re still using UA (note: UA stops processing data in July 2024), use this adjusted snippet instead:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-YOUR_TRACKING_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); const currentDomain = window.top ? window.top.location.hostname : window.location.hostname; gtag('config', 'UA-YOUR_TRACKING_ID', { 'cookie_domain': 'auto', 'hostname': currentDomain, 'custom_map': { 'dimension1': 'visited_domain' } }); gtag('event', 'page_view', { 'visited_domain': currentDomain }); </script>
For UA, add your domains to the view’s Filter settings (create an include filter for hostnames matching your 10 domains) and set up the custom dimension similarly in the UA admin panel.
Key Notes
- If your masked forwarding uses iframe embedding, the
window.top.location.hostnameline ensures we capture the domain users see, not your host’s underlying domain. - The
cookie_domain: 'auto'setting lets GA manage cookies correctly across all your domains, so returning users are tracked consistently regardless of which domain they visit.
内容的提问来源于stack exchange,提问作者Faseeh Haris

