使用siteSpeedSampleRate设置的Google Analytics网站速度存在JavaScript错误

时间:2013-09-23 09:52:45

标签: javascript google-analytics

我想使用Google Analytics来监控网站速度。我在下面的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.jsUA-xxxxxxxx-x作为真实ID,domain.com作为真实域名。 siteSpeedSampleRate用于设置采样率(请参阅this link)。

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

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

3 个答案:

答案 0 :(得分:3)

自2014.01.02起,此错误消息在analytics_debug.js发送一个pagespeed计时信标时是标准的。你的代码很好/正常工作。

答案 1 :(得分:1)

您应该使用以下语法:

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

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

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

在此处详细了解:https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#customizeTracker

答案 2 :(得分:-1)

实际上这是解决方案:

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

请相应调整您的值。

相关问题