[IMP] Refactoring web_analytics, step 2

bzr revid: vta@openerp.com-20130103091821-mo57o9oybzpyrzqm
This commit is contained in:
vta vta@openerp.com 2013-01-03 10:18:21 +01:00
parent 1774e390ec
commit a1d18ccb7a
1 changed files with 135 additions and 126 deletions

View File

@ -3,7 +3,8 @@ var _gaq = _gaq || []; // asynchronous stack used by google analytics
openerp.web_analytics = function(instance) {
/** The Google Analytics Module inserts the Google Analytics JS Snippet
/*
* The Google Analytics Module inserts the Google Analytics JS Snippet
* at the top of the page, and sends to google an url each time the
* openerp url is changed.
* The pushes of the urls is made by triggering the 'state_pushed' event in the
@ -21,37 +22,49 @@ openerp.web_analytics = function(instance) {
})();
instance.web_analytics.Tracker = instance.web.Class.extend({
/*
* This method initializes the tracker
*/
init: function() {
// initialize tracker
_gaq.push(['_setAccount', 'UA-35793871-1']);
// _gaq.push(['_setAccount', 'UA-7333765-1']);
//_gaq.push(['_setAccount', 'UA-36797757-1']); // Debug code
_gaq.push(['_setDomainName', 'none']); // Change for the real domain
this.initialize_custom();
instance.client.on('state_pushed', this, on_state_pushed);
instance.client.on('state_pushed', this, this.on_state_pushed);
this.include_tracker();
},
initialize_custom: function() {},
on_state_pushed: function() {},
include_tracker: function() {},
});
var init_tracker = function() {
// initialize tracker
_gaq.push(['_setAccount', 'UA-35793871-1']);
// _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
/*
* 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
*/
initialize_custom: function() {
// Track User Access Level, Custom Variable 4 in GA with visitor level scope
// Values: 'Admin User', 'Normal User', 'Portal User', 'Anonymous User'
if (instance.session.uid === 1) {
_gaq.push(['_setCustomVar', 1, 'User Type', 'Admin User', 1]);
_gaq.push(['_setCustomVar', 4, 'User Type', 'Admin User', 1]);
// Make the difference between portal users and anonymous users
} else if (instance.session.username.indexOf('@') !== -1) {
if (instance.session.username.indexOf('anonymous') === -1) {
_gaq.push(['_setCustomVar', 4, 'User Type', 'Portal User', 1]);
} else {
_gaq.push(['_setCustomVar', 1, 'User Type', 'Normal User', 1]);
_gaq.push(['_setCustomVar', 4, 'User Type', 'Anonymous User', 1]);
}
};
var on_state_pushed = function(state) {
} else if (instance.session.username.indexOf('anonymous') !== -1) {
_gaq.push(['_setCustomVar', 4, 'User Type', 'Anonymous User', 1]);
} else {
_gaq.push(['_setCustomVar', 4, 'User Type', 'Normal User', 1]);
}
},
/*
* This method contains the initialization of the object and view type
* custom variables stored in GA. Setting here the CV will work if web_analytics
* module is auto-install, because the first view of a fresh DB is the modules
* kanban view (also in trial), otherwise the responsibility of the first
* initialization relies on instance.web.ActionManager#ir_actions_client
* method
*/
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) {
@ -63,12 +76,14 @@ openerp.web_analytics = function(instance) {
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
},
/*
* This method includes the tracker into view and managers. It can be overriden
* by other modules in order to extend tracking functionalities
*/
include_tracker: function() {
// Track the events related with the creation and the modification of records,
// the view type is always form
instance.web.FormView = instance.web.FormView.extend({
init: function(parent, dataset, view_id, options) {
this._super.apply(this, arguments);
@ -84,11 +99,16 @@ openerp.web_analytics = function(instance) {
}
});
// Track client actions
// Track client actions, also if not in a fresh DB it initializes the
// CV related to objects and view types
instance.web.ActionManager.include({
ir_actions_client: function (action, options) {
console.log(action);
console.log(options);
if (action.res_model) {
_gaq.push(['_setCustomVar', 2, 'Object', action.res_model, 3]);
} else {
_gaq.push(['_setCustomVar', 2, 'Object', action.type, 3]);
}
_gaq.push(['_setCustomVar', 2, 'View Type', action.name, 3]);
var url = instance.web_analytics.generateUrl({'action': action.tag});
_gaq.push(['_trackPageview', url]);
return this._super.apply(this, arguments);
@ -132,31 +152,20 @@ openerp.web_analytics = function(instance) {
this._super.apply(this, arguments);
},
});
};
},
});
// Set correctly the tracker in the current instance
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();
instance.webclient.tracker = new instance.web_analytics.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);
var d = this._super.apply(this, arguments);
this.tracker = new instance.web_analytics.Tracker();
return d;
},
start: function() {
var self = this;
return this._super.apply(this, arguments).done(function() {
self.on('state_pushed', self, on_state_pushed);
include_tracker();
});
}
});
}