Google Analytics 强制使用 HTTPS 以防止 307 内部重定向
Google Analytics force HTTPS to prevent 307 Internal Redirect
当 Google Analytics 从 HTTP 页面发送数据时,它以 HTTP 请求开始,如下所示:
http://www.google-analytics.com/collect?payload-data-goes-here
但是由于 HTTP Strict Transport Security (HSTS),这会导致 307 状态代码(内部重定向),并且此重定向是完全相同的 https 版本 URL。
如何强制 Google Analytics 从一个 http 页面只发送一个 https 请求?
解决方案是使用ForceSSL
。这会强制 Google Analytics 始终 通过 https 发送数据。
analytics.js
ga('set', 'forceSSL', true);
By default, tracking beacons sent from https pages will be sent using https while beacons sent from http pages will be sent using http. Setting forceSSL to true will force http pages to also send all beacons using https.
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#forceSSL
示例:
<!-- 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('set', 'forceSSL', true); // <---------------------------- add this!
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
ga.js(旧版)
_gaq.push(['_gat._forceSSL']);
Configures Google Analytics to send all hits using SSL, even from insecure (HTTP) pages.
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_forcessl
示例(异步):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_gat._forceSSL']); // <------------------------ add this!
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
示例(传统的 .js 片段):
var pageTracker = _gat._getTracker("UA-XXXXX-X");
_gat._forceSSL(); // <---------------------------------------- add this!
pageTracker._trackPageview();
当 Google Analytics 从 HTTP 页面发送数据时,它以 HTTP 请求开始,如下所示:
http://www.google-analytics.com/collect?payload-data-goes-here
但是由于 HTTP Strict Transport Security (HSTS),这会导致 307 状态代码(内部重定向),并且此重定向是完全相同的 https 版本 URL。
如何强制 Google Analytics 从一个 http 页面只发送一个 https 请求?
解决方案是使用ForceSSL
。这会强制 Google Analytics 始终 通过 https 发送数据。
analytics.js
ga('set', 'forceSSL', true);
By default, tracking beacons sent from https pages will be sent using https while beacons sent from http pages will be sent using http. Setting forceSSL to true will force http pages to also send all beacons using https.
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#forceSSL
示例:
<!-- 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('set', 'forceSSL', true); // <---------------------------- add this!
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
ga.js(旧版)
_gaq.push(['_gat._forceSSL']);
Configures Google Analytics to send all hits using SSL, even from insecure (HTTP) pages.
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_forcessl
示例(异步):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_gat._forceSSL']); // <------------------------ add this!
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
示例(传统的 .js 片段):
var pageTracker = _gat._getTracker("UA-XXXXX-X");
_gat._forceSSL(); // <---------------------------------------- add this!
pageTracker._trackPageview();