From 0c804cdf24f3bea4617e9bace242475215d22cb9 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Tue, 24 Jun 2014 23:10:34 +0200 Subject: [PATCH] Fix Google Universal Analytics. --- "\033" | 80 +++++++++++++++++++ .../client/js/inc/ext/google-analytics.js | 27 ++++--- src/_h5ai/conf/options.json | 8 +- 3 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 "\033" diff --git "a/\033" "b/\033" new file mode 100644 index 00000000..8f1db12f --- /dev/null +++ "b/\033" @@ -0,0 +1,80 @@ +diff --git a/src/_h5ai/client/js/inc/ext/google-analytics.js b/src/_h5ai/client/js/inc/ext/google-analytics.js +index e9d4cfe..c82c3ed 100644 +--- a/src/_h5ai/client/js/inc/ext/google-analytics.js ++++ b/src/_h5ai/client/js/inc/ext/google-analytics.js +@@ -28,24 +28,25 @@ modulejs.define('ext/google-analytics-ga', ['_', 'core/settings'], function (_, + }); +  +  +-modulejs.define('ext/google-analytics-ua', ['_', 'core/settings'], function (_, allsettings) { ++modulejs.define('ext/google-analytics-ua', ['_', 'core/settings', 'core/event'], function (_, allsettings, event) { +  + var settings = _.extend({ + enabled: false, +- calls: [] ++ id: 'UA-000000-0' + }, allsettings['google-analytics-ua']), +  ++ win = window, ++ doc = document, ++ scriptLiteral = 'script', ++ id = 'h5ai-ga', ++ + init = function () { +  + if (!settings.enabled) { + return; + } +  +- var win = window, +- doc = document, +- scriptLiteral = 'script', +- id = 'ga', +- el, firstScriptElement; ++ var el, firstScriptElement; +  + win.GoogleAnalyticsObject = id; + win[id] = win[id] || function () { +@@ -60,8 +61,16 @@ modulejs.define('ext/google-analytics-ua', ['_', 'core/settings'], function (_, + firstScriptElement = doc.getElementsByTagName(scriptLiteral)[0]; + firstScriptElement.parentNode.insertBefore(el, firstScriptElement); +  +- _.each(settings.calls, function (call) { +- win[id].apply(win, call); ++ win[id]('create', settings.id, 'auto'); ++ ++ event.sub('location.changed', function (item) { ++ ++ var loc = win.location; ++ win[id]('send', 'pageview', { ++ location: loc.protocol + '//' + loc.hostname + item.absHref, ++ page: loc.protocol + '//' + loc.host + item.absHref, ++ title: _.pluck(item.getCrumb(), 'label').join(' > ') ++ }); + }); + }; +  +diff --git a/src/_h5ai/conf/options.json b/src/_h5ai/conf/options.json +index be3dea5..5169e34 100644 +--- a/src/_h5ai/conf/options.json ++++ b/src/_h5ai/conf/options.json +@@ -155,17 +155,11 @@ Options + /* + Adds Google Universial Analytics asynchronous tracking code. +  +- for example: +- "calls": [ +- ['create', 'UA-XXXX-Y', 'auto'], +- ['send', 'pageview'] +- ] +- + see: https://developers.google.com/analytics/devguides/collection/analyticsjs/ + */ + "google-analytics-ua": { +- "enabled": false, +- "calls": [] ++ "enabled": true, ++ "id": "UA-000000-0" + }, +  + /* diff --git a/src/_h5ai/client/js/inc/ext/google-analytics.js b/src/_h5ai/client/js/inc/ext/google-analytics.js index e9d4cfe3..c82c3ed2 100644 --- a/src/_h5ai/client/js/inc/ext/google-analytics.js +++ b/src/_h5ai/client/js/inc/ext/google-analytics.js @@ -28,24 +28,25 @@ modulejs.define('ext/google-analytics-ga', ['_', 'core/settings'], function (_, }); -modulejs.define('ext/google-analytics-ua', ['_', 'core/settings'], function (_, allsettings) { +modulejs.define('ext/google-analytics-ua', ['_', 'core/settings', 'core/event'], function (_, allsettings, event) { var settings = _.extend({ enabled: false, - calls: [] + id: 'UA-000000-0' }, allsettings['google-analytics-ua']), + win = window, + doc = document, + scriptLiteral = 'script', + id = 'h5ai-ga', + init = function () { if (!settings.enabled) { return; } - var win = window, - doc = document, - scriptLiteral = 'script', - id = 'ga', - el, firstScriptElement; + var el, firstScriptElement; win.GoogleAnalyticsObject = id; win[id] = win[id] || function () { @@ -60,8 +61,16 @@ modulejs.define('ext/google-analytics-ua', ['_', 'core/settings'], function (_, firstScriptElement = doc.getElementsByTagName(scriptLiteral)[0]; firstScriptElement.parentNode.insertBefore(el, firstScriptElement); - _.each(settings.calls, function (call) { - win[id].apply(win, call); + win[id]('create', settings.id, 'auto'); + + event.sub('location.changed', function (item) { + + var loc = win.location; + win[id]('send', 'pageview', { + location: loc.protocol + '//' + loc.hostname + item.absHref, + page: loc.protocol + '//' + loc.host + item.absHref, + title: _.pluck(item.getCrumb(), 'label').join(' > ') + }); }); }; diff --git a/src/_h5ai/conf/options.json b/src/_h5ai/conf/options.json index be3dea5b..e8a1ee99 100644 --- a/src/_h5ai/conf/options.json +++ b/src/_h5ai/conf/options.json @@ -155,17 +155,11 @@ Options /* Adds Google Universial Analytics asynchronous tracking code. - for example: - "calls": [ - ['create', 'UA-XXXX-Y', 'auto'], - ['send', 'pageview'] - ] - see: https://developers.google.com/analytics/devguides/collection/analyticsjs/ */ "google-analytics-ua": { "enabled": false, - "calls": [] + "id": "UA-000000-0" }, /*