指南 · Google Analytics
从 Classic Analytics 迁移到 Google Analytics 4
从 Google Classic Analytics 示例中复制的代码至今仍包含 example-petstore.com 和 example-commerce-host.com。这些代码已无法衡量任何数据,其中的示例链接还会将访问者引导到错误的网站。本指南将介绍如何用 Google Analytics 4 取而代之。
识别旧代码
2009 年至 2024 年间,Google 关于使用 Classic Analytics(ga.js)跟踪多个网域的指南以位于 example-petstore.com 的宠物商店及位于 example-commerce-host.com 的购物车为例。常被复制的典型代码行如下:
_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');">
更早的变体使用带前导点的 pageTracker._setDomainName(".example-petstore.com"),或
_setDomainName("none")。Universal Analytics(analytics.js)代码则改用
ga('create', …, {'allowLinker': true}) 和 ga('linker:autoLink', […])。
旧代码现在的影响
- 不会收集任何数据。Universal Analytics 已于 2023年7月1日停止处理数据,并于 2024年7月1日关停。Classic Analytics 的历史更早。只含这类代码的网页完全不会被衡量。
- 示例链接会将访问者引走。带有完整示例地址(例如
http://dogs.example-petstore.com/intro.html)的链接和表单会将真实访问者带到示例域名,而不是您自己的网页。 - 不带协议的链接会失效。
href="www.example-petstore.com"会被解析为您自己网站上的路径,最终指向“未找到”页面。 - Cookie 设置会悄然失败。浏览器会拒绝为网页所在网域以外的网域设置 Cookie,因此在真实网站上,使用示例域名的
_setDomainName从来就没有生效过。
删除旧代码
- 在您的模板、主题和代码管理工具中搜索
_gaq、pageTracker、urchin、ga.js、analytics.js、ga('create'、example-petstore和example-commerce-host。 - 删除整个跟踪代码块,而不仅仅是示例域名。
- 删除调用
_link、_linkByPost或pageTracker的onclick和onsubmit处理程序。 - 将示例链接和表单(“See my pet store”、“Continue Shopping”、
/petStoreCart/begin.php)指向您自己的网页或真实的结账页面,或将其删除。
安装 Google Analytics 4
创建一个带有网站数据流的 Google Analytics 4 媒体资源,然后直接或通过 Google Tag Manager 将 Google 代码添加到每个网页。将 G-XXXXXXXXXX 替换为您数据流的衡量 ID。
<!-- 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>
跨网域衡量
仅当一次访问跨越您的多个网域时才需要设置,例如商店和结账服务提供商。在 Google Analytics 4 中,此项在管理界面中设置,而不是在代码中:
- 打开 Admin › Data streams,然后选择您的网站数据流。
- 打开 Configure tag settings › Configure your domains。
- 为您自己的每个网域添加一个条件。切勿添加 example-petstore.com 或 example-commerce-host.com。
- 保存。此后,Google 代码会为这些网域之间的链接添加
_gl参数。
在 Google Tag Manager 中,同一列表位于 Google tags › 您的代码 › Show all 下。如果您更喜欢使用代码,可在 Google 代码中设置链接器网域:
// Only if you configure cross-domain measurement in code instead of in the admin
gtag('set', 'linker', {
'domains': ['www.example.com', 'checkout.example.net']
});
不需要的引荐
支付服务提供商或其他外部环节可能会显示为销售的来源。请将这类网域添加到 Configure tag settings › List unwanted referrals 下(每个数据流最多 50 个)。这样,匹配的访问将延续现有会话,而不会开始新的会话。
验证
- 网页源代码中不再包含
ga.js、analytics.js或示例域名。 - Reports › Realtime 显示您自己的访问。
- 从您的一个网域指向另一个网域的链接,其目标地址中带有
_gl=。 - 在 Admin › DebugView 中(开启 Tag Assistant 或调试模式),该访问跨网域后仍作为同一个会话继续。
来源
- 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