Here's something I've developed for myself that I think will be useful for others. It would be great if it could be included in the module.
I'm still perfecting this but works fine for now and I've been using it in production in one site so far without issues.
This will add the ability, while creating/editing content, to specify Google Analytics event tracking to the links that belong to the content type.
Workflow:
1. When configuring the component in the content type there is a checkbox that allows the user to decide if analytics events are permitted or not
2. When adding the content, three textboxes are added that allow the user to specify the , and of the event. The and field were excluded because they are optional, usually remain unchanged and simplify the interface. They can be added easily without problem.
3. In the frontend jQuery events are added to actually send the event information onclick. The template developer must include the tags in the link through
The Javascript code for the frontend may be included in the module itself. This will be up to the main developer. I my company I have an internal Drupal distribution and personally, in every module I remove all JS and CSS files.
Here's the jQuery I use in my projects to do the event tracking
(function ($) {
$(document).on("click", "a[data-ga-category]", function(event) {
event.preventDefault();
var $this = $(this);
var category = $this.data("ga-category") || "";
var action = $this.data("ga-action") || "";
var label = $this.data("ga-label") || "";
var value = $this.data("ga-value") || 0;
var nonInteraction = $this.data("ga-noninteraction") || false;
var timeout = $this.data("ga-timeout") || 500;
if(category.length > 0 && action.length > 0 && label.length > 0) {
try {
_gaq.push(["_trackEvent", category, action, label, value, nonInteraction]);
setTimeout('document.location = "' + $this.attr("href") + '"', timeout);
} catch(e) {
console.log(e.message);
}
}
});
})(jQuery);