: Google Analytics recording event based on title attribute I am declaring: var title = (typeof(el.attr('title')) != 'undefined' ) ? el.attr('title') :""; and then have the following:
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;
}
}
}
});
});
}
More posts by @Twilah146
1 Comments
Sorted by latest first Latest Oldest Best
I have no way of testing this code without creating a jsfiddle or having you show me the site, but from a high-level, if a title does not exist, meaning it would be given "" as a value:
var title = (typeof(el.attr('title')) != 'undefined' ) ? el.attr('title') :"";
Then, you do your else if, and attempt to match title using "" as the value of title, it probably won't work, correct?
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;
}
Thoughts?
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.