ndhar
ndhar

Reputation: 63

On localhost, simple Google Analytic code only works in FireFox, not IE or Chrome- undefined exception in ga.js

Edited title to indicate i was working locally.

This was part of a larger project but i have simplified to the following.

My google trackin code fires only in FireFox 28.0. The same code does not fire in IE or in Chrome.

Using Fiddler, I can confirm that GA.js is being downloaded.
I then click on the Link.
In Firefox 28.0, i see a tracking request go out.
In IE and Chrome, nothing happens. There are no errors in the Console. The code does break into ga.js and i get an exception (undefined). See call stack below code.

Am i doing something obviously wrong in the code below? I have simplified it down to the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
    Home Page
</title><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
    _gaq.push(['_trackPageview']);
</script>
</head>
<body>
    <form method="post" action="default.aspx" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEwMDUyNjYzMjhkZP9z+uGsx6oopXVpIrMvCo49qhSdX9KtN6fgRHcEez6N" />
</div>  
      <a onclick="_gaq.push(['_trackEvent', 'Login', 'Button Click', 'Main Link Click']);" href="#">Go To My Site</a>
    </form>
</body>

<script type="text/javascript">
    (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.com/ga.js";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(ga, s)
    })();
</script>
</html>

FWIW, in Chrome Developer tools, if i check the "Pause on Uncaught Exceptions", it stops at the following location in the minified ga.js code: The exception is: Paused on exception undefined The call stack is as follows:

 Zc.(anonymous function).stopPropagation (ga.js:15)
    Gd (ga.js:31)
    Uc.(anonymous function).cb (ga.js:21)
    j (ga.js:14)
    E.F (ga.js:47)
    a.(anonymous function) (ga.js:14)
    E.O (ga.js:27)
    E.push (ga.js:25)
    a.(anonymous function) (ga.js:14)
    onclick ((index):21)

Upvotes: 0

Views: 465

Answers (1)

CrayonViolent
CrayonViolent

Reputation: 32532

You figured out the answer to your problem on your own, but followed up with a question about why what you did worked:

@DalmTo Yes I did. So turns out, that for some reason I have to set the domain name. _gaq.push(['_setDomainName', 'none']); I have no idea why? Because i'm in my Dev environment and using localhost?

Okay so you didn't originally mention that you were testing this locally. Had you mentioned that, I woulda been able to point this out! Just goes to show, leave no stone unturned when explaining a problem! Anyways...

GA won't send a request if it can't properly set its cookies. In order to properly set its cookies, it must know the domain. When you are working locally (localhost) there is no domain (e.g. document.domain yields and empty string). So in order to get around this, GA allows you to set _setDomainName as 'none' so that it can know to work under that scenario.

As an alternative, you can add an entry to your OS hosts file (e.g. windows is \Windows\System32\drivers\etc\hosts) to map your localhost ip (which is usually 127.0.0.1) to a domain name, e.g.

127.0.0.1 www.test.com

And then I assume that you have a server setup locally e.g. apache or IIS or w/e, so make a virtualhost (apache) or host header (iis) for it, and you should now be able to go to e.g. http://www.test.com/page.asp instead of http://localhost/page.asp or w/e and then GA should be able to set cookies based on www.test.com without you having to use _setDomainName

Upvotes: 1

Related Questions