How to track Google analytic goals using jQuery ajax

Discussion in 'jQuery' started by rags02, Oct 13, 2009.

  1. #1
    I am trying to have GA track my goals from an ajax script using jQuery (the "form" plugin). I have looked around and tried a few solutions (see below). I'm not getting any results from my goals in analytics. I have a small custom built tracker to track visitors and opt-ins, but want to integrate with GA. As you can see I am trying to utilize the _trackPageview function within the "success" action of the ajax call. Any help is appreciated. Thank you.

    PS. The code below runs on the landing page, with the javascript at the bottom before the final </body> tag.

    <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("xxxxxxxxxx");
    pageTracker._trackPageview("/lp/");
    } catch(err) {}
     
    // creates ajax 'options'
    function ajax_options(this_target) {
      var options = {
        target: this_target, 
        beforeSubmit: validSignUp, 
        success: function() {         
            var pageTracker = _gat._getTracker("xxxxxxxxxx");
            pageTracker._trackPageview("/lp/c.html");
            $('.loading_block').hide();
            $(this_target).fadeIn('slow'); 
          } 
        }
      return options;
    }
     
    // ... then function for ajax using the "options" created above
    Code (markup):
     
    rags02, Oct 13, 2009 IP