如何將Google GA的JS追蹤代碼(Script)貼到多個不同頁面中?

#20190719更新網站使用頁框的解決方案
#20190806更新analytics.js方法

1.靜態網頁(HTML)

將GA的追蹤程式存成獨立的.js檔案(analytics.js),在各個html檔案中加入以下內容

<head>
<script src="analytics.js" type="text/javascript" />
</head>

analytics.js的內容(https://developers.google.com/analytics/devguides/collection/analyticsjs/)

<!-- 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 -->

2.WordPress網站

使用Insert Headers and Footers

接著再把GA代碼貼入即可

3.PHP網站

將GA代碼存成analytics.php,在透過下列程式引入即可

<?php include 'analytics.php'; ?>

4.框架式網站

將GA代碼放在網頁框架頁裡,GA即可針對不同的頁面,呈現出統計數字

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store