adding google event tracking to links in posts

I am trying to add outbound link tracking for links within my posts. But only for links that do not have an img as a child. My code is below but does not add my class to any links in my posts:

jQuery(document).ready(function(){
    //outbound post links
    //var outbound_post_name = jQuery(this).find('h2.title').text();
    var post_link = jQuery('.post .entry').find('a');
        if( jQuery(post_link).has('img') ){
            //do not add class or tracking if the link has an img
        }
        else{
            jQuery(post_link).addClass('outbound-link');
            jQuery(post_link).click(function(){
                _gaq.push(['_trackEvent', 'Outbound Link', 'Click', outbound_post_name + 'Post Link', '', false]);
            });
        }
});

I also need to get the post title for my label but am unsure of how to get it set as “outbound_post_name”.

Read More

If I remove the “if” statement all links get the class “outbound-link” What am I missing?

Thanks!

Related posts

Leave a Reply

1 comment