search

Home  >  Q&A  >  body text

javascript - google分析 封装代码可行性

跟踪代码示例

新跟踪代码ga.js

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3397001-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

把代码放置在网站的每一页(静态网页),</body>标记之前

为了减少代码的复杂度

就把 这些代码封装在了一个 函数里

/* 谷歌统计代码 */
googleCount:function(){
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");    
    loadjscssfile(gaJsHost+'google-analytics.com/ga.js','js');
    try { var pageTracker = _gat._getTracker("UA-436090-1"); pageTracker._trackPageview(); } catch (err) { };
    
    (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', '//www.google-analytics.com/analytics.js', 'ga');

    ga('create', 'UA-436090-2', 'auto');ga('require', 'displayfeatures');
    ga('send', 'pageview');
},

然后在需要的页面调用一下这个函数就可以了 ,

那么问题来了 这么做 对 统计和 跟踪有影响吗??

求大神赐教 ..

大家讲道理大家讲道理2777 days ago692

reply all(0)I'll reply

No reply
  • Cancelreply