I see that you use jQuery on your website. Despite being unable to write attributes onto the links, you can include this snippet of JavaScript which will log a Google Analytics event before sending the user to the link.
function logeventga(category, action, label, value, callback, nonAction){
var event = {
'eventCategory': category,
'eventAction': action
};
if (label) event['eventLabel'] = label;
if (value) event['eventValue'] = value;
if (callback) event['hitCallback'] = callback;
if (nonAction) event['nonInteraction'] = 1;
if (typeof ga == 'function' && ga.hasOwnProperty('loaded') && ga.loaded === true) {
ga('send', 'event', event);
} else if (callback) {
callback.call();
}
}
$(document).ready(function(){
$('#div-id a').click(){
var href = $(this).attr('href');
logeventga('external-link','click',href,null,function(){
document.location=href;
});
return false;
}
});
Some notes on how it works:
- You have to wait until the event logging is done before allowing the user to leave the page, otherwise the event may not get logged. Google Analytics has a callback function that gets triggered when the event is logged. You can use this callback function to take the user to the new page after the event is logged by setting
document.href
. The return false
from the click event handler prevents the user from being sent off immediately after the click.
- Some users block Google Analytics with Adblock or similar software. In those cases, you don't want your JavaScript to fail and the user be unable to use the link. The
logeventga
detects cases of Google Analytics not being loaded and triggers the callback function right away.
- You will have to replace
$('#div-id a')
with the appropriate jQuery selector that actually finds your links. If there are in a div with an id, you can just replace div-id
with the actual id.
- My
logeventga
function assumes that you use the new Universal analytics that uses the ga
function. If you are using the old style analytics calls with _gaq
you will have to change the code a little bit.
onclick
attribute?