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”.

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

Thanks!

1 Answer
1

Have a look at this article -> http://www.optimum7.com/internet-marketing/web-development/dynamic-google-analytics-event-tracking-for-wordpress.html

Also to get the post title of the particular page. You could use the following jQuery ( Assuming you post title is wrapped in tag ).

outbound_post_name = jQuery('h1').html();

Leave a Comment