mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
New client random catchup (#8891)
* Added initial challenge pages * Added challenge item and find guilds page * Added challenge detail * Added challenge modals * Ported over challenge service code * Ported over challenge ctrl code * Added styles and column * Minor modal updates * Removed duplicate keys * Fixed casing * Added initial chat component * Added copy as todo modal * Added sync * Added chat to groups * Fixed lint * Added notification service * Added tag services * Added notifications * Added hall * Added analytics * Added http interceptor * Added initial autocomplete * Added initial footer component * Began coding and designing footer * Added inital hall * Ported over inital group plan ctrl code * Added initial invite modal * Added initial member detail modal * Added initial notification menu * Ported over inital notification code * Fixed import line * Fixed autocomplete import casing
This commit is contained in:
86
website/client/mixins/analytics.js
Normal file
86
website/client/mixins/analytics.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import keys from 'lodash/keys';
|
||||
import pick from 'lodash/pick';
|
||||
import includes from 'lodash/includes';
|
||||
|
||||
let REQUIRED_FIELDS = ['hitType', 'eventCategory', 'eventAction'];
|
||||
let ALLOWED_HIT_TYPES = [
|
||||
'pageview',
|
||||
'screenview',
|
||||
'event',
|
||||
'transaction',
|
||||
'item',
|
||||
'social',
|
||||
'exception',
|
||||
'timing',
|
||||
];
|
||||
|
||||
|
||||
export default {
|
||||
methods: {
|
||||
register (user) {
|
||||
// @TODO: What is was the timeout for?
|
||||
window.amplitude.setUserId(user._id);
|
||||
window.ga('set', {userId: user._id});
|
||||
},
|
||||
login (user) {
|
||||
window.amplitude.setUserId(user._id);
|
||||
window.ga('set', {userId: user._id});
|
||||
},
|
||||
track (properties) {
|
||||
if (this.doesNotHaveRequiredFields(properties)) return false;
|
||||
if (this.doesNotHaveAllowedHitType(properties)) return false;
|
||||
|
||||
window.amplitude.logEvent(properties.eventAction, properties);
|
||||
window.ga('send', properties);
|
||||
},
|
||||
updateUser (properties, user) {
|
||||
properties = properties || {};
|
||||
|
||||
this.gatherUserStats(user, properties);
|
||||
|
||||
window.amplitude.setUserProperties(properties);
|
||||
window.ga('set', properties);
|
||||
},
|
||||
gatherUserStats (user, properties) {
|
||||
if (user._id) properties.UUID = user._id;
|
||||
if (user.stats) {
|
||||
properties.Class = user.stats.class;
|
||||
properties.Experience = Math.floor(user.stats.exp);
|
||||
properties.Gold = Math.floor(user.stats.gp);
|
||||
properties.Health = Math.ceil(user.stats.hp);
|
||||
properties.Level = user.stats.lvl;
|
||||
properties.Mana = Math.floor(user.stats.mp);
|
||||
}
|
||||
|
||||
properties.balance = user.balance;
|
||||
properties.balanceGemAmount = properties.balance * 4;
|
||||
|
||||
properties.tutorialComplete = user.flags && user.flags.tour && user.flags.tour.intro === -2;
|
||||
if (user.habits && user.dailys && user.todos && user.rewards) {
|
||||
properties['Number Of Tasks'] = {
|
||||
habits: user.habits.length,
|
||||
dailys: user.dailys.length,
|
||||
todos: user.todos.length,
|
||||
rewards: user.rewards.length,
|
||||
};
|
||||
}
|
||||
if (user.contributor && user.contributor.level) properties.contributorLevel = user.contributor.level;
|
||||
if (user.purchased && user.purchased.plan.planId) properties.subscription = user.purchased.plan.planId;
|
||||
},
|
||||
doesNotHaveRequiredFields (properties) {
|
||||
if (!isEqual(keys(pick(properties, REQUIRED_FIELDS)), REQUIRED_FIELDS)) {
|
||||
// @TODO: Log with Winston?
|
||||
// console.log('Analytics tracking calls must include the following properties: ' + JSON.stringify(REQUIRED_FIELDS));
|
||||
return true;
|
||||
}
|
||||
},
|
||||
doesNotHaveAllowedHitType (properties) {
|
||||
if (!includes(ALLOWED_HIT_TYPES, properties.hitType)) {
|
||||
// @TODO: Log with Winston?
|
||||
// console.log('Hit type of Analytics event must be one of the following: ' + JSON.stringify(ALLOWED_HIT_TYPES));
|
||||
return true;
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user