Google Analytics 默认代码处理 http 和 https?

Google Analytics default code handle http and https?

好吧,我有一个 运行 在 http 的域,但是当用户转到支付页面时它 运行 在 https,那么我需要同时跟踪 http 和 https。

我是否需要更改此默认曲目代码中的某些内容?

<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-76202182-1', 'auto');
  ga('send', 'pageview');

</script>

谢谢。

您需要将 url 更改为相对协议 URL。

所以从https://www.google-analytics.com/analytics.js改为//www.google-analytics.com/analytics.js

<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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-76202182-1', 'auto');
  ga('send', 'pageview');

</script>

接受的答案是错误的。标准代码在 https 和 http 页面上都可以正常工作。

答案中更改的 url 甚至没有提到跟踪调用,这是从中加载跟踪库的 url。以前是ssl给服务器增加了额外的负载,所以Google曾经有相对的urls只有在必要的时候才会切换到ssl,但那都是过去的事了,所以现在他们有了ssl url 就在默认代码中。

使用标准代码就可以了。您甚至可以通过 using the force ssl option 独立于页面协议强制使用 ssl,这不会影响页面跟踪。