We recently ran into an issue with commonly used Google Analytics plugin for CKEditor, called appropriately googletracker. While that plugin allows adding attributes to links in content, the issue we encountered is that subsequent content editing removes all inserted onclick attributes.
To address this issue, we made slight modifications googletracker plugin, available for download here and now it uses data-tracking attribute which is not removed by CKEditor.
Next we added JavaScript code to front end which replaces data-tracking to onlick and generates additional meaningful information for Google Analytics, such as link name (using link's text), category and webpage address.
Here is a sample code we used
var pageUrlArr = window.location.href.split('/'); $('a').each(function () { if ($(this).attr('data-tracking')){ var tracking; //console.log($(this)); var page; if (pageUrlArr[pageUrlArr.length-1]) { page = pageUrlArr[pageUrlArr.length-1]; }else { page = 'home'; } tracking = '"' + $(this).text() + '"'+ ' link on "' +page+ '" page.'; tracking = tracking.replace(/&/g,"_"); if ($(this).data('tracking') == '_trackEvent') { $(this) .removeAttr('data-tracking') // .attr("onclick", "_gaq.push(['_trackEvent', 'page_view', 'click', '" + tracking + "']);"); .attr("onclick", "ga('send', 'event', 'Track event from CKEditor', 'click', '" + tracking + "',{'nonInteraction': 1});"); } else { $(this) .removeAttr('data-tracking') // .attr("onclick", "_gaq.push(['_trackPageview', '" + tracking + "']);"); .attr("onclick", "ga('send', 'pageview', 'Track event from CKEditor', 'click', '" + tracking + "',{'nonInteraction': 1});"); } } });
After adding tags to CKEdtior content and custom JavaScript to front end, we can see our tracking on Google Analytics, Real Time > Events screen.