小丑魚 選擇了 Google Analytics 作為網(wǎng)站的數(shù)據(jù)監(jiān)測(cè)平臺(tái),基礎(chǔ)的功能并不復(fù)雜:
頁(yè)面監(jiān)測(cè):
在頁(yè)面中添加這樣的代碼就可以實(shí)現(xiàn)頁(yè)面監(jiān)測(cè):
(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-85667918-1', 'auto');
ga('send', 'pageview');
事件監(jiān)測(cè):
文檔:https://developers.google.com/analytics/devguides/collection/analyticsjs/events
簡(jiǎn)單的封裝:
gaEventSend = function(category, action)
{
ga('send', 'event', category, action);
}
gaEventSendAndHref = function(category, action, url)
{
gaEventSend(category, action);
location.href = url;
}
頁(yè)面載入時(shí)間
if (window.performance) {
// Gets the number of milliseconds since page load
// (and rounds the result since the value must be an integer).
var timeSincePageLoad = Math.round(performance.now());
// Sends the timing hit to Google Analytics.
ga('send', 'timing', 'JS Dependencies', 'load', timeSincePageLoad);
}