Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery live + Disqus / Google Analytics

I'm using the following function to overload my website url links with Ajax:

$(document).ready(function() {
    $('.insite').live("click", function(ev) {
        if ( history.pushState ) history.pushState( {}, document.title, $(this).attr('href'));
        ev.preventDefault();
        $('#content').fadeOut().load($(this).attr('href')+' #content', function() {
                $(this).fadeIn();
            });
    });
});

I would like to know if it's possible to integrate Google Analytics tracking and Disqus loading into the function. This is the code I have tried to load disqus but it loads comments from another websites for some reasons:

window.disqus_no_style = true;
$.getScript("http://disqus.com/forums/mnml/embed.js")

Thanks

like image 224
Roch Avatar asked Apr 29 '11 13:04

Roch


1 Answers

You can just put the Google Analytics function directly in the event call, placing the new virtual URL in the second parameter.

$(document).ready(function() {
    $('.insite').live("click", function(ev) {
    var href = $(this).attr('href');
        if ( history.pushState ) history.pushState( {}, document.title, href);
        ev.preventDefault();
        $('#content').fadeOut().load(href+' #content', function() {
                $(this).fadeIn();
                _gaq.push(['_trackPageview', href ]);
            });
    });
});

(I've edited the function to cache the href within the event, since its inefficient to spin up 3 (now 4) separate jQuery objects for a value that will be fixed for each call. )

like image 72
Yahel Avatar answered Oct 30 '22 11:10

Yahel