大网数据广告活动
网站运营与故障问答

Can I configure Google Analytics to use the canonical URL?

· 52CE 我爱测

The website includes pages like example.com/category1/product1 and example.com/category2/product1 and example.com/category3/product1 Here, the example.com/category1/product1 is the canonical page; and all other pages are duplicat…

问题原文

原帖链接 · Bhargav Joshi · CC BY-SA 4.0

The website includes pages like

  • example.com/category1/product1 and
  • example.com/category2/product1 and
  • example.com/category3/product1

Here, the example.com/category1/product1 is the canonical page; and all other pages are duplicate pages.

The duplicate pages are marked with proper canonical tags using <link rel="canonical" href="example.com/category1/product1"/>

Although, all of these duplicate pages contain the same Google Analytics Code. So are these three pages considered as individual pages in Google Analytics? If yes, then how to configure Google Analytics so that traffic to the all three URLs are consolidated in one single canonical URL?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

You can modify your Google Analytics tracking snippet to use the canonical URL that you specify. Your normal GA tracking snippet should look something like this:

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');    
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

Google has instructions for modifying the ga('send', 'pageview'); line to specify the URL of the page getting tracked. Their example is ga('send', 'pageview', location.pathname);

This StackOverflow question has the JavaScript to get the canonical URL: document.querySelector("link[rel='canonical']").getAttribute("href");

We can put that together to use the location.pathname unless the canonical exists. We can also use the regular expression /https?:\/\/[^\/]+/i to strip the protocol and host name off the canonical URL.

var path=location.pathname
var canonical=document.querySelector("link[rel='canonical']")
if (canonical) path=canonical.getAttribute('href').replace(/https?:\/\/[^\/]+/i,'')
ga('send', 'pageview', path);

The only other wrinkle is that your canonical needs to be defined before your GA snippet. Otherwise it won't be in the page when the JS looks for it. your final canonical and snippet should look something like this in the <head> section of your pages:

<link rel=canonical href="http://example.com/hello-world">
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');    
ga('create', 'UA-XXXXX-Y', 'auto');
var path=location.pathname
var canonical=document.querySelector("link[rel='canonical']")
if (canonical) path=canonical.getAttribute('href').replace(/https?:\/\/[^\/]+/i,'')
ga('send', 'pageview', path);
</script>
<!-- End Google Analytics -->

This doesn't check to make sure the canonical is on another page on your own site. If you use some external canonical URLs to other sites, you would have to add some more checks to the code.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。