Checking completion (asynchronously) Google Analytics tracking my page

How do I check that an asynchronous Google Analytics snippet has finished tracking a webpage before I (for example) redirect a user?

if(gaFinished())
{
    ....

      

what should gaFinished()

be?

Thanks in advance.

Alternatively, I wouldn't need to do anything if I was using Google's synchronous code, but is there any modern synchronous script (because I can't find it). I would rather do this, perhaps.

+3


source to share


3 answers


The _gaq array is used as a queue, i.e. after the action has been performed, it is removed from the array. In addition to removing an element from the array, it looks like the entire array is removed after all actions have been processed. With that in mind, the following code should do what you are looking for:



if (typeof _gaq.length == "undefined" || _gaq.length == 0)
{
    ....
}

      

0


source


In the end, I used the (old) synchronous snippet provided and maintained by google:

<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-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}
</script>

      



This will allow scripts to run further down the page after tracking is complete.

0


source


You can check if window['_gat']

.

EDIT

Like Randy's solution, window['_gat']

it only reports when the tracking code has been initialized ...

Another way is _gaq._getAsyncTracker()

, which only exists after the call to __utm.gif. Therefore, you can do this:

var gaFinished = function(){
    try {
        var pageTracker = _gaq._getAsyncTracker();
        reutrn true;
    } catch(err) {
        return false;
    }
};

      

0


source







All Articles