Google Analytics Site Speed with siteSpeedSampleRate set has

Google Analytics Site Speed with siteSpeedSampleRate set has JavaScript error

本文关键字:siteSpeedSampleRate set has with Speed Analytics Site Google      更新时间:2023-09-26

我想使用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>

为了在我的本地主机中测试它,我使用 analytics_debug.js 而不是 analytics.jsUA-xxxxxxxx-x 作为真实 id,domain.com 作为真实域名。 siteSpeedSampleRate用于设置采样率(请参阅此链接)。

但是,当访问我的 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

截至 2014.01.02,此错误消息在发送 PageSpeed 计时信标时是 analytics_debug.js 的标准信息。您的代码很好/正常工作。

应使用以下语法:

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

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

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

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

实际上这是

解决方案:

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

请相应地调整您的值。

相关文章:
  • 没有找到相关文章