Guide · Google Analytics
From Classic Analytics to Google Analytics 4
Code copied from Google’s Classic Analytics examples still contains example-petstore.com and example-commerce-host.com. It no longer measures anything, and its example links send visitors to the wrong site. This guide replaces it with Google Analytics 4.
Recognise the old code
From 2009 to 2024, Google’s guide to tracking multiple domains with Classic Analytics (ga.js) used a
pet store at example-petstore.com with a cart at example-commerce-host.com. Typical lines that were copied:
_gaq.push(['_setAccount', 'UA-12345-1']);
_gaq.push(['_setDomainName', 'example-petstore.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setCookiePath', '/myStore/']);
<a href="http://dogs.example-petstore.com/intro.html"
onclick="_gaq.push(['_link', 'http://dogs.example-petstore.com/intro.html']); return false;">See my pet store</a>
<form onSubmit="pageTracker._linkByPost('www.example-commerce-host.com/petStoreCart/begin.php');">
Older variants use pageTracker._setDomainName(".example-petstore.com") with a leading dot, or
_setDomainName("none"). Universal Analytics (analytics.js) code uses
ga('create', …, {'allowLinker': true}) and ga('linker:autoLink', […]) instead.
What it does now
- No data is collected. Universal Analytics stopped processing data on 1 July 2023 and was switched off on 1 July 2024. Classic Analytics came before it. Pages with only this code are not measured at all.
- Example links send visitors away. Links and forms with a full example address, such as
http://dogs.example-petstore.com/intro.html, take real visitors to the example domain instead of your own pages. - Links without a scheme break.
href="www.example-petstore.com"is read as a path on your own site and leads to a “not found” page. - Cookie settings fail silently. A browser refuses cookies for a domain the page is not on, so
_setDomainNamewith an example domain never worked on a real site.
Remove the old code
- Search your templates, theme and tag manager for
_gaq,pageTracker,urchin,ga.js,analytics.js,ga('create',example-petstoreandexample-commerce-host. - Delete the whole tracking block, not only the example domain.
- Remove
onclickandonsubmithandlers that call_link,_linkByPostorpageTracker. - Point example links and forms (“See my pet store”, “Continue Shopping”,
/petStoreCart/begin.php) at your own pages or your real checkout, or remove them.
Install Google Analytics 4
Create a Google Analytics 4 property with a web data stream, then add the Google tag to every page, directly or
through Google Tag Manager. Replace G-XXXXXXXXXX with the measurement ID of your stream.
<!-- Google tag (gtag.js) for Google Analytics 4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
Cross-domain measurement
Only needed when one visit spans several of your domains, for example a shop and a checkout provider. In Google Analytics 4 it is set in the admin, not in code:
- Open Admin › Data streams and select your web stream.
- Open Configure tag settings › Configure your domains.
- Add a condition for each of your own domains. Never add example-petstore.com or example-commerce-host.com.
- Save. The Google tag now adds a
_glparameter to links between these domains.
In Google Tag Manager, the same list is under Google tags › your tag › Show all. If you prefer code, set the linker domains in the Google tag:
// Only if you configure cross-domain measurement in code instead of in the admin
gtag('set', 'linker', {
'domains': ['www.example.com', 'checkout.example.net']
});
Unwanted referrals
A payment provider or other external step can show up as the source of a sale. Add such domains under Configure tag settings › List unwanted referrals (up to 50 per stream). Matching visits then continue the existing session instead of starting a new one.
Verify
- The page source no longer contains
ga.js,analytics.jsor the example domains. - Reports › Realtime shows your own visit.
- A link from one of your domains to another carries
_gl=in the destination address. - In Admin › DebugView (with Tag Assistant or debug mode on), the visit continues as one session across domains.
Sources
- Tracking Multiple Domains (Classic Analytics, 2009 snapshot) Google Code via the Wayback Machine
- Tracking Multiple Domains (Classic Analytics, 2013 snapshot) Google Developers via the Wayback Machine
- Universal Analytics has been replaced by Google Analytics 4 Google Analytics Help
- Measure activity across multiple domains Google Tag Platform
- Set up cross-domain measurement Google Analytics Help
- Identify unwanted referrals Google Analytics Help
- Google tag settings in Tag Manager Tag Manager Help
- Monitor events in DebugView Google Analytics Help