Skip to content

Commit

Permalink
MINOR: fixing various merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Seidenberg committed Aug 21, 2012
2 parents ce37b5f + 0af3d61 commit c2e7e52
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 41 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -52,8 +52,9 @@ Bigger improvements:
1. follow the usual [module installation process](http://doc.silverstripe.org/modules#installation)
2. activate the logger by adding the following to the end of your mysite/_config.php: `GoogleLogger::activate('UA-XXXXX-Y');` (hardcode google code, useful in combination with _ss_environment.php) or `GoogleLogger::activate('SiteConfig');` (use SiteConfig for setup)
3. activate the analyzer by adding the following to the end of your mysite/_config.php: `GoogleAnalyzer::activate('1234567', "my@google.login", "mypassword");` (hardcode credentials, useful in combination with _ss_environment.php) or `GoogleAnalyzer::activate('SiteConfig');` (use SiteConfig for setup)
4. run dev/build (http://www.mysite.com/dev/build?flush=all)
5. if you're using SiteConfig populate your siteconfig in the CMS.
4. If you wish to active the event tracking helper, include `GoogleLogger::set_event_tracking_enabled(true)`
5. run dev/build (http://www.mysite.com/dev/build?flush=all)
6. if you're using SiteConfig populate your siteconfig in the CMS.

## Retrieving your credentials from GA

Expand Down
7 changes: 4 additions & 3 deletions code/GoogleAnalyzer.php
@@ -1,5 +1,8 @@
<?php

/**
* @package googleanalytics
*/
class GoogleAnalyzer extends DataExtension {

static public $sapphire_version;
Expand Down Expand Up @@ -44,10 +47,8 @@ public static function activate($profile = 'SiteConfig', $email = null, $passwor
Object::add_extension('SiteTree', 'GoogleAnalyzer');
}

public function updateCMSFields(FieldSet $fields) {

public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root', new Tab('GoogleAnalytics', 'Google Analytics'));

$fields->addFieldToTab("Root.GoogleAnalytics", new TabSet('Stats'));
$fields->addFieldToTab('Root.GoogleAnalytics.Stats', new Tab('Performance', 'Performance'));
$fields->addFieldToTab("Root.GoogleAnalytics.Stats.Performance", new GooglePerformanceChart($this->owner));
Expand Down
5 changes: 4 additions & 1 deletion code/GoogleConfig.php
@@ -1,5 +1,8 @@
<?php

/**
* @package googleanalytics
*/
class GoogleConfig extends DataExtension {

static $db = array(
Expand All @@ -9,7 +12,7 @@ class GoogleConfig extends DataExtension {
'GoogleAnalyticsPassword' => 'Varchar',
);

public function updateCMSFields(FieldSet $fields) {
public function updateCMSFields(FieldList $fields) {

$fields->addFieldToTab("Root", new Tab('GoogleAnalytics'));
$fields->addFieldToTab('Root.GoogleAnalytics', new TextField('GoogleAnalyticsCode', 'Google Analytics Code (UA-XXXXXX-X)'));
Expand Down
25 changes: 25 additions & 0 deletions code/GoogleLogger.php
Expand Up @@ -5,6 +5,11 @@ class GoogleLogger extends Extension {
// the Google Analytics code to be used in the JS snippet or
public static $google_analytics_code;

/**
* @var bool
*/
public static $include_event_tracking = false;

// supported web crawlers, keys for nice names and values for signature regexes
public static $web_crawlers = array(
'Google' => 'googlebot',
Expand Down Expand Up @@ -58,5 +63,25 @@ public function onAfterInit() {
}
}
}

// include event tracking api if required, jQuery 1.5 is required for automatic data attributes
if(self::event_tracking_enabled()) {
Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
Requirements::javascript('googleanalytics/javascript/googleanalytics.event.tracking.js');
}
}

/**
* @param bool
*/
public static function set_event_tracking_enabled($bool) {
self::$include_event_tracking = (bool) $bool;
}

/**
* @return bool
*/
public static function event_tracking_enabled() {
return self::$include_event_tracking;
}
}
Empty file added docs/_manifest_exclude
Empty file.
85 changes: 85 additions & 0 deletions docs/en/index.md
@@ -0,0 +1,85 @@
# Google Analytics Module

## Features

### Event Tracking API

Included in the module is a simple jQuery plugin which wraps Google Analytics Event
based tracking and provides several shortcuts to including event tracking in your
project.

To enable event tracking, first you will need to enable the Google Logger then include
`GoogleLogger::set_event_tracking_enabled(true)` in your mysite/_config.php file:

GoogleLogger::activate('UA-XXXX-XX');
GoogleLogger::set_event_tracking_enabled(true);

After including the event tracking API you can call `analyticsTrackEvent(1)` from your
own code whenever you need to track an interaction event.

#### analyticsTrackEvent(1)

`analyticsTrackEvent` takes an object with a number of properties. You can define
which ever properties you need to, otherwise the plugin will revert to some common
default values. Each of the provided properties in the object can either be a literal
value or a function which returns a literal value. This allows you to build complex
event labels and categories based on the entire DOM of a page and not just limited to
a single event object.

$(this).analyticsTrackEvent({
category: ..
label: ..
action: ...
value: ..
})


Property | Default Value | Optional?
------------- | --------------- | -----
Category | window.location | No
Action | 'click' | No
Label | $(this).text() | No
Value | NULL | Yes


#### Examples

Tracks any link with a class of *track*. Takes the default options of `analyticsTrackEvent(1)`
as shown in the table above

$("a.track").click(function() {
$(this).analyticsTrackEvent();
});

Tracks users focusing into a given text box. Sends a custom label and action label.

$("input.track").focus(function() {
$(this).trackEvent({
label: 'Entered Text into box',
action: 'focus'
});
});

Tracks when a user leaves an input box, what was the value that they entered into
the box. Demonstrates how you can use a function to return a dynamic value for any
of the properties.

$("input.track").blur(function() {
$(this).trackEvent({
category: function(elem) { return $(elem).parents("form").attr("name"); },
label: function(elem) { return $(elem).val(); },
action: 'leave text field'
})
});

## FAQ

### I'm not seeing any event information

Google takes up to 48 hours to process event tracking information in many cases,
so you may have to wait several hours to see your results.

If you still don't see any information appearing, try using the official
`ga_debug.js` extension to Google Chrome.

http://code.google.com/apis/analytics/docs/tracking/gaTrackingTroubleshooting.html#gaDebug
64 changes: 64 additions & 0 deletions javascript/googleanalytics.event.tracking.js
@@ -0,0 +1,64 @@
;(function($) {
/**
* A simple helper function for wrapping Google Analytic event tracking
* into sites through jQuery and HTML5 data attributes.
*
* See the provided documentation folder for instructions and examples
* on how to add event tracking.
*/
$.fn.analyticsTrackEvent = function(args) {
_gaq = _gaq || [];

/**
* Evaluates an argument. If a non terminal (i.e a function) return
* the result of that function. Otherwise returns the terminal value.
*/
var e = function(f) {
if($.isFunction(f)) return f(self); return f;
};

var category, action, label, value, self = $(this[0]);

var args = $.extend({
category: function(ele) {
category = $(ele).data('category');
if(!category) category = window.location.toString();

return category;
},
action: function(ele) {
action = $(ele).data('action');
if(!action) action = 'click';

return action;
},
label: function(ele) {
// optional but recommened field
label = $(ele).data('label');
if(!label) label = $(ele).text();

return label;
},
value: function(ele) {
return $(ele).data('value');
},
}, args);


var track = ['_trackEvent', e(args.category), e(args.action)];

label = e(args.label);
value = parseInt(e(args.value));

if(label) track.push(label);
if(value) track.push(value);

try {
_gaq.push(track);
} catch(err) {
if(window.console != undefined) {
window.console.log(err);
}
}
}
})(jQuery);

0 comments on commit c2e7e52

Please sign in to comment.