mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-03 00:09:41 +00:00
Wrap service in anonymous function; put constants at top of file
This commit is contained in:
parent
fbc466cb29
commit
34c08e941f
1 changed files with 108 additions and 107 deletions
|
|
@ -1,113 +1,114 @@
|
|||
'use strict';
|
||||
|
||||
angular
|
||||
.module('habitrpg')
|
||||
.factory('Analytics', analyticsFactory);
|
||||
|
||||
analyticsFactory.$inject = [
|
||||
'User'
|
||||
];
|
||||
|
||||
function analyticsFactory(User) {
|
||||
|
||||
var user = User.user;
|
||||
|
||||
// Amplitude
|
||||
var r = window.amplitude || {};
|
||||
r._q = [];
|
||||
function a(window) {r[window] = function() {r._q.push([window].concat(Array.prototype.slice.call(arguments, 0)));}}
|
||||
var i = ["init", "logEvent", "logRevenue", "setUserId", "setUserProperties", "setOptOut", "setVersionName", "setDomain", "setDeviceId", "setGlobalUserProperties"];
|
||||
for (var o = 0; o < i.length; o++) {a(i[o])}
|
||||
window.amplitude = r;
|
||||
amplitude.init(window.env.AMPLITUDE_KEY);
|
||||
|
||||
// Google Analytics (aka Universal Analytics)
|
||||
window['GoogleAnalyticsObject'] = 'ga';
|
||||
window['ga'] = window['ga'] || function() {
|
||||
(window['ga'].q = window['ga'].q || []).push(arguments)
|
||||
}, window['ga'].l = 1 * new Date();
|
||||
ga('create', window.env.GA_ID, 'auto');
|
||||
|
||||
function loadScripts() {
|
||||
// Amplitude
|
||||
var n = document.createElement("script");
|
||||
var s = document.getElementsByTagName("script")[0];
|
||||
n.type = "text/javascript";
|
||||
n.async = true;
|
||||
n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js";
|
||||
s.parentNode.insertBefore(n, s);
|
||||
|
||||
// Google Analytics
|
||||
var a = document.createElement('script');
|
||||
var m = document.getElementsByTagName('script')[0];
|
||||
a.async = 1;
|
||||
a.src = '//www.google-analytics.com/analytics.js';
|
||||
m.parentNode.insertBefore(a, m);
|
||||
}
|
||||
|
||||
function register() {
|
||||
amplitude.setUserId(user._id);
|
||||
ga('set', {'userId':user._id});
|
||||
}
|
||||
|
||||
function login() {
|
||||
amplitude.setUserId(user._id);
|
||||
ga('set', {'userId':user._id});
|
||||
}
|
||||
|
||||
function track(properties) {
|
||||
if(_doesNotHaveRequiredFields(properties)) { return false; }
|
||||
if(_doesNotHaveAllowedHitType(properties)) { return false; }
|
||||
|
||||
amplitude.logEvent(properties.eventAction,properties);
|
||||
ga('send',properties);
|
||||
}
|
||||
|
||||
function updateUser(properties) {
|
||||
properties = properties || {};
|
||||
|
||||
_gatherUserStats(user, properties);
|
||||
|
||||
amplitude.setUserProperties(properties);
|
||||
ga('set',properties);
|
||||
}
|
||||
|
||||
return {
|
||||
loadScripts: loadScripts,
|
||||
register: register,
|
||||
login: login,
|
||||
track: track,
|
||||
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) {
|
||||
(function(){
|
||||
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;
|
||||
}
|
||||
}
|
||||
angular
|
||||
.module('habitrpg')
|
||||
.factory('Analytics', analyticsFactory);
|
||||
|
||||
analyticsFactory.$inject = [
|
||||
'User'
|
||||
];
|
||||
|
||||
function analyticsFactory(User) {
|
||||
|
||||
var user = User.user;
|
||||
|
||||
// Amplitude
|
||||
var r = window.amplitude || {};
|
||||
r._q = [];
|
||||
function a(window) {r[window] = function() {r._q.push([window].concat(Array.prototype.slice.call(arguments, 0)));}}
|
||||
var i = ["init", "logEvent", "logRevenue", "setUserId", "setUserProperties", "setOptOut", "setVersionName", "setDomain", "setDeviceId", "setGlobalUserProperties"];
|
||||
for (var o = 0; o < i.length; o++) {a(i[o])}
|
||||
window.amplitude = r;
|
||||
amplitude.init(window.env.AMPLITUDE_KEY);
|
||||
|
||||
// Google Analytics (aka Universal Analytics)
|
||||
window['GoogleAnalyticsObject'] = 'ga';
|
||||
window['ga'] = window['ga'] || function() {
|
||||
(window['ga'].q = window['ga'].q || []).push(arguments)
|
||||
}, window['ga'].l = 1 * new Date();
|
||||
ga('create', window.env.GA_ID, 'auto');
|
||||
|
||||
function loadScripts() {
|
||||
// Amplitude
|
||||
var n = document.createElement("script");
|
||||
var s = document.getElementsByTagName("script")[0];
|
||||
n.type = "text/javascript";
|
||||
n.async = true;
|
||||
n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js";
|
||||
s.parentNode.insertBefore(n, s);
|
||||
|
||||
// Google Analytics
|
||||
var a = document.createElement('script');
|
||||
var m = document.getElementsByTagName('script')[0];
|
||||
a.async = 1;
|
||||
a.src = '//www.google-analytics.com/analytics.js';
|
||||
m.parentNode.insertBefore(a, m);
|
||||
}
|
||||
|
||||
function register() {
|
||||
amplitude.setUserId(user._id);
|
||||
ga('set', {'userId':user._id});
|
||||
}
|
||||
|
||||
function login() {
|
||||
amplitude.setUserId(user._id);
|
||||
ga('set', {'userId':user._id});
|
||||
}
|
||||
|
||||
function track(properties) {
|
||||
if(_doesNotHaveRequiredFields(properties)) { return false; }
|
||||
if(_doesNotHaveAllowedHitType(properties)) { return false; }
|
||||
|
||||
amplitude.logEvent(properties.eventAction,properties);
|
||||
ga('send',properties);
|
||||
}
|
||||
|
||||
function updateUser(properties) {
|
||||
properties = properties || {};
|
||||
|
||||
_gatherUserStats(user, properties);
|
||||
|
||||
amplitude.setUserProperties(properties);
|
||||
ga('set',properties);
|
||||
}
|
||||
|
||||
return {
|
||||
loadScripts: loadScripts,
|
||||
register: register,
|
||||
login: login,
|
||||
track: track,
|
||||
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) {
|
||||
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) {
|
||||
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