mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-03 00:09:41 +00:00
Create private methods for analytics functions
This commit is contained in:
parent
15c08f091d
commit
fbc466cb29
1 changed files with 34 additions and 21 deletions
|
|
@ -56,21 +56,15 @@ function analyticsFactory(User) {
|
|||
}
|
||||
|
||||
function track(properties) {
|
||||
var REQUIRED_FIELDS = ['hitType','eventCategory','eventAction'];
|
||||
var ALLOWED_HIT_TYPES = ['pageview','screenview','event','transaction','item','social','exception','timing'];
|
||||
if (!_.isEqual(_.keys(_.pick(properties, REQUIRED_FIELDS)), REQUIRED_FIELDS)) {
|
||||
return console.log('Analytics tracking calls must include the following properties: ' + JSON.stringify(REQUIRED_FIELDS));
|
||||
}
|
||||
if (!_.contains(ALLOWED_HIT_TYPES, properties.hitType)) {
|
||||
return console.log('Hit type of Analytics event must be one of the following: ' + JSON.stringify(ALLOWED_HIT_TYPES));
|
||||
}
|
||||
if(_doesNotHaveRequiredFields(properties)) { return false; }
|
||||
if(_doesNotHaveAllowedHitType(properties)) { return false; }
|
||||
|
||||
amplitude.logEvent(properties.eventAction,properties);
|
||||
ga('send',properties);
|
||||
}
|
||||
|
||||
function updateUser(properties) {
|
||||
if (!properties) properties = {};
|
||||
properties = properties || {};
|
||||
|
||||
_gatherUserStats(user, properties);
|
||||
|
||||
|
|
@ -78,18 +72,6 @@ function analyticsFactory(User) {
|
|||
ga('set',properties);
|
||||
}
|
||||
|
||||
function _gatherUserStats(user, properties) {
|
||||
if (user._id) properties.UUID = user._id;
|
||||
if (user.stats.class) properties.Class = user.stats.class;
|
||||
if (user.stats.exp) properties.Experience = Math.floor(user.stats.exp);
|
||||
if (user.stats.gp) properties.Gold = Math.floor(user.stats.gp);
|
||||
if (user.stats.hp) properties.Health = Math.ceil(user.stats.hp);
|
||||
if (user.stats.lvl) properties.Level = user.stats.lvl;
|
||||
if (user.stats.mp) properties.Mana = Math.floor(user.stats.mp);
|
||||
if (user.contributor.level) properties.contributorLevel = user.contributor.level;
|
||||
if (user.purchased.plan.planId) properties.subscription = user.purchased.plan.planId;
|
||||
}
|
||||
|
||||
return {
|
||||
loadScripts: loadScripts,
|
||||
register: register,
|
||||
|
|
@ -98,3 +80,34 @@ function analyticsFactory(User) {
|
|||
updateUser: updateUser
|
||||
};
|
||||
}
|
||||
|
||||
function _gatherUserStats(user, properties) {
|
||||
if (user._id) properties.UUID = user._id;
|
||||
if (user.stats.class) properties.Class = user.stats.class;
|
||||
if (user.stats.exp) properties.Experience = Math.floor(user.stats.exp);
|
||||
if (user.stats.gp) properties.Gold = Math.floor(user.stats.gp);
|
||||
if (user.stats.hp) properties.Health = Math.ceil(user.stats.hp);
|
||||
if (user.stats.lvl) properties.Level = user.stats.lvl;
|
||||
if (user.stats.mp) properties.Mana = Math.floor(user.stats.mp);
|
||||
if (user.contributor.level) properties.contributorLevel = user.contributor.level;
|
||||
if (user.purchased.plan.planId) properties.subscription = user.purchased.plan.planId;
|
||||
}
|
||||
|
||||
function _doesNotHaveRequiredFields(properties) {
|
||||
var REQUIRED_FIELDS = ['hitType','eventCategory','eventAction'];
|
||||
|
||||
if (!_.isEqual(_.keys(_.pick(properties, REQUIRED_FIELDS)), REQUIRED_FIELDS)) {
|
||||
console.log('Analytics tracking calls must include the following properties: ' + JSON.stringify(REQUIRED_FIELDS));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function _doesNotHaveAllowedHitType(properties) {
|
||||
var ALLOWED_HIT_TYPES = ['pageview','screenview','event','transaction','item','social','exception','timing'];
|
||||
|
||||
if (!_.contains(ALLOWED_HIT_TYPES, properties.hitType)) {
|
||||
console.log('Hit type of Analytics event must be one of the following: ' + JSON.stringify(ALLOWED_HIT_TYPES));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue