学习笔记《Google Analytics》

小丑鱼 选择了 Google Analytics 作为网站的数据监测平台,基础的功能并不复杂:

页面监测:

在页面中添加这样的代码就可以实现页面监测:

(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');

事件监测:

文档:https://developers.google.com/analytics/devguides/collection/analyticsjs/events

简单的封装:

gaEventSend = function(category, action)
{
    ga('send', 'event', category, action);
}

gaEventSendAndHref = function(category, action, url)
{
    gaEventSend(category, action);
    location.href = url;
}

页面载入时间

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);
}

你可能感兴趣的:(学习笔记《Google Analytics》)