-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathmixpanel-group.js
174 lines (158 loc) · 5.98 KB
/
mixpanel-group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* eslint camelcase: "off" */
import { addOptOutCheckMixpanelGroup } from './gdpr-utils';
import { apiActions } from './api-actions';
import { _ } from './utils';
/**
* Mixpanel Group Object
* @constructor
*/
var MixpanelGroup = function() {};
_.extend(MixpanelGroup.prototype, apiActions);
MixpanelGroup.prototype._init = function(mixpanel_instance, group_key, group_id) {
this._mixpanel = mixpanel_instance;
this._group_key = group_key;
this._group_id = group_id;
};
/**
* Set properties on a group.
*
* ### Usage:
*
* mixpanel.get_group('company', 'mixpanel').set('Location', '405 Howard');
*
* // or set multiple properties at once
* mixpanel.get_group('company', 'mixpanel').set({
* 'Location': '405 Howard',
* 'Founded' : 2009,
* });
* // properties can be strings, integers, dates, or lists
*
* @param {Object|String} prop If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [to] A value to set on the given property name
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype.set = addOptOutCheckMixpanelGroup(function(prop, to, callback) {
var data = this.set_action(prop, to);
if (_.isObject(prop)) {
callback = to;
}
return this._send_request(data, callback);
});
/**
* Set properties on a group, only if they do not yet exist.
* This will not overwrite previous group property values, unlike
* group.set().
*
* ### Usage:
*
* mixpanel.get_group('company', 'mixpanel').set_once('Location', '405 Howard');
*
* // or set multiple properties at once
* mixpanel.get_group('company', 'mixpanel').set_once({
* 'Location': '405 Howard',
* 'Founded' : 2009,
* });
* // properties can be strings, integers, lists or dates
*
* @param {Object|String} prop If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [to] A value to set on the given property name
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype.set_once = addOptOutCheckMixpanelGroup(function(prop, to, callback) {
var data = this.set_once_action(prop, to);
if (_.isObject(prop)) {
callback = to;
}
return this._send_request(data, callback);
});
/**
* Unset properties on a group permanently.
*
* ### Usage:
*
* mixpanel.get_group('company', 'mixpanel').unset('Founded');
*
* @param {String} prop The name of the property.
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype.unset = addOptOutCheckMixpanelGroup(function(prop, callback) {
var data = this.unset_action(prop);
return this._send_request(data, callback);
});
/**
* Merge a given list with a list-valued group property, excluding duplicate values.
*
* ### Usage:
*
* // merge a value to a list, creating it if needed
* mixpanel.get_group('company', 'mixpanel').union('Location', ['San Francisco', 'London']);
*
* @param {String} list_name Name of the property.
* @param {Array} values Values to merge with the given property
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype.union = addOptOutCheckMixpanelGroup(function(list_name, values, callback) {
if (_.isObject(list_name)) {
callback = values;
}
var data = this.union_action(list_name, values);
return this._send_request(data, callback);
});
/**
* Permanently delete a group.
*
* ### Usage:
*
* mixpanel.get_group('company', 'mixpanel').delete();
*
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype['delete'] = addOptOutCheckMixpanelGroup(function(callback) {
// bracket notation above prevents a minification error related to reserved words
var data = this.delete_action();
return this._send_request(data, callback);
});
/**
* Remove a property from a group. The value will be ignored if doesn't exist.
*
* ### Usage:
*
* mixpanel.get_group('company', 'mixpanel').remove('Location', 'London');
*
* @param {String} list_name Name of the property.
* @param {Object} value Value to remove from the given group property
* @param {Function} [callback] If provided, the callback will be called after the tracking event
*/
MixpanelGroup.prototype.remove = addOptOutCheckMixpanelGroup(function(list_name, value, callback) {
var data = this.remove_action(list_name, value);
return this._send_request(data, callback);
});
MixpanelGroup.prototype._send_request = function(data, callback) {
data['$group_key'] = this._group_key;
data['$group_id'] = this._group_id;
data['$token'] = this._get_config('token');
var date_encoded_data = _.encodeDates(data);
return this._mixpanel._track_or_batch({
type: 'groups',
data: date_encoded_data,
endpoint: this._get_config('api_host') + '/groups/',
batcher: this._mixpanel.request_batchers.groups
}, callback);
};
MixpanelGroup.prototype._is_reserved_property = function(prop) {
return prop === '$group_key' || prop === '$group_id';
};
MixpanelGroup.prototype._get_config = function(conf) {
return this._mixpanel.get_config(conf);
};
MixpanelGroup.prototype.toString = function() {
return this._mixpanel.toString() + '.group.' + this._group_key + '.' + this._group_id;
};
// MixpanelGroup Exports
MixpanelGroup.prototype['remove'] = MixpanelGroup.prototype.remove;
MixpanelGroup.prototype['set'] = MixpanelGroup.prototype.set;
MixpanelGroup.prototype['set_once'] = MixpanelGroup.prototype.set_once;
MixpanelGroup.prototype['union'] = MixpanelGroup.prototype.union;
MixpanelGroup.prototype['unset'] = MixpanelGroup.prototype.unset;
MixpanelGroup.prototype['toString'] = MixpanelGroup.prototype.toString;
export {MixpanelGroup};