UNPKG

6.99 kBJavaScriptView Raw
1/**
2 * Mixin with profile-related helpers (for people and groups)
3 */
4
5const util = require('util');
6const {ensure_timestamp} = require('./utils');
7
8function merge_modifiers(data, modifiers) {
9 if (modifiers) {
10 if (modifiers.$ignore_alias) {
11 data.$ignore_alias = modifiers.$ignore_alias;
12 }
13 if (modifiers.$ignore_time) {
14 data.$ignore_time = modifiers.$ignore_time;
15 }
16 if (modifiers.hasOwnProperty("$ip")) {
17 data.$ip = modifiers.$ip;
18 }
19 if (modifiers.hasOwnProperty("$time")) {
20 data.$time = ensure_timestamp(modifiers.$time);
21 }
22 if (modifiers.hasOwnProperty("$latitude") && modifiers.hasOwnProperty('$longitude')) {
23 data.$latitude = modifiers.$latitude;
24 data.$longitude = modifiers.$longitude;
25 }
26 }
27 return data;
28};
29exports.merge_modifiers = merge_modifiers;
30
31exports.ProfileHelpers = (Base = Object) => class extends Base {
32 get token() {
33 return this.mixpanel.token;
34 }
35
36 get config() {
37 return this.mixpanel.config;
38 }
39
40 _set(prop, to, modifiers, callback, {identifiers, set_once = false}) {
41 let $set = {};
42
43 if (typeof(prop) === 'object') {
44 if (typeof(to) === 'object') {
45 callback = modifiers;
46 modifiers = to;
47 } else {
48 callback = to;
49 }
50 $set = prop;
51 } else {
52 $set[prop] = to;
53 if (typeof(modifiers) === 'function' || !modifiers) {
54 callback = modifiers;
55 }
56 }
57
58 let data = {
59 '$token': this.token,
60 ...identifiers,
61 };
62
63 const set_key = set_once ? "$set_once" : "$set";
64 data[set_key] = $set;
65
66 if ('ip' in $set) {
67 data.$ip = $set.ip;
68 delete $set.ip;
69 }
70
71 if ($set.$ignore_time) {
72 data.$ignore_time = $set.$ignore_time;
73 delete $set.$ignore_time;
74 }
75
76 data = merge_modifiers(data, modifiers);
77
78 if (this.config.debug) {
79 console.log(`Sending the following data to Mixpanel (${this.endpoint}):`);
80 console.log(data);
81 }
82
83 this.mixpanel.send_request({ method: "GET", endpoint: this.endpoint, data }, callback);
84 }
85
86 _delete_profile({identifiers, modifiers, callback}){
87 let data = {
88 '$delete': '',
89 '$token': this.token,
90 ...identifiers,
91 };
92
93 if (typeof(modifiers) === 'function') { callback = modifiers; }
94
95 data = merge_modifiers(data, modifiers);
96
97 if (this.config.debug) {
98 console.log(`Deleting profile ${JSON.stringify(identifiers)}`);
99 }
100
101 this.mixpanel.send_request({ method: "GET", endpoint: this.endpoint, data }, callback);
102 }
103
104 _remove({identifiers, data, modifiers, callback}) {
105 let $remove = {};
106
107 if (typeof(data) !== 'object' || util.isArray(data)) {
108 if (this.config.debug) {
109 console.error("Invalid value passed to #remove - data must be an object with scalar values");
110 }
111 return;
112 }
113
114 for (const [key, val] of Object.entries(data)) {
115 if (typeof(val) === 'string' || typeof(val) === 'number') {
116 $remove[key] = val;
117 } else {
118 if (this.config.debug) {
119 console.error("Invalid argument passed to #remove - values must be scalar");
120 console.error("Passed " + key + ':', val);
121 }
122 return;
123 }
124 }
125
126 if (Object.keys($remove).length === 0) {
127 return;
128 }
129
130 data = {
131 '$remove': $remove,
132 '$token': this.token,
133 ...identifiers
134 };
135
136 if (typeof(modifiers) === 'function') {
137 callback = modifiers;
138 }
139
140 data = merge_modifiers(data, modifiers);
141
142 if (this.config.debug) {
143 console.log(`Sending the following data to Mixpanel (${this.endpoint}):`);
144 console.log(data);
145 }
146
147 this.mixpanel.send_request({ method: "GET", endpoint: this.endpoint, data }, callback);
148 }
149
150 _union({identifiers, data, modifiers, callback}) {
151 let $union = {};
152
153 if (typeof(data) !== 'object' || util.isArray(data)) {
154 if (this.config.debug) {
155 console.error("Invalid value passed to #union - data must be an object with scalar or array values");
156 }
157 return;
158 }
159
160 for (const [key, val] of Object.entries(data)) {
161 if (util.isArray(val)) {
162 var merge_values = val.filter(function(v) {
163 return typeof(v) === 'string' || typeof(v) === 'number';
164 });
165 if (merge_values.length > 0) {
166 $union[key] = merge_values;
167 }
168 } else if (typeof(val) === 'string' || typeof(val) === 'number') {
169 $union[key] = [val];
170 } else {
171 if (this.config.debug) {
172 console.error("Invalid argument passed to #union - values must be a scalar value or array");
173 console.error("Passed " + key + ':', val);
174 }
175 }
176 }
177
178 if (Object.keys($union).length === 0) {
179 return;
180 }
181
182 data = {
183 '$union': $union,
184 '$token': this.token,
185 ...identifiers,
186 };
187
188 if (typeof(modifiers) === 'function') {
189 callback = modifiers;
190 }
191
192 data = merge_modifiers(data, modifiers);
193
194 if (this.config.debug) {
195 console.log(`Sending the following data to Mixpanel (${this.endpoint}):`);
196 console.log(data);
197 }
198
199 this.mixpanel.send_request({ method: "GET", endpoint: this.endpoint, data }, callback);
200 }
201
202 _unset({identifiers, prop, modifiers, callback}){
203 let $unset = [];
204
205 if (util.isArray(prop)) {
206 $unset = prop;
207 } else if (typeof(prop) === 'string') {
208 $unset = [prop];
209 } else {
210 if (this.config.debug) {
211 console.error("Invalid argument passed to #unset - must be a string or array");
212 console.error("Passed: " + prop);
213 }
214 return;
215 }
216
217 let data = {
218 '$unset': $unset,
219 '$token': this.token,
220 ...identifiers,
221 };
222
223 if (typeof(modifiers) === 'function') {
224 callback = modifiers;
225 }
226
227 data = merge_modifiers(data, modifiers);
228
229 if (this.config.debug) {
230 console.log(`Sending the following data to Mixpanel (${this.endpoint}):`);
231 console.log(data);
232 }
233
234 this.mixpanel.send_request({ method: "GET", endpoint: this.endpoint, data }, callback);
235 }
236};