views:

119

answers:

2

I have implemented an XMLHttpRequest() call to a standalone html page which simply has an html, title & body tag which Google Analytics Tracking code.

I want to track when someone makes a request to display information (i.e. phone number) to try and understand what portion of people look at my directory versus obtaining a phone number to make a call.

It is very simple code:

var xhReq = new XMLHttpRequest();
xhReq.open("GET", "/registerPhoneClick.htm?id=" + id, false);
xhReq.send(null);
var serverResponse = xhReq.responseText

Yet I cannot see the "hit" in Analytics... Has anyone had this issue? All the analytics tracking code does is call:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX");
pageTracker._trackPageview();
} catch(err) {}</script>

So realistically, my XmlHTTPRequest() calls an htm file within which a script is execute to make an outbound call to Google Analytics.

Is there any reason why an XmlHTTPRequest() would not execute this?

Does an XmlHTTPRequest() still bring the code to the client before execution?

Help Please

+3  A: 

Requesting the file doesn't mean that it's automatically executed. You just get the content of the file back as a string.

For the code to be executed, it would have to be loaded as a page in the browser. You can for example use an iframe to load it.

Guffa
So you're saying that xmlhttprequest does not execute code?
sjw
@sjw: Exactly. Besides, if it would, requesting that code wouldn't work. You can only use document.write while the page is loading, or it will replace the current page instead.
Guffa
+1  A: 

To those having similar issues, obviously analytics won't track XMLHttpRequest() so to get around it, I found this post: Tracking API: Basic Configuration which explains how to simply log a pageview using javascript.

I simply added the following code to my javascrip:

var pageTracker = _gat._getTracker("UA-XXXXX-XXX");
pageTracker._trackPageview("/home/landingPage");

Much cleaner, easier and simpler than what I was originally trying to do...

sjw