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

javascript - XMLHttpRequest() and Google Analytics Tracking - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked May 1, 2010 at 3:15 sjwsjw 2,6335 gold badges22 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

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

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

发布评论

评论列表(0)

  1. 暂无评论