Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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();
share|improve this answer
    
Thanks for the link, perfect! I know I can grab my title via my h2.title tag on single post pages without problems. But when adding the post title to the vent tracking in category, archive pages I get a bit lost. I think I need to use a combination of parents() and prevAll(). I'll try it out and see what happens. Thanks again! –  dkmojo Nov 30 '11 at 16:40
    
Here is the code that worked for me getting the post title: codevar post_title = $(this).parent().parent().siblings('h2.title').text();code –  dkmojo Nov 30 '11 at 16:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.