-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (78 loc) · 2.65 KB
/
index.js
File metadata and controls
95 lines (78 loc) · 2.65 KB
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
/**
* Created by Brad Hanebury on 2016-10-20.
* @copyright Mobials Inc. 2016
*/
'use strict';
var https = require('https');
//the endpoint where all analytics is to be sent
var DISPATCH_URI = 'https://api.mobials.com/tracker/dispatch';
var DISPATCH_HOST = 'api.mobials.com';
var DISPATCH_PATH = '/tracker/dispatch';
var analytics = {};
//we need to fetch our global analytics variable from wherever this was called.
(function (global){
analytics = global.analytics ? global.analytics : {};
analytics.queue = analytics.queue ? analytics.queue : [];
analytics.debug = typeof analytics.debug === 'undefined' ? false : analytics.debug;
analytics.dispatch_uri = analytics.dispatch_uri ? analytics.dispatch_uri : DISPATCH_URI;
analytics.host = analytics.host ? analytics.host : DISPATCH_HOST;
analytics.path = analytics.path ? analytics.path : DISPATCH_PATH;
analytics.SDKClientKey = analytics.SDKClientKey ? analytics.SDKClientKey : null;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {});
/**
* Send our tracking event to the server
*
* @param eventType the name of the event (e.g., 'impression')
* @param payload any relevant data for the event
*/
analytics.track = function(eventType, payload) {
var postData = JSON.stringify({
event: eventType,
payload: [payload]
});
var request = https.request({
host: DISPATCH_HOST,
path: DISPATCH_PATH + '?q=' + encodeURIComponent(postData),
method: 'GET',
withCredentials: false
}, function(res) {
if (analytics.debug === true) {
console.log(res.body);
console.log(res.statusCode);
}
});
request.end();
};
/**
*
* @param eventType the name of the event
* @param payload an array of event payload objects
*/
analytics.trackBatch = function(eventType, payload) {
var postData = JSON.stringify({
event: eventType,
payload: payload
});
var request = https.request({
host: DISPATCH_HOST,
path: DISPATCH_PATH + '?q=' + encodeURIComponent(postData),
method: 'GET',
withCredentials: false
}, function(res) {
if (analytics.debug === true) {
console.log(res.body);
console.log(res.statusCode);
}
});
request.end();
};
/**
* Once everything is setup, we check the queue that was passed in order to see
* if there's anything to do
*/
if (analytics.queue.length > 0) {
for (var i in analytics.queue) {
analytics.track(analytics.queue[i].event, analytics.queue[i].payload);
}
}
module.exports = analytics;