summaryrefslogtreecommitdiff
path: root/addons/web_analytics/static/src/js/web_analytics.js
diff options
context:
space:
mode:
authorQuentin (OpenERP) <qdp-launchpad@openerp.com>2013-02-05 16:55:56 +0100
committerQuentin (OpenERP) <qdp-launchpad@openerp.com>2013-02-05 16:55:56 +0100
commit552583b9d802090e0e92a78586cb4878abc95b11 (patch)
tree31a1d5ce6e796dc250171671eca90b56484fd0ae /addons/web_analytics/static/src/js/web_analytics.js
parent451c85c52a066df0e6859250cf43a43e23e93f63 (diff)
parent82da94b3edc75e02104454f24b909e9c7a3a8ad1 (diff)
[MERGE] merged v7 branch
bzr revid: qdp-launchpad@openerp.com-20130205155556-b4zw4y3gr9v2eom3
Diffstat (limited to 'addons/web_analytics/static/src/js/web_analytics.js')
-rw-r--r--addons/web_analytics/static/src/js/web_analytics.js163
1 files changed, 80 insertions, 83 deletions
diff --git a/addons/web_analytics/static/src/js/web_analytics.js b/addons/web_analytics/static/src/js/web_analytics.js
index 28df9610bbf..c8212f4d213 100644
--- a/addons/web_analytics/static/src/js/web_analytics.js
+++ b/addons/web_analytics/static/src/js/web_analytics.js
@@ -16,7 +16,7 @@ openerp.web_analytics = function(instance) {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);
})();
@@ -25,16 +25,15 @@ openerp.web_analytics = function(instance) {
/*
* This method initializes the tracker
*/
- init: function() {
- /* Comment this lines when going on production, only used for testing on localhost
- _gaq.push(['_setAccount', 'UA-35793871-1']);
- _gaq.push(['_setDomainName', 'none']);
- */
-
- /* Uncomment this lines when going on production */
+ init: function(webclient) {
+ var self = this;
+ self.initialized = $.Deferred();
_gaq.push(['_setAccount', 'UA-7333765-1']);
_gaq.push(['_setDomainName', '.openerp.com']); // Allow multi-domain
- /**/
+ self.initialize_custom(webclient).then(function() {
+ webclient.on('state_pushed', self, self.on_state_pushed);
+ self.include_tracker();
+ });
},
/*
* This method MUST be overriden by saas_demo and saas_trial in order to
@@ -48,41 +47,53 @@ openerp.web_analytics = function(instance) {
* This method gets the user access level, to be used as CV in GA
*/
_get_user_access_level: function() {
+ if (!instance.session.session_is_valid()) {
+ return "Unauthenticated User";
+ }
if (instance.session.uid === 1) {
return 'Admin User';
+ }
// Make the difference between portal users and anonymous users
- } else if (instance.session.username.indexOf('@') !== -1) {
- if (instance.session.username.indexOf('anonymous') === -1) {
- return 'Portal User';
- } else {
- return 'Anonymous User';
- }
- } else if (instance.session.username.indexOf('anonymous') !== -1) {
+ if (instance.session.username.indexOf('@') !== -1) {
+ return 'Portal User';
+ }
+ if (instance.session.username === 'anonymous') {
return 'Anonymous User';
- } else {
- return 'Normal User';
}
+ return 'Normal User';
},
+
/*
* This method contains the initialization of all user-related custom variables
* stored in GA. Also other modules can override it to add new custom variables
+ * Must be followed by a call to _push_*() in order to actually send the data
+ * to GA.
*/
initialize_custom: function() {
var self = this;
- return instance.session.rpc("/web/webclient/version_info", {})
+ instance.session.rpc("/web/webclient/version_info", {})
.done(function(res) {
_gaq.push(['_setCustomVar', 5, 'Version', res.server_version, 3]);
- // Track User Access Level, Custom Variable 4 in GA with visitor level scope
- // Values: 'Admin User', 'Normal User', 'Portal User', 'Anonymous User'
- _gaq.push(['_setCustomVar', 4, 'User Access Level', self.user_access_level, 1]);
-
- // Track User Type Conversion, Custom Variable 3 in GA with session level scope
- // Values: 'Visitor', 'Demo', 'Online Trial', 'Online Paying', 'Local User'
- _gaq.push(['_setCustomVar', 1, 'User Type Conversion', self._get_user_type(), 2]);
- _gaq.push(['_trackPageview']);
- return;
+ self._push_customvars();
+ self.initialized.resolve(self);
});
+ return self.initialized;
},
+
+ /*
+ * Method called in order to send _setCustomVar to GA
+ */
+ _push_customvars: function() {
+ var self = this;
+ // Track User Access Level, Custom Variable 4 in GA with visitor level scope
+ // Values: 'Admin User', 'Normal User', 'Portal User', 'Anonymous User'
+ _gaq.push(['_setCustomVar', 4, 'User Access Level', self._get_user_access_level(), 1]);
+
+ // Track User Type Conversion, Custom Variable 3 in GA with session level scope
+ // Values: 'Visitor', 'Demo', 'Online Trial', 'Online Paying', 'Local User'
+ _gaq.push(['_setCustomVar', 1, 'User Type Conversion', self._get_user_type(), 2]);
+ },
+
/*
* Method called in order to send _trackPageview to GA
*/
@@ -142,6 +153,7 @@ openerp.web_analytics = function(instance) {
'action': state.view_type,
'label': url,
});
+ this._push_pageview(url);
}
},
/*
@@ -157,19 +169,19 @@ openerp.web_analytics = function(instance) {
this._super.apply(this, arguments);
var self = this;
this.on('record_created', self, function(r) {
- var url = instance.web_analytics.generateUrl({'model': r.model, 'view_type': 'form'});
+ var url = instance.web_analytics.generateUrl({'model': self.model, 'view_type': 'form'});
t._push_event({
- 'category': r.model,
- 'action': 'form',
+ 'category': self.model,
+ 'action': 'create',
'label': url,
'noninteraction': true,
});
});
this.on('record_saved', self, function(r) {
- var url = instance.web_analytics.generateUrl({'model': r.model, 'view_type': 'form'});
+ var url = instance.web_analytics.generateUrl({'model': self.model, 'view_type': 'form'});
t._push_event({
- 'category': r.model,
- 'action': 'form',
+ 'category': self.model,
+ 'action': 'save',
'label': url,
'noninteraction': true,
});
@@ -181,12 +193,12 @@ openerp.web_analytics = function(instance) {
instance.web.ActionManager.include({
ir_actions_client: function (action, options) {
var url = instance.web_analytics.generateUrl({'action': action.tag});
- var category = action.res_model || action.type;
t._push_event({
- 'category': action.res_model || action.type,
- 'action': action.name || action.tag,
+ 'category': action.type,
+ 'action': action.tag,
'label': url,
});
+ t._push_pageview(url);
return this._super.apply(this, arguments);
},
});
@@ -224,21 +236,12 @@ openerp.web_analytics = function(instance) {
options = {'action': params.action};
}
var url = instance.web_analytics.generateUrl(options);
- if (error.code) {
- t._push_event({
- 'category': error.message,
- 'action': error.data.message,
- 'label': url,
- 'noninteraction': true,
- });
- } else {
- t._push_event({
- 'category': error.type,
- 'action': error.data.debug,
- 'label': url,
- 'noninteraction': true,
- });
- }
+ t._push_event({
+ 'category': options.model || "ir.actions.client",
+ 'action': "error " + (error.code ? error.message + error.data.message : error.type + error.data.debug),
+ 'label': url,
+ 'noninteraction': true,
+ });
this._super.apply(this, arguments);
},
});
@@ -251,43 +254,37 @@ openerp.web_analytics = function(instance) {
instance.web_analytics.generateUrl = function(options) {
var url = '';
- _.each(options, function(value, key) {
- url += '/' + key + '=' + value;
+ var keys = _.keys(options);
+ keys = _.sortBy(keys, function(i) { return i;});
+ _.each(keys, function(key) {
+ url += '/' + key + '/' + options[key];
});
return url;
};
+ // kept for API compatibility
instance.web_analytics.setupTracker = function(wc) {
- var t = wc.tracker;
- return $.when(t._get_user_access_level()).then(function(r) {
- t.user_access_level = r;
- t.initialize_custom().then(function() {
- wc.on('state_pushed', t, t.on_state_pushed);
- t.include_tracker();
- });
- });
+ return wc.tracker.initialized;
};
- // Set correctly the tracker in the current instance
- if (instance.client instanceof instance.web.WebClient) { // not for embedded clients
- instance.webclient.tracker = new instance.web_analytics.Tracker();
- instance.web_analytics.setupTracker(instance.webclient);
- } else if (!instance.client) {
- // client does not already exists, we are in monodb mode
- instance.web.WebClient.include({
- start: function() {
- var d = this._super.apply(this, arguments);
- this.tracker = new instance.web_analytics.Tracker();
- return d;
- },
- show_application: function() {
- var self = this;
- $.when(this.subscribe_deferred).then(function() {
- instance.web_analytics.setupTracker(self);
- });
- this._super();
- },
- });
- }
+ instance.web.Client.include({
+ bind_events: function() {
+ this._super.apply(this, arguments);
+ this.tracker = new instance.web_analytics.Tracker(this);
+ },
+ });
+ instance.web.Session.include({
+ session_authenticate: function() {
+ return $.when(this._super.apply(this, arguments)).then(function() {
+ // the call to bind_events() may have been delayed in some embed
+ // cases, and when that happens we can skip the push, as the
+ // proper one will be done when bind_event is eventually called.
+ if (instance.client && instance.client.tracker) {
+ instance.client.tracker._push_customvars();
+ }
+ });
+ },
+ });
+
};