user2300788
user2300788

Reputation: 1

inserting google 'onclick' tracking link attribute in wordpress

I am looking for a way to modify the links on my Wordpress site so it tracks all my 3rd party links.

I was able to find some help with the, how to create the correct tracking code in Google Analytics, but the second part of the process is to add some specific link attributes.

Here is the example which they suggest I replicate:

<'a href="www.blog-hosting-service.com/myBlog" onclick="_gaq.push(['_link', 'www.blog-hosting-service.com/myBlog']); return false;">View My Blog

Does anyone know where I can insert this code link attributes so I can collect the external clicks via Google Analytics?

Upvotes: 0

Views: 409

Answers (1)

WebsterDevelopine
WebsterDevelopine

Reputation: 117

JQuery would be the best way in my opinion.

// start by getting the current page path (the one you are sending to tracker)

var pathname = window.location.pathname;

// ready handler to change the links on hosts not equal to location host

$(document).ready(function() {

    $('a[href^="http://"]').filter(function() {

        return this.hostname && this.hostname !== location.hostname;

    }).click(function(e) {

        _gaq.push(['_link', pathname]);

    });
});

Upvotes: 0

Related Questions