-
Notifications
You must be signed in to change notification settings - Fork 0
/
osjs.js
191 lines (174 loc) · 4.63 KB
/
osjs.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//osjs main class
function Osjs(){
this.write_key = null;
this.anonymousId = null;
this.userId = null;
this.datas = [];
var self = this;
//options
this.setOptions = function(options){
if(options.write_key){
self.write_key = options.write_key;
}
if(options.anonymousId){
//if an anon id is passed, we save it
self.anonymousId = options.anonymousId;
}
else if(!self.anonymousId){
//no anon id, we generate one in case we need it.
self.anonymousId = Math.floor(Math.random() * 0xfffffffffffff);
}
if(options.userId && !self.userId){
self.userId = options.userId;
}
}
//xhr func
this.xhr = function(url, data){
//we add timestamp to data, if there is not one already present
if(!data.timestamp){
var d = new Date();
var t = d.toISOString(); //segment need timestamp at ISO format
data.timestamp = t;
}
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
console.log(JSON.stringify(data, null,4));
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status == 200){
//success, we can send the queue if there is one
self.sendQueue();
}
else{
//error => queue
self.queue(url, data);
}
}
}
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Authorization", "Basic " + btoa(self.write_key + ":" + null));
xmlhttp.setRequestHeader("Content-type","application/json");
//xmlhttp.setRequestHeader("Content-length", data.length);
xmlhttp.send(JSON.stringify(data));
}
this.queue = function(url, data){
var a = url.split("/").pop(); //we get the action name from the url
//we merge the action into the data, so that we can use import
data.action = a;
self.datas.push(data);
}
this.sendQueue = function(){
if(self.datas.length !== 0){
//we have some stored data => we send it
//we create the data with the queue
var data = {
"batch":self.datas
};
//we empty the queue
self.datas = [];
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status == 200){
//it worked. nothing to do.
}
else{
//it didnt work. higly improbable. but we repopulate the queue
for(var i=0;i<data.batch.length;i++){
self.datas.push(data.batch[i]);
}
}
}
}
xmlhttp.open("POST", "https://api.segment.io/v1/import", true);
xmlhttp.setRequestHeader("Authorization", "Basic " + btoa(self.write_key + ":" + null));
xmlhttp.setRequestHeader("Content-type","application/json");
//xmlhttp.setRequestHeader("Content-length", data.length);
xmlhttp.send(JSON.stringify(data));
}
else{
//queue empty. we do nothing
}
}
//identify
this.identify = function(data){
if(!data.anonymousId){
data.anonymousId = self.anonymousId;
}
if(!data.userId && self.userId){
data.userId = self.userId;
}
self.xhr("https://api.segment.io/v1/identify", data);
}
//group
this.group = function(data){
if(!data.anonymousId){
data.anonymousId = self.anonymousId;
}
if(!data.userId && self.userId){
data.userId = self.userId;
}
self.xhr("https://api.segment.io/v1/group", data);
}
//track
this.track = function(data){
if(!data.anonymousId){
data.anonymousId = self.anonymousId;
}
if(!data.userId && self.userId){
data.userId = self.userId;
}
self.xhr("https://api.segment.io/v1/track", data);
}
//page
this.page = function(data){
if(!data.anonymousId){
data.anonymousId = self.anonymousId;
}
if(!data.userId && self.userId){
data.userId = self.userId;
}
self.xhr("https://api.segment.io/v1/page", data);
}
//screen
this.screen = function(data){
if(!data.anonymousId){
data.anonymousId = self.anonymousId;
}
if(!data.userId && self.userId){
data.userId = self.userId;
}
self.xhr("https://api.segment.io/v1/screen", data);
}
//alias
this.alias = function(data){
if(!data.userId && self.userId){
data.userId = self.userId;
}
if(!data.previousId && self.anonymousId){
//we link userId to anonId if no previous Id set
data.previousId = self.anonymousId;
}
self.xhr("https://api.segment.io/v1/alias", data);
}
//import to make compatible with queue system
/*this.import = function(data){
self.xhr("https://api.segment.io/v1/import", data);
}*/
}
var osjs = new Osjs();