[FIX] web_analytics: works in monodb mode

bzr revid: chs@openerp.com-20121204131322-ob2hgda17oj83fax
This commit is contained in:
Christophe Simonis 2012-12-04 14:13:22 +01:00
parent 966890c540
commit a7ec9f3743
1 changed files with 113 additions and 79 deletions

View File

@ -14,16 +14,17 @@ openerp.web_analytics = function(instance) {
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true
ga.async = true;
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);
})();
if (instance.webclient) {
// Set the account and domain to start tracking
_gaq.push(['_setAccount', 'UA-7333765-1']); // vta@openerp.com localhost
var init_tracker = function() {
// initialize tracker
_gaq.push(['_setAccount', 'UA-7333765-1']);
//_gaq.push(['_setAccount', 'UA-36797757-1']); // Debug code
_gaq.push(['_setDomainName', 'none']); // Change for the real domain
// Track user types
@ -43,9 +44,9 @@ openerp.web_analytics = function(instance) {
_gaq.push(['_setCustomVar', 3, 'View Type', 'default', 3]);
_gaq.push(['_trackPageview']);
};
var self = this;
instance.webclient.on('state_pushed', self, function(state) {
var on_state_pushed = function(state) {
// Track only pages corresponding to a 'normal' view of OpenERP, views
// related to client actions are tracked by the action manager
if (state.model && state.view_type) {
@ -54,11 +55,13 @@ openerp.web_analytics = function(instance) {
// Tack view usage
_gaq.push(['_setCustomVar', 3, 'View Type', state.view_type, 3]);
// Track the page
var url = instance.web_analytics.parseUrl({'model': state.model, 'view_type': state.view_type});
var url = instance.web_analytics.generateUrl({'model': state.model, 'view_type': state.view_type});
_gaq.push(['_trackPageview', url]);
}
});
}
};
var include_tracker = function() {
// include the tracker into views and managers
// Track the events related with the creation and the modification of records
instance.web.FormView = instance.web.FormView.extend({
@ -66,11 +69,11 @@ 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.parseUrl({'model': this.model, 'view_type': 'form'});
var url = instance.web_analytics.generateUrl({'model': this.model, 'view_type': 'form'});
_gaq.push(['_trackEvent', this.model, 'on_button_create_save', url]);
});
this.on('record_saved', self, function(r) {
var url = instance.web_analytics.parseUrl({'model': this.model, 'view_type': 'form'});
var url = instance.web_analytics.generateUrl({'model': this.model, 'view_type': 'form'});
_gaq.push(['_trackEvent', this.model, 'on_button_edit_save', url]);
});
}
@ -79,7 +82,7 @@ openerp.web_analytics = function(instance) {
// Track client actions
instance.web.ActionManager.include({
ir_actions_client: function (action, options) {
var url = instance.web_analytics.parseUrl({'action': action.tag});
var url = instance.web_analytics.generateUrl({'action': action.tag});
_gaq.push(['_trackPageview', url]);
return this._super.apply(this, arguments);
},
@ -96,14 +99,14 @@ openerp.web_analytics = function(instance) {
} else {
action = action_data.string || action_data.special || '';
}
var label = instance.web_analytics.parseUrl({'model': category, 'view_type': this.view_type});
var label = instance.web_analytics.generateUrl({'model': category, 'view_type': this.view_type});
_gaq.push(['_trackEvent', category, action, label]);
return this._super.apply(this, arguments);
},
});
// Track error events
instance.web.CrashManager = instance.web.CrashManager.extend({
instance.web.CrashManager.include({
show_error: function(error) {
var hash = window.location.hash;
var params = $.deparam(hash.substr(hash.indexOf('#')+1));
@ -113,7 +116,7 @@ openerp.web_analytics = function(instance) {
} else {
options = {'action': params.action};
}
var label = instance.web_analytics.parseUrl(options);
var label = instance.web_analytics.generateUrl(options);
if (error.code) {
_gaq.push(['_trackEvent', error.message, error.data.fault_code, label, ,true]);
} else {
@ -122,8 +125,39 @@ openerp.web_analytics = function(instance) {
this._super.apply(this, arguments);
},
});
};
instance.web_analytics.parseUrl = function(options) {
if (instance.client instanceof instance.web.WebClient) { // not for embedded clients
init_tracker();
// Set the account and domain to start tracking
instance.client.on('state_pushed', this, on_state_pushed);
include_tracker();
} else if (!instance.client) {
// client does not already exists, we are in monodb mode
instance.web.WebClient.include({
init: function() {
init_tracker();
return this._super.apply(this, arguments);
},
start: function() {
var self = this;
return this._super.apply(this, arguments).done(function() {
self.on('state_pushed', self, on_state_pushed);
include_tracker();
});
}
});
}
// ----------------------------------------------------------------
// utility functions
// ----------------------------------------------------------------
instance.web_analytics.generateUrl = function(options) {
var url = '';
_.each(options, function(value, key) {
url += '/' + key + '=' + value;