解決方案是使用ForceSSL
。這迫使谷歌分析總是通過https發送數據。
的analytics.js
ga('set', 'forceSSL', true);
默認情況下,跟蹤從HTTPS網頁則使用HTTPS,同時從HTTP網頁發送信標,它將利用HTTP發送發送發送信標。將forceSSL設置爲true將強制http頁面也使用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']);
將Google Analytics配置爲使用SSL發送所有匹配,即使是來自不安全(HTTP)頁面也是如此。
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();