开发者

Google Analytics - Setting Custom Variable Failed!

i am attempting to set a custom variable on a contact form that tracks when a form submission is success. However, in my google analytics page the custom variable isn't showing up in Visitor -> Custom Variable. I understand it takes a few hours for it to refresh but it has been a week. So, my basic assumption is that it didn't work. I am using async _gaq.push() to achieve this. Basically, i set custom variable after my ajax function call to submitting the contact form and within the callback function if success i set the following:

_gaq.push(['_setCustomVar', 1, 'contact', 'success', 3]);

This is my google analytic code in the head.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-x']); //my google analytic api key
_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.com/ga.js';
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

Actually a better question is, does a _gaq.push(['_trackPageview']); need to be called after a _gaq.push(['_setCustomVar', 1, 'contact', 'success', 3]); for it to "register" the _setCustomVar call, and is it ok for me to call _gaq.push(['_trackPageview']); more than once after DOM is loade开发者_如何学编程d via javascript (weather it be off an event or callback ajax function). Thanks guys.


setCustomVar has to be set prior to a request sent to Google. It doesn't set it's own request; it merely queues up the variable to be sent anytime within that page session.

If it's set after the pageview, and the page changes before any requests are sent to Google Analytics (like making a pageview or event track call), then Google will never receive the data about your custom variable.

So, if you can't set the variable before the initial pageview, you need to either generate a 2nd pageview. Or, you can make a google analytics event tracking call, which can also carry your custom variable to Google Analytics.

So, what you need to do is set another call directly after your setCustomVar to do something like this:

_gaq.push(['_setCustomVar', 1, 'contact', 'success', 3]);
_gaq.push(['_trackEvent', 'contact', 'success',"", 0, true]);


Have you tried simply _setVar? We are still using an old method of tracking, but this is the code we use:

try {
var pageTracker = _gat._getTracker("UA-xxxxxxx-x");
pageTracker._setVar('custom value goes here');
pageTracker._trackPageview();
} catch(err) {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜