Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added code

  • Loading branch information...
commit 1487f1115a4f422ebfa4975f606fbb80c91d8404 1 parent f1fc91c
Rob authored October 28, 2011

Showing 1 changed file with 142 additions and 0 deletions. Show diff stats Hide diff stats

  1. 142  jquery.building-blocks.googleanalytics.js
142  jquery.building-blocks.googleanalytics.js
... ...
@@ -0,0 +1,142 @@
  1
+
  2
+/*!
  3
+* Google Analytics Event Tracking jQuery Plugin
  4
+*
  5
+* Copyright (c) 2011 Building Blocks
  6
+*
  7
+* Author: Robert Stevenson-Leggett
  8
+* Version: 0.3
  9
+*
  10
+* Handles event tracking through the use of data attributes
  11
+*
  12
+*/
  13
+(function ($) {
  14
+
  15
+    //Wrap the track event function
  16
+    var trackEvent = function (category, action, label, value) {
  17
+        if (console !== undefined && console.log !== undefined) {
  18
+            console.log('GA tracking event: category=' + category + ', action=' + action + ', label=' + label + ', value=' + value);
  19
+        }
  20
+        _gaq.push(['_trackEvent', category, action, label, value]);
  21
+    };
  22
+
  23
+    var defaultOptions = {
  24
+        //The category attribute
  25
+        categoryAttribute: 'data-ga-category',
  26
+        //The action attribute
  27
+        actionAttribute: 'data-ga-action',
  28
+        //The label attribute (could be changed to href when tracking file downloads)
  29
+        labelAttribute: 'data-ga-label',
  30
+        //The value attribute (must be integer)
  31
+        valueAttribute: 'data-ga-value',
  32
+        //Whether to look for the label
  33
+        useLabel: true,
  34
+        //Whether to look for a value
  35
+        useValue: false,
  36
+        //false = track as soon as the plugin loads, true = bind to an event
  37
+        useEvent: false,
  38
+        //The event to bind to if useEvent is true
  39
+        event: 'click',
  40
+        //A method to call to check whether or not we should call the tracking when the event is clicked
  41
+        valid: function (elem) { return true; },
  42
+        //Tracking complete
  43
+        complete: function (elem) { },
  44
+        //When using an event, delay the browser to allow the event to fire
  45
+        delay: 0,
  46
+        //Category should always be set if using gaTrackEvent
  47
+        category: 'Unspecified',
  48
+        //Action should always be set if using gaTrackEvent
  49
+        action: 'Unspecified'
  50
+    });
  51
+
  52
+    //
  53
+    // gaTrackEvent adds unobtrusive tracking attributes itself
  54
+    // So you can add tracking to links etc.
  55
+    //
  56
+    // This allows to do sitewide event tracking e.g. Document Downloads just
  57
+    // by selecting the elements e.g.
  58
+    //
  59
+    //
  60
+    //  $('.track-download').gaTrackEvent({
  61
+    //          category:'Downloads', action:'PDF', useEvent:true, event:'click'
  62
+    //    });
  63
+    //
  64
+    $.fn.gaTrackEvent = function (options) {
  65
+        options = $.extend(defaultOptions, options);
  66
+
  67
+        return this.each(function () {
  68
+            var element = $(this);
  69
+            element.attr(options.categoryAttribute, options.category);
  70
+            element.attr(options.actionAttribute, options.action);
  71
+
  72
+            if (options.useLabel) {
  73
+                element.attr(options.labelAttribute, options.label);
  74
+            }
  75
+            if (options.useValue) {
  76
+                element.attr(options.valueAttribute, options.value);
  77
+            }
  78
+            
  79
+            element.gaTrackEventUnobtrusive(options);
  80
+        });
  81
+    };
  82
+
  83
+    //Create a plugin
  84
+    $.fn.gaTrackEventUnobtrusive = function (options) {
  85
+
  86
+        //Merge options
  87
+        options = $.extend(defaultOptions, options);
  88
+
  89
+        //Keep the chain going
  90
+        return this.each(function () {
  91
+
  92
+            var _this = $(this);
  93
+
  94
+            //Wrap the tracking so we can reuse it.
  95
+            var callTrackEvent = function () {
  96
+                //Retreive the info
  97
+                var category = _this.attr(options.categoryAttribute);
  98
+                var action = _this.attr(options.actionAttribute);
  99
+                var label = _this.attr(options.labelAttribute);
  100
+                var value = _this.attr(options.valueAttribute);
  101
+
  102
+                if (options.useLabel && options.useValue) {
  103
+                    trackEvent(category, action, label, value);
  104
+                }
  105
+                else if (options.useLabel) {
  106
+                    trackEvent(category, action, label);
  107
+                }
  108
+                else {
  109
+                    trackEvent(category, action);
  110
+                }
  111
+            };
  112
+
  113
+            //If we want to bind to an event, do it.
  114
+            if (options.useEvent == true) {
  115
+
  116
+                //This is what happens when you actually click a button
  117
+                var constructedFunction = function () {
  118
+                    //Check the callback function
  119
+                    if (options.valid(_this) === true) {
  120
+                        callTrackEvent();
  121
+                        options.complete(_this);
  122
+                    }
  123
+                };
  124
+
  125
+                //E.g. if we are going to click on a link
  126
+                _this.bind(options.event, constructedFunction);
  127
+            }
  128
+            else {
  129
+                //Otherwise just track immediately (e.g. if we just came from a post-back)
  130
+                callTrackEvent();
  131
+            }
  132
+        });
  133
+    };
  134
+
  135
+    //an alternative way to call the function.
  136
+    $.extend({
  137
+        ga: {
  138
+            trackEvent: trackEvent
  139
+        }
  140
+    });
  141
+
  142
+})(jQuery);

0 notes on commit 1487f11

Please sign in to comment.
Something went wrong with that request. Please try again.