From e25e5b72f4eea5ed1169b6846a2004cc925c9912 Mon Sep 17 00:00:00 2001
From: Lars Jung <lrsjng@gmail.com>
Date: Thu, 2 Aug 2012 23:13:43 +0200
Subject: [PATCH] Adds Google Analytics support (async).

---
 src/_h5ai/config.js                      | 18 ++++++++++++++
 src/_h5ai/js/inc/ext/google-analytics.js | 30 ++++++++++++++++++++++++
 src/_h5ai/js/inc/main.js                 |  1 +
 3 files changed, 49 insertions(+)
 create mode 100644 src/_h5ai/js/inc/ext/google-analytics.js

diff --git a/src/_h5ai/config.js b/src/_h5ai/config.js
index 096ede71..5589798c 100644
--- a/src/_h5ai/config.js
+++ b/src/_h5ai/config.js
@@ -115,6 +115,24 @@ var H5AI_CONFIG = {
 			"folders": {}
 		},
 
+		/*
+		 * Adds Google Analytics asynchronous tracking code.
+		 *
+		 * for example:
+		 * "gaq": [
+		 *   ["_setAccount", "UA-xxxxxx-x"],
+		 *   ["_setDomainName", ".your-domain.tld"],
+		 *   ["_trackPageview"],
+		 *   ["_trackPageLoadTime"]
+		 * ]
+		 *
+		 * see: http://support.google.com/googleanalytics/bin/topic.py?hl=en&topic=27612
+		 */
+		"google-analytics": {
+			"enabled": false,
+			"gaq": []
+		},
+
 		/*
 		 * Localization, for example "en", "de" etc. - see "langs" below for
 		 * possible values. Adjust it to your needs. If lang is not found in
diff --git a/src/_h5ai/js/inc/ext/google-analytics.js b/src/_h5ai/js/inc/ext/google-analytics.js
new file mode 100644
index 00000000..60a82d33
--- /dev/null
+++ b/src/_h5ai/js/inc/ext/google-analytics.js
@@ -0,0 +1,30 @@
+
+modulejs.define('ext/google-analytics', ['_', 'core/settings'], function (_, allsettings) {
+
+	var defaults = {
+			enabled: false,
+			gaq: []
+		},
+
+		settings = _.extend({}, defaults, allsettings['google-analytics']),
+
+		init = function () {
+
+			if (!settings.enabled) {
+				return;
+			}
+
+			window._gaq = settings.gaq;
+
+			var strScript = 'script',
+				doc = document,
+				newScriptTag = doc.createElement(strScript),
+				firstScriptTag = doc.getElementsByTagName(strScript)[0];
+
+			newScriptTag.async = true;
+			newScriptTag.src = ('https:' === location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+			firstScriptTag.parentNode.insertBefore(newScriptTag, firstScriptTag);
+		};
+
+	init();
+});
diff --git a/src/_h5ai/js/inc/main.js b/src/_h5ai/js/inc/main.js
index 6449574b..e656c598 100644
--- a/src/_h5ai/js/inc/main.js
+++ b/src/_h5ai/js/inc/main.js
@@ -28,6 +28,7 @@
 	// @include "ext/download.js"
 	// @include "ext/filter.js"
 	// @include "ext/folderstatus.js"
+	// @include "ext/google-analytics.js"
 	// @include "ext/l10n.js"
 	// @include "ext/link-hover-states.js"
 	// @include "ext/mode.js"