Google Analytics Events

Created on 22 October 2012, about 12 years ago
Updated 31 August 2024, 3 months ago

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);
Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

🇵🇹Portugal rvelhote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 🇫🇷France dqd London | N.Y.C | Paris | Hamburg | Berlin

    Link module is in Drupal core now. I'll close this issue due to inactivity for 12 years and upcoming EOL of Drupal 7 in January 2025 . While the project version for Drupal 7 will keep providing support for security issues and website breaking bug fixes for Drupal 7, we should try to minimize the open issues for Drupal 7 in the queue as much as possible. Thanks for the report and all the efforts in here. Feel free to re-open if you can provide a fix or found an additional related problem which increases the issue priority to "Critical" here.

Production build 0.71.5 2024