views:

89

answers:

1

Hello folks!

You'll have to forgive me on this one. I understand that the issue is probably very simple, but I'm not a JS coder and am only just starting to get to grips with jQuery.

So I have a jQuery Ajax call in $(document).ready:

$('#newesttab').click(function() {

        $('li').removeClass('selectedtab');
        $('#newesttab').addClass('selectedtab');
        $('#col2').removeClass('featuredcol topratedcol').addClass('newestcol');

        $("#ajaxresults").fadeOut("slow", function() {
            $.ajax({
                url: "<?php echo site_url('code/newest'); ?>",
                success: function(msg) {
                    $('#ajaxresults').html(msg);
                    $("#ajaxresults").fadeIn("fast");
                    <?php if ($_SERVER['HTTP_HOST'] != 'localhost:8888') { ?>pageTracker._trackPageview('/code/newest/ajax');<?php } ?>
                }
            })
        });
    });

So basically when #newesttab is clicked, it does a bit of mumbo-jumbo with some Clases & IDs, fades stuff out, then loads some data from an Ajax CodeIgniter function. On success, the div #ajaxresults is filled with the data, fades in - and finally, you'll see this line:

<?php if ($_SERVER['HTTP_HOST'] != 'localhost:8888') { ?>
   pageTracker._trackPageview('/code/newest/ajax');
<?php } ?>

All that does is check if I'm not on MAMP localhost so as to not get funky data in Analytics. If not, I call Google Analytic's pageTracker thingy to track the URL code/newest/ajax so it appears in my Analytics despite the page being called via Ajax. That's correct, right?

Well Chrome's console shows me this error: Uncaught ReferenceError: pageTracker is not defined

The part I don't understand, is that because this code is in (document).ready surely it should only fire when the entire document has loaded? And that means, therefore, that the Google Analytics Asynchronous snippet has loaded before my </body> tag (yep, it's definitely there)... I guess I'm misunerstanding something, right?

And before anybody asks, yes, I've tried this not on localhost ;)

Thanks!

Jack

A: 

Turns out I was calling pageTracker when with the Async code you need to call gaq_push.

Jack Webb-Heller