Google Analytics recording event based on <a> title attribute
Posted
by
rlsaj
on Pro Webmasters
See other posts from Pro Webmasters
or by rlsaj
Published on 2013-10-15T04:08:05Z
Indexed on
2013/10/21
4:11 UTC
Read the original article
Hit count: 212
google-analytics
I am declaring:
var title = (typeof(el.attr('title')) != 'undefined' ) ? el.attr('title') :"";
and then have the following:
else if (title.match(/^"Matching Content"\:/i)) {
elEv.category = "Matching Content Click";
elEv.action = "click-Matching-Content";
elEv.label = href.replace(/^https?\:\/\//i, '');
elEv.non_i = true;
elEv.loc = href;
}
However, using Google Analytics debugger this is not being recorded.
Any suggestions?
The complete function is:
if (typeof jQuery != 'undefined') {
jQuery(document).ready(function gLinkTracking($) {
var filetypes = /\.(avi|csv|dat|dmg|doc.*|exe|flv|gif|jpg|mov|mp3|mp4|msi|pdf|png|ppt.*|rar|swf|txt|wav|wma|wmv|xls.*|zip)$/i;
var baseHref = '';
if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href');
jQuery('a').on('click', function (event) {
var el = jQuery(this);
var track = true;
var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') :"";
var title = (typeof(el.attr('title')) != 'undefined' ) ? el.attr('title') :"";
var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]);
if (!href.match(/^javascript:/i)) {
var elEv = []; elEv.value=0, elEv.non_i=false;
if (href.match(/^mailto\:/i)) {
elEv.category = "Email link";
elEv.action = "click-email";
elEv.label = href.replace(/^mailto\:/i, '');
elEv.loc = href;
}
else if (title.match(/^"Matching Content"\:/i)) {
elEv.category = "Matching Content Click";
elEv.action = "click-Matching-Content";
elEv.label = href.replace(/^https?\:\/\//i, '');
elEv.non_i = true;
elEv.loc = href;
}
else if (href.match(filetypes)) {
var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
elEv.category = "File Downloaded";
elEv.action = "click-" + extension[0];
elEv.label = href.replace(/ /g,"-");
elEv.loc = baseHref + href;
}
else if (href.match(/^https?\:/i) && !isThisDomain) {
elEv.category = "External link";
elEv.action = "click-external";
elEv.label = href.replace(/^https?\:\/\//i, '');
elEv.non_i = true;
elEv.loc = href;
}
else if (href.match(/^tel\:/i)) {
elEv.category = "Telephone link";
elEv.action = "click-telephone";
elEv.label = href.replace(/^tel\:/i, '');
elEv.loc = href;
}
else track = false;
if (track) {
_gaq.push(['_trackEvent', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i]);
if ( el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') {
setTimeout(function() { location.href = elEv.loc; }, 400);
return false;
}
}
}
});
});
}
© Pro Webmasters or respective owner