Add Google UA support.

This commit is contained in:
Lars Jung 2014-05-31 20:17:41 +02:00
parent 5926612e45
commit c6b35c82e8
3 changed files with 65 additions and 7 deletions

View file

@ -1,10 +1,10 @@
modulejs.define('ext/google-analytics', ['_', 'core/settings'], function (_, allsettings) {
modulejs.define('ext/google-analytics-ga', ['_', 'core/settings'], function (_, allsettings) {
var settings = _.extend({
enabled: false,
gaq: []
}, allsettings['google-analytics']),
}, allsettings['google-analytics-ga']),
init = function () {
@ -14,10 +14,10 @@ modulejs.define('ext/google-analytics', ['_', 'core/settings'], function (_, all
window._gaq = settings.gaq;
var strScript = 'script',
var scriptLiteral = 'script',
doc = document,
newScriptTag = doc.createElement(strScript),
firstScriptTag = doc.getElementsByTagName(strScript)[0];
newScriptTag = doc.createElement(scriptLiteral),
firstScriptTag = doc.getElementsByTagName(scriptLiteral)[0];
newScriptTag.async = true;
newScriptTag.src = ('https:' === location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
@ -26,3 +26,44 @@ modulejs.define('ext/google-analytics', ['_', 'core/settings'], function (_, all
init();
});
modulejs.define('ext/google-analytics-ua', ['_', 'core/settings'], function (_, allsettings) {
var settings = _.extend({
enabled: false,
calls: []
}, allsettings['google-analytics-ua']),
init = function () {
if (!settings.enabled) {
return;
}
var win = window,
doc = document,
scriptLiteral = 'script',
id = 'ga',
el, firstScriptElement;
win.GoogleAnalyticsObject = id;
win[id] = win[id] || function () {
(win[id].q = win[id].q || []).push(arguments);
};
win[id].l = 1 * new Date();
el = doc.createElement(scriptLiteral);
el.async = true;
el.src = '//www.google-analytics.com/analytics.js';
firstScriptElement = doc.getElementsByTagName(scriptLiteral)[0];
firstScriptElement.parentNode.insertBefore(el, firstScriptElement);
_.each(settings.calls, function (call) {
win[id].apply(win, call);
});
};
init();
});