5

我想使用谷歌分析来监控网站速度。我将以下代码粘贴到我的 HTML 页面中:

<script type="text/javascript">
    (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_debug.js','ga');

    ga('create', 'UA-xxxxxxxx-x', 'domain.com', {'siteSpeedSampleRate': 100});
    ga('send', 'pageview');
</script>

为了在我的 localhost 中测试它,我使用analytics_debug.js代替analytics.js,UA-xxxxxxxx-x作为真实 id 和domain.com真实域名。 siteSpeedSampleRate用于设置采样率(请参阅此链接)。

但是,当访问我的 HTML 页面时,调用时会发生此 JavaScript 错误ga('send', 'pageview')

Missing required field '"timingCategory"' for hit of type '"timing"' analytics_debug.js:8
log analytics_debug.js:8
O analytics_debug.js:7
b analytics_debug.js:15
Ec analytics_debug.js:15
nc analytics_debug.js:21
gc.(anonymous function).execute analytics_debug.js:19
ad.(anonymous function).send analytics_debug.js:45
Y.b.(anonymous function) analytics_debug.js:32
(anonymous function) analytics_debug.js:46
Sc analytics_debug.js:33
(anonymous function) analytics_debug.js:33
Missing required field '"timingVar"' for hit of type '"timing"' analytics_debug.js:8
log analytics_debug.js:8
O analytics_debug.js:7
b analytics_debug.js:15
Ec analytics_debug.js:15
nc analytics_debug.js:21
gc.(anonymous function).execute analytics_debug.js:19
ad.(anonymous function).send analytics_debug.js:45
Y.b.(anonymous function) analytics_debug.js:32
(anonymous function) analytics_debug.js:46
Sc analytics_debug.js:33
(anonymous function) analytics_debug.js:33
4

3 回答 3

3

As of 2014.01.02 this error message is standard in analytics_debug.js when it is sending a pagespeed timing beacon. Your code is fine / working corectly.

于 2014-01-02T17:41:18.777 回答
1

您应该使用以下语法:

ga('create', 'UA-XXXXXXX-Y', {'cookieDomain': 'example.com', 'siteSpeedSampleRate': 100});

传递所有选项参数{},你应该没问题。

要在本地主机上对其进行测试,请将其设置cookieDomainnone

在此处阅读有关此内容的更多信息:https ://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#customizeTracker

于 2013-12-18T13:52:15.043 回答
-1

实际上这是解决方案:

ga('send', {'hitType': 'timing', 
            'timingCategory': 'Category', 
            'timingVar': 'Load', 
            'timingValue': 120, 
            'timingLabel': 'Time to Load'});

请相应地调整您的值。

于 2014-06-23T19:18:26.533 回答