A SilverStripe module to support flexible page and event level tracking to Google Analytics

Installs: 144

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 7

Forks: 2

Open Issues: 0



dev-master 2016-07-10 21:16 UTC

This package is auto-updated.

Last update: 2020-05-21 03:17:13 UTC


The Google Tracking module makes adding tracking to your site easier. It provides two basic mechanisms:

  • Page level tracking that is automatically hooked into your pages
  • event level tracking for attaching tracking actions on DOM events.


Ensure that the section of your site contains the following:


This will include a tag for configuration information for the Google API.

Configuration properties that are only settable in config.yml of your site include:

  • self_hosted_tracking_js - if 1, it will serve the module's copy of the Google universal_analytics.js script. This is for sites where the content policy prohibits inclusion of cross-domain scripts.

Also, you will need to bootstrap the javascript component of the module by doing the following:

(function($) {
	var tracker = $.tracker;
	$(document).ready(function () {
		// add calls to tracker.addEventDef here if you are using them
}) (jQuery);

or a short form if you prefer:

(function($) {
	$(document).ready(function () {
}) (jQuery);

The parameter to init() is used if there are delays in loading the google analytics javascript. Init will wait until it is loaded, and waits for this period each time (in milliseconds) before retrying.

Page Level Tracking

The module will automatically add itself to ContentController, thereby including page tracking for all pages on the site. However, if you have a custom controller that does not extend ContentController, but you want tracking behaviour added, simple add the extension to your controller as well:

	- GoogleTrackingControllerExtension

Event Tracking

Using DOM classes

The module supports registering tracking events directly against DOM elements by decorating your markup with attributes. For example, to track clicks on the header icon of a page, you might have markup that looks like this:

<div class="logo">
	<a href="$HomePageLink"

This will send tracking data to Google on click of the element. The category and action are sent as the literal values provided. The label sent is the value of the href attribute of the clicked tab, in this case the URL that is linked to.

The full list of supported atributes is:

		name of event to listen to. Can be 'click', 'submit' or 'pageload'.
		category sent in tracking event
		action sent in tracking event
		label sent in tracking event
		value sent in tracking event
		if "true" or "1" then event is sent as non-interactive
		if "true" or "1" then event stopPropagation and preventDefault
		are called.
		if specified, it's value needs to be the number of milliseconds
		to delay before triggering.
		if specified, the value is evaluated to determine if the tracking
		event should be sent. If null, empty string, false or 0, or an
		array whose length is zero (including jQuery objects), the
		tracking event won't be sent. Otherwise it will be.

data-ga-category, data-ga-action, data-ga-label, data-ga-value and data-ga-cond attributes all use the same syntax for specifying values. If the attribute value starts with an '@', the value specifies an expression for getting the value. Otherwise the value is interpreted as a literal. If an @-expression is used, it can be one of the following forms:

		get attribute value of this element
		e.g. data-ga-label="@attr:href"

		get attribute value of another element
		e.g. data-ga-label="@attr:value,@sel:#myfield"

		get attribute value of another element, but within the current element i.e uses $(someSelector, $el)
		e.g. data-ga-label="@attr:value,@sel:span,@this"

		get the inner text of the target element. Equivalent to calling

		call a named function to evaluate the value. This may be prohibited
		by content policy. The function is called with the matching
		element as a parameter. The function name is evaluated in the
		global name space.
		e.g. data-ga-label="@call:someobject.myMethod"

Note you can also inject template variables, which allows server-side determination of:

<div class="logo">
	<a href="$HomePageLink"

Using JavaScript events

If you prefer using JavaScript to initiate event tracking, or have requirements that are not met by the simplified DOM class mechanism, the module also supports registering DOM listeners by jQuery selector.

An advantage this has over using DOM classes is that you can programmatically control the properties sent to Google. The disadvantage is that you cannot inject property values easily.

	'.ga-content-container a[rel!="external"]',
		"eventCategory": "navigation",
		"eventAction": function() { return 'linked-content' },
		"eventLabel": function() { return $(this).attr('href') },
		"eventValue": function() { return null },
		"nonInteraction": false

This works by registering a listener against DOM elements that match the selector. The parameters are:

id (string)
		a unique name you give to each rule
		event name, such as "click" or "submit"
bodyClasses (string)	
		if present, rule only applies if body matches these classes.
selector (string)
		selector to match against. Event listener is attached to this
		if set, it is a selector that the listener is attached to instead
		of the selector, and the selector is used to filter components.
		Correlates to $(delegate).on(names, selector)
		if set, specifies a number of milliseconds to wait before triggering the event using jQuery.simulate. Also terminates
		the originating event.
		if true, causes event.preventDefault and event.stopPropagation.
googleEventData (object)
		an object to pass to GA tracking. The object can have properties:
		The first 4 parameters can be a literal string, or can be a
		function that returns a string.


May need to include jquery simulate Relies on framework jquery