简体   繁体   中英

Google Analytics Site Speed with siteSpeedSampleRate set has JavaScript error

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

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.

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

Actually this is the solution:

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

Please, adjust your values accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM