Google Analytics Site Speed with siteSpeedSampleRa

2019-05-02 07:48发布

问题:

I want to use Google Analytics to monitor site speed. I pasted the code below in my HTML page:

<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>

In order to test it in my localhost, I use analytics_debug.js instead of analytics.js, UA-xxxxxxxx-x as the real id, and domain.com as the real domain name. siteSpeedSampleRate is used to set the sample rate (see this link).

However, when my HTML page is visited, this JavaScript error happens when calling 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

回答1:

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.



回答2:

You should use the following syntax:

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

Pass all option parameters within the {} and you should be fine.

To test it on local host set the cookieDomain to none

Read more about this here: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#customizeTracker



回答3:

Actually this is the solution:

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

Please, adjust your values accordingly.