Recording AJAX Navigation w/ New Google Analytics

Hey gang, I saw this script when searching for a way to do monitor AJAX navigation with the newest codes for Google Analytics.


$.ajax({
    ...
    success: function (data) {
        ga('send','pageview','/virtual/....');
    },
    ...
});

I keep my Analytics code in my footer, and was hoping to see if there was a way to ensure that the ga(‘create’, ‘UA-xxxxxxx-1’, ‘mysite.com’); has already been run before trying to call ga before it exists. Is there a way to check existance (or ensure ga ‘create’ is complete) before doing the ga “send pageview” function as shown above?

I am just afraid that some people might try to change the page so quick that it will bug out my AJAX as it attempts to run the GA send function before the GA create function is complete.

All feedback appreciated!

Cheers!
Ryan

This is what I’ve decided to run with… let me know if you see issue. Using jquery:


//inside ajax success function
if($.isFunction(ga)) {
								ga('send','pageview','/this_is_page.html');
							}

LMK if you can think of problems.

Cheers!
Ryan