To use Google Analytics, you put some JavaScript code in your web page which will make an asynchronous request to Google when the page loads.
From what I have read, this shouldn't block or slow down page load times if you include it directly before the end of your HTML Body. To verify this, I want to make the request after some period of time. The user should be able to log into my site regardless of the time it takes for the request to Google or if it comes back at all (the tracking code is on the login page).
There is a 'pageTracker._trackPageview()' function call in the Google Tracking code. Is this where the request is sent to Google?
If so, should I just do:
window.setTimeout(pageTracker._trackPageview(), 5000);
any help is appreciated, especially if you have worked with Google Analytics and have this same problem.
window.setTimeout(pageTracker._trackPageview(), 5000); will call the code immediately - what you want is
window.setTimeout(function() { pageTracker._trackPageview(); }, 5000);
This should work:
window.setTimeout(pageTracker._trackPageview, 5000);
That should do it. Put some quotes around the call:
window.setTimeout("pageTracker._trackPageview()", 5000);
You can check this using Firebug if you want to see the request go through.
Related
i found this answer in some threads here in stackoverflow https://stackoverflow.com/a/8692559, But my reputation is not enough to comment. Here is my question, does google analytic track still work if I set the Meta Refresh to 0 http://example.com/"> ?
Or should I use this method https://stackoverflow.com/a/8692588/3068292 instead of the above code?
If you use async code, you probably refresh before the codes actually fires.
Because async wait till the page is loaded to get executed. since there's no time after end-of-execution and new request, your track will be lost (at least, most of the time).
The wait sync works is that the page is not loaded until it load and fires the javascript with a response. This way, by the time it reach the refresh, all the tracking has been done.
I'm using a $.post callback in the following to redirect the page after the data has been posted. Sometimes the page redirects pretty fast, but other times it can take a good 3-5 seconds.
I'm a novice, so not sure why it's taking so long. Is it waiting for the php to end? Anything here I can change to speed this up?
On another note... encodeURIComponent doesn't seem to be working. The URL always has a space in it like ?fbname=John Doe" which I am trying to remove withencodeURIComponent`
Thanks!
FB.api('/me', function(response) {
$.post("addtodb.php",
{fbname:response.name},
function(data) {
window.location.href = "step2.php?fbname="+encodeURIComponent(response.name); //redirect after post callback
})
});
If you use're using a tool like Firebug or Google Chromes inbuilt tools you can view network activity. This will give you an idea of where the holdup is. The wait time could be due to the responsiveness of the server, or even something within your JavaScript.
Example:
I can see that on my personal website Facebook's like.php had a fairly slow transfer time..
http://i.imgur.com/pApRt.png
What I am trying to do is to redirect the user to the next page right after sending an event to Google Analytics:
_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';
The above code manages to register events because I can see them in the GA report. However I suspect that some events were lost because the browser redirects the user to the new page before the track pixel loads.
My questions are:
Does _gaq.push() block until the event has successfully reached Google's server?
If not, what is the best way to make achieve what I need?
Thanks!
Google has a support page that says you might want to add a delay after calling _gac.push, however they claim that this is to give the Google Analytics JavaScript a chance to load. They don't say that it's to ensure the request is sent.
First, delay the outbound click by a fraction of a second.
This delay will hardly be noticeable by the user, but it will provide the browser more time load the tracking code. Without this method, it's possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded.
I haven't checked #pixelfreak's claim of handling onbeforeunload, but it seems that's what they do.
This is my observation based on some quick research. That function is part of Google Analytic's asynchronous API, so it should not block anything, otherwise, it's not async. :)
Digging through the obfuscated ga.js, you can kinda see that the tracking is probably called onbeforeunload, which fires when you leave a page.
It really doesn't matter if the tracking pixel loads completely, it's a fire & forget. As long as the request is initiated and reaches Google's server, it'll get tracked. The response (in this case, the pixel image) is a by-product.
Maybe you can try this method, but I have not tried
_gaq.push(['_trackEvent', 'xxx' ...]);
_gaq.push(function(){location.reload()});
Just use 1 sec delay. Like this:
_gaq.push(['_trackEvent', 'xxx' ...]);
setTimeout(function(){window.location = 'some_url'}, 1000);
Problem
I am not able to refresh the page with window.location.reload() which is used inside the success call made to yahoo.
Any hints how it can be fixed.
The whole of the code is working fine it is making call to cse server getting contents from there saving on yahoo. but i have to manually refresh the page to bring the contents. I want it to be automatic so I used window.location.reload() but thats not working. Any suggestions how it can be done. The function below is actually a function for a button.
That's the problem, right there.
If your script is running from the CSE server's domain, you cannot send data to the yahoo server. This is javascript's main limitations. Likewise, if running off of the yahoo domain, you can send data to it, but cannot send data to the CSE server, unless it is part of the yahoo domain.
Would work:
Get data from blahblahblah.yahoo.com, then send data to somedomain.yahoo.com
Would not work:
Get data from blahblahblah.somesite.com and send data to somedomain.yahoo.com
Main point, if you're getting data from "csce.unl.edu" and running off of that domain (aka running your script in a browser window from that domain), you can only send data to a site that ends with ".unl.edu". So you can send or receive from "test.unl.edu", but not some yahoo site.
A solution:
Host a proxy script on some webserver, or write all of your code in PHP. Here is two great references on what a proxy script is, and the second link actually provides one for you:
Link 1
Link 2
Any more help needed, you can let me know, I had to set one up myself, on my server, and I can help you out if you run into problems.
did you tried:
window.location = window.location;
My site uses the Google Maps API. In situations where the connection to Google is slow and the map can't be rendered in a reasonable time, I'd like a Javascript callback method to be called such that I can display a useful message to the user rather than have a 'loading...' message constantly displayed.
Is this achievable?
Maybe you could have a sleep function that would check if the page has loaded yet, and after a certain time you take some sort of action.
See this posting for a situation similar to yours
setTimeout might be useful too.
so, you would have:
setTimeout((function()
{ /* test if the page is loaded,
if so, call another function
or set a flag to get out*/
}),2000); //set for 2 seconds