Reputation: 571
I want to push two events in a function.My code goes somewhat like this-
function track(){
_gaq.push(['_trackEvent', 'Search', 'Search Attempt','User searched for a username']);
//few more lines of code before an ajax call
$.ajax({
url : '/xyz.json'
,type : 'POST'
,data : "uname="+uname
,dataType : 'json'
,success : function(data) {
if(data.success) {
_gaq.push(['_trackEvent', 'Search', 'Search Success','User found user with username searched']);
window.location.href='/thread/'+uname;
}
});
}
The problem that I am facing is that, the first event gets tracked but the other one with the if condition after the json call does not gets tracked.However "window.location.href" redirects me to the new url.
Also there is a case when I push this-
_gaq.push(['_trackEvent', 'Search', 'Search Attempt','User searched for a username'],
['_trackEvent', 'Search', 'Search Success','User found user with username searched']);
In this case, only first event gets tracked. (Both the cases work properly sometimes, where could be the problem then?
Upvotes: 0
Views: 122
Reputation: 3317
SainiAnkit,
I would suggest using hitCallback function instead of "hard" delay, it's a bit more effective (once the request to GA is successful, it will just continue whatever the browser was up to) and won't cause that much delay.
All you need to know about hit callback concept can be found in this article.
Hit Callback is supported in "Classic" Analytics as well -- see this related answer with details and code examples.
Upvotes: 1
Reputation: 2360
You should to add some delay before redirecting (to allow time for the tracking), because _gaq.push function add events to the queue, tracking does not occur immediately. Just add timeout for your redirect, something like this:
if(data.success) {
_gaq.push(['_trackEvent', 'Search', 'Search Success','User found user with username searched']);
setTimeout(function(){
window.location.href='/thread/'+uname;
},300);
}
Upvotes: 2