最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "_gaq is undefined” Google Analytics Error in IE7 and IE8 - Stack Overflow

programmeradmin2浏览0评论

I am geting _gaq is undefined error in IE7 and IE8

My Script is bellow

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX']);
    _gaq.push(['_setDomainName', 'yyy']);
    _gaq.push(['_trackPageview']); 
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics/ga.js?';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Can I assign default value for _gaq? Refer this link

I am geting _gaq is undefined error in IE7 and IE8

My Script is bellow

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX']);
    _gaq.push(['_setDomainName', 'yyy.']);
    _gaq.push(['_trackPageview']); 
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics./ga.js?';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Can I assign default value for _gaq? Refer this link

Share Improve this question edited May 23, 2017 at 11:48 CommunityBot 11 silver badge asked Nov 15, 2012 at 6:15 Dharmalingam ArumugamDharmalingam Arumugam 40711 silver badges25 bronze badges 4
  • 1 that what's happening (setting a default) with var _gaq = _gaq || []; either it's taking current _gaq or it's creating an empty array. there must be something else wrong with your code. can you post some more? – Asken Commented Nov 15, 2012 at 10:29
  • At which line you get _gaq is undefined? var _gaq = _gaq || []; is supposed to prevent that. – vinczemarton Commented Nov 15, 2012 at 10:30
  • 1 Agree with above ments, the error isn't ing from this code, more likely from some other that references it. Is this script defined in the <head> section of your HTML? In older versions of GA you would put the script just before the closing </body> tag. If it's still there, that's likely the issue. As long as the script tag has been read and processed before the _gaq variable is referenced, you should not get this error (even from IE7 and IE8!!). – Tom Harrison Commented Nov 16, 2012 at 3:50
  • @tharrison: I added the script before </body> – Dharmalingam Arumugam Commented Nov 16, 2012 at 13:01
Add a ment  | 

1 Answer 1

Reset to default 7

Based on your reply to my ment, the answer to this question is

Google Analytics moved to a new, more modern way of handling events several years ago, allowing browsers to load a plete page without having to wait for a response from the GA servers. Whereas in the past, you were guided to put the <script> snippet provided by google near the end of the page (just before the closing body tag), now you should instead include the tag somewhere in the <head> section of your HTML.

The new script works by defining a javascript variable _gaq as an array, that is treated like a queue. Pageviews, events and other stuff you want to track is pushed onto the array, which is effectively instant. When there is time, the GA code will check the array for contents and pop them off, processing the requests to the GA server until the array is empty.

A side effect of the _gaq variable is that the script tag must be in the HTML before it is referenced, or else you'll get the JS error _gaq is undefined.

For example as documented here in the Google Analytics event tracking guide you might want to know when a user plays a video on your site like this:

<a href="#" onClick="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);">Play</a>

Notice the reference to the variable _gaq here. If not defined already, you'll get the error.

Short answer: put the async javascript snippet in the <head> of your HTML.

发布评论

评论列表(0)

  1. 暂无评论