-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (56 loc) · 1.64 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
// index.js
const { merge } = require('webpack-merge');
const config = require('./config.js');
let identifierParams = {};
const {
guid,
getCookie,
setCookie,
sendRequest,
generateRequestUrl,
} = require('./util.js');
function initialize(inputApiKey, inputHostname) {
config.apiKey = inputApiKey;
config.hostname = inputHostname;
}
function track(eventName, obj = {}) {
if (!config.apiKey) {
throw new Error('You must initialize the module with an API key before tracking an event.');
}
// merge identifier params with event params
obj = { ...identifierParams, ...obj };
// generate request string
const requestUrl = generateRequestUrl(
config.protocol,
config.hostname,
config.apiKey,
eventName,
obj,
)
sendRequest(requestUrl);
}
function identify(userProperties = {}) {
if (!config.apiKey) {
throw new Error('You must initialize the module with an API key before identifying a user.');
}
// remember only the most important identifier
identifierParams = {};
if (userProperties.email) {
identifierParams.email = userProperties.email;
} else if (userProperties.customer_id) {
identifierParams.customer_id = userProperties.customer_id;
} else if (userProperties.device_id) {
identifierParams.device_id = userProperties.device_id;
} else if (userProperties.phone_number) {
identifierParams.phone_number = userProperties.phone_number;
} else if (userProperties.user_uuid) {
identifierParams.user_uuid = userProperties.user_uuid;
}
track("identify", { ...userProperties });
}
// Expose initialize, track, and identify
module.exports = {
initialize,
track,
identify,
};