Skip to content Skip to sidebar Skip to footer

How To Add An Onclick Function To Wordpress Menu Item (For Google Cross Domain Tracking)

I am setting up google analytics cross domain tracking on my website. I know I need to change each link to include: onclick='_gaq.push(['_link', 'http://mywebsite.com/']); return f

Solution 1:

For example:

<script>

$(document).ready(function() {
    $('#nav-main a ').click(function() {
    var p = $(this).attr('href');
    if (p.search(/.+mywebsite/) == -1){ 
        _gaq.push(['_link', 'http://mywebsite.com/']); return false;
    }       
    });
});

</script>

You don't need the "delegate" - that's necessary only when new links are created dynamically (links created via javascript have no events attached, delegate takes care of that). Replace nav-main with the id of your navigation an mywebsite with your domain.

The p.search bit makes sure that _link is only attached to the links that do not point to the domain the script runs on (since you need this only for links that go to your other domain).


Post a Comment for "How To Add An Onclick Function To Wordpress Menu Item (For Google Cross Domain Tracking)"