-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
364 lines (315 loc) · 10.7 KB
/
script.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* If the affiliData variable not defined or not array, create it
*/
if(typeof(affiliData) !== 'array') {
window.affiliData = window.affiliData || [];
}
/**
* If the affili is not function, define it
*/
if(typeof(affili) !== 'function') {
window.affili = function() {
affiliData.push(arguments);
}
}
/**
* Encapsulated function
* The window object is supported by all browsers. It represents the browser's window.
* The accountId is your account id where can be fined in your affili panel, in developers section.
*/
(function(window) {
/**
* Is a cookie object to help create, edit or delete cookies
* based on js-cookie package
* https://github.com/js-cookie/js-cookie
*/
const cookies = {
getCookie(name, def = null) {
let v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)')
return v ? v[2] : def
},
setCookie(name, value, days) {
let date = new Date
date.setTime(date.getTime() + 24*60*60*1000*days)
let siteDomain = location.hostname.split('.').slice(-2).join('.')
document.cookie = name + "=" + value + ";domain=."+ siteDomain +";path=/;expires=" + date.toGMTString()
},
deleteCookie(name) {
this.setCookie(name, '', -1)
}
}
/**
* All supported conversion types
*/
const conversionTypes = {
LEAD: 'lead',
SALE: 'sale',
}
/**
* The request object used to get URL parameters
*/
const request = {
/**
* Return URL parameters as a array
*/
getUrlVars() {
let vars = {}
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value
})
return vars
},
/**
* @param {string} parameter
* @param {string} defaultvalue
*/
getUrlParam(parameter, defaultvalue = null) {
let urlparameter = defaultvalue
if(window.location.href.indexOf(parameter) > -1){
urlparameter = request.getUrlVars()[parameter]
}
return urlparameter
}
}
/**
* The xhr object handle requests to affili server
*/
const xhr = {
/**
* When we need to set a cookie on the client browser
*
* @param {object} data
* @param {function} callback
*/
setCookie(data, callback = function(response){}) {
this.request('POST', this.url('set-cookie'), data, callback)
},
/**
* When we need to track conversion
*
* @param {object} data
* @param {function} callback
*/
saveConversion(data, callback = function(response){}) {
this.request('POST', this.url('conversion'), data, callback)
},
/**
* The Base API url
*
* @param {string} uri
*/
url(uri) {
return 'https://core.affili.ir/api/v2/clients/'+uri
},
/**
* The request method send a request to affili server
*
* @param {string} method
* @param {string} url
* @param {object} data
* @param {function} callback
*/
request(method, url ,data, callback = function(response){}) {
let xhr = "undefined" != typeof XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xhr.withCredentials = true
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4 && this.status === 200) {
let response = JSON.parse(this.response)
callback(response)
}
})
let json = JSON.stringify(data)
xhr.open(method, url)
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8')
xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with')
xhr.send(json)
}
}
/**
* The affiliConst object manage all of traking processes
*/
const affiliConst = {
account_id: false,
aff_id: false,
referrer: false,
token: false,
/**
* To initial affili, The create method must be call
* In this method, we check cookies on the client's browser
*
* @param {string} account_id
* @param {object} options
*/
create(account_id, options = {}, callback = function () {}) {
this.account_id = account_id
this.token = cookies.getCookie('affili_token', false)
this.referrer = cookies.getCookie('affili_referrer', false)
this.aff_id = cookies.getCookie('affili_aff_id', false)
callback()
},
/**
* After calling the create method, we detect it to set new cookies
* if the URL has 'aff_id' and 'referrer' parameters
*
* @param {object} options
*/
detect(options = {}, callback = function () {}) {
let that = this
let aff_id = request.getUrlParam('aff_id', false)
let referrer = request.getUrlParam('referrer', false)
// If the URL has not 'aff_id' and 'referrer' return false
if(aff_id === false || referrer === false) {
return false
}
this.aff_id = aff_id
this.referrer = referrer
const defaultDetectOptions = {}
options = Object.assign({}, defaultDetectOptions, options, {
aff_id: this.aff_id,
referrer: this.referrer,
account_id: this.account_id
})
// Set or override cookies
xhr.setCookie(options, function(response) {
that.token = response.data.token
cookies.setCookie('affili_token', that.token, response.data.valid_days)
cookies.setCookie('affili_referrer', that.referrer, response.data.valid_days)
cookies.setCookie('affili_aff_id', that.aff_id, response.data.valid_days)
cookies.setCookie('delete_cookie', response.data.delete_cookie, response.data.valid_days)
})
callback()
},
/**
* Calling The conversion method, When The client order process completed and client reach goal page
*
* @param {string} order_id
* @param {integer} amount
* @param {object} options
* @param {boolean} deleteCookie
*/
conversion(order_id, amount, options = {}, deleteCookie = true, callback = function () {}) {
const defaultData = {
meta_data: null,
coupon: null,
products: null,
type: conversionTypes.SALE,
aff_id: this.aff_id,
referrer: this.referrer,
token: this.token,
}
const data = Object.assign({}, defaultData, options, {
account_id: this.account_id,
order_id: order_id,
amount: amount,
})
if(data.aff_id === false || data.referrer === false) {
return false
}
let that = this
// Save conversion and reset cookies as a default
xhr.saveConversion(data, function(response) {
// As a default we reset cookies
if(['1',1,'true', true].includes(cookies.getCookie('delete_cookie', true))
&& deleteCookie === true
) {
that.reset()
}
})
callback()
},
/**
* Calling the click method, When in your affiliate program you set CPL commissions
*
* @param {object} options
*/
lead(options = {}, callback = function () {}) {
const defaultData = {
meta_data: {},
type: conversionTypes.LEAD,
aff_id: this.aff_id,
referrer: this.referrer,
token: this.token,
}
const data = Object.assign({}, defaultData, options, {
account_id: this.account_id,
})
if(data.aff_id === false || data.referrer === false) {
return false
}
xhr.saveConversion(data)
callback()
},
/**
* Calling the reset method to clear all tokens and settings.
*/
reset(callback = function () {}) {
this.aff_id = false
this.referrer = false
this.token = false
cookies.deleteCookie('affili_token')
cookies.deleteCookie('affili_referrer')
cookies.deleteCookie('affili_aff_id')
cookies.deleteCookie('delete_cookie')
callback()
},
/**
* Calling the log method to print current settings in console
*/
log(callback = function () {}) {
console.log({
'account_id': this.account_id,
'aff_id': this.aff_id,
'referrer': this.referrer,
'token': this.token,
})
callback()
},
__data__(callback = function () {}) {
const data = JSON.parse(JSON.stringify({
aff_id: this.aff_id,
referrer: this.referrer,
token: this.token,
}))
callback(data)
}
}
/**
* The affiliFunc used to make all of the affiliConst methods promise able.
*
* @param {string} method
* @param {complex} params
*/
let affiliFunc = function(method, params) {
return new Promise(function(resolve, reject) {
try {
let result = affiliConst[method](...params)
resolve(result)
} catch(e) {
reject(e)
}
})
}
/**
* As a simple way, At first, we push all functions to affiliData array then we call them after
* script loaded.
*/
affiliData.forEach(function(arg) {
var slicedArgs = Array.prototype.slice.call(arg, 1);
affiliFunc(arg[0], slicedArgs)
})
let eventify = function(arr, callback) {
arr.push = function(e) {
Array.prototype.push.call(arr, e)
callback(arr)
}
}
/**
* Make all affiliConst methods async
*/
eventify(affiliData, function(args) {
args.forEach(function(arg) {
var slicedArgs = Array.prototype.slice.call(arg, 1);
affiliFunc(arg[0], slicedArgs)
})
})
})(window)