-
Notifications
You must be signed in to change notification settings - Fork 107
/
index.js
144 lines (120 loc) · 3.35 KB
/
index.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
import {Platform, Dimensions} from 'react-native';
import {Constants} from 'expo';
import {Buffer} from 'buffer';
const {width, height} = Dimensions.get('window');
const MIXPANEL_API_URL = 'http://api.mixpanel.com';
const isIosPlatform = Platform.OS === 'ios';
export default class ExpoMixpanelAnalytics {
constructor(token) {
this.ready = false;
this.queue = [];
this.token = token;
this.userId = null;
this.clientId = Constants.deviceId;
this.identify(this.clientId);
Constants.getWebViewUserAgentAsync()
.then(userAgent => {
this.userAgent = userAgent;
this.appName = Constants.manifest.name;
this.appId = Constants.manifest.slug;
this.appVersion = Constants.manifest.version;
this.screenSize = `${width}x${height}`;
this.deviceName = Constants.deviceName;
if (isIosPlatform) {
this.platform = Constants.platform.ios.platform;
this.model = Constants.platform.ios.model;
this.osVersion = Constants.platform.ios.systemVersion;
} else {
this.platform = "android";
}
this.ready = true;
this._flush();
});
}
track(name, props) {
this.queue.push({
name,
props
});
this._flush();
}
identify(userId) {
this.userId = userId;
}
reset() {
this.identify(this.clientId);
}
people_set(props) {
this._people('set', props);
}
people_set_once(props) {
this._people('set_once', props);
}
people_unset(props) {
this._people('unset', props);
}
people_increment(props) {
this._people('add', props);
}
people_append(props) {
this._people('append', props);
}
people_union(props) {
this._people('union', props);
}
people_delete_user() {
this._people('delete', '');
}
// ===========================================================================================
_flush() {
if (this.ready) {
while (this.queue.length) {
const event = this.queue.pop();
this._pushEvent(event)
.then(() => event.sent = true);
}
}
}
_people(operation, props) {
if (this.userId) {
const data = {
"$token": this.token,
"$distinct_id": this.userId
};
data[`$${operation}`] = props;
this._pushProfile(data);
}
}
_pushEvent(event) {
let data = {
event: event.name,
properties: event.props
};
if (this.userId) {
data.properties.distinct_id = this.userId;
}
data.properties.token = this.token;
data.properties.user_agent = this.userAgent;
data.properties.app_name = this.appName;
data.properties.app_id = this.appId;
data.properties.app_version = this.appVersion;
data.properties.screen_size = this.screenSize;
data.properties.client_id = this.clientId;
data.properties.device_name = this.deviceName;
if (this.platform) {
data.properties.platform = this.platform;
}
if (this.model) {
data.properties.model = this.model;
}
if (this.osVersion) {
data.properties.os_version = this.osVersion;
}
data = new Buffer(JSON.stringify(data)).toString('base64');
return fetch(`${MIXPANEL_API_URL}/track/?data=${data}`);
}
_pushProfile(data) {
data = new Buffer(JSON.stringify(data)).toString('base64');
return fetch(`${MIXPANEL_API_URL}/engage/?data=${data}`);
}
}