-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (117 loc) · 4.71 KB
/
index.ts
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
import { validate, generateContext } from "./src/services/SteliosClient";
import { send } from "./src/services/EventsProcessor";
import { appendLocal, setLocal, ifExists, getLocal, removeFromStart, deleteSteliosLocal, initStorage } from "./src/services/StelioLocalStore"
import { EventPayload } from './src/models/eventPayload'
import { v4 as uuidv4 } from 'uuid';
import { SteliosEvent } from "./src/models/steliosEvent";
import { axiosCreate } from "./src/utility/AxiosUtility";
export var axiosRequest: any = null;
export var eventProcessor: any = null
export var client: any = null
export var apiKey: any = null;
export var stelioLocal: any = null
export var batchExecutorID: any = null
var apiDurationTimer: any = null; // Global variable to time difference between multiple API calls
// to avoid sending same data from multiple tabs
export async function identify(userID: string, traits: any, emitLoginEvent = true) {
const existingIdentity = getLocal('userIdentity');
let newIdentity = {}
if (existingIdentity && Object.keys(existingIdentity).length > 0 ) {
// userIdentity exists
if (existingIdentity.userID && existingIdentity.userID !== userID) {
// userIdentity exists with a different userID, generate new anonymousID and userID
newIdentity = {
anonymousID: uuidv4(),
userID: userID
};
} else {
// userIdentity exists with the same userID, only add userID to existing anonymousID
newIdentity = {
...existingIdentity,
userID: userID // Ensure userID is updated or added without changing anonymousID
};
}
} else {
// userIdentity doesn't exist, create a new one with new anonymousID
newIdentity = {
anonymousID: uuidv4(),
userID: userID
};
}
setLocal("userIdentity", newIdentity);
if(emitLoginEvent) {
sendEvent("user_login", { "event_type": "identity", ...traits });
}
}
export async function reset() {
deleteSteliosLocal('userIdentity');
deleteSteliosLocal('apiKey');
let newIdentity = {
anonymousID: uuidv4()
}
setLocal("userIdentity", newIdentity)
}
export async function initialize(endpoint: string, apiKey: any, flushInterval: number = 15000, apiDurationInterval: number = 5000) {
apiDurationTimer = apiDurationInterval;
initStorage()
axiosCreate(endpoint, apiKey)
if (!batchExecutorID)
batchExecutorID = setInterval(sendBatch, flushInterval)
}
export async function validateClient(apiKey: any) {
return {
"data": {
isTokenValid: "true"
}
}
//return validate( apiKey)
}
export async function sendEvent(eventName: any, props: any) {
if (!ifExists('userIdentity')) {
setLocal('userIdentity', { anonymousID: uuidv4() })
}
if (!eventName || !props) {
throw new Error('Please provide eventName and properties of the user');
}
let payload: SteliosEvent = generateContext(eventName, props);
payload.user_id = getLocal('userIdentity').userID || null;
payload.anonymous_id = getLocal('userIdentity').anonymousID
if (!ifExists('flickEvents')) {
setLocal('flickEvents', new Array(payload))
return;
}
return appendLocal('flickEvents', payload)
}
async function sendBatch() {
// check if apiCallTimestamp is present in localStorage
// if not present, set it to current time epoch
if (!ifExists('apiCallTimestamp')) {
setLocal('apiCallTimestamp', new Date().getTime());
} else {
// if timestamp is present check if apiDurationTimer has elapsed since then, if yes reset it
if (new Date().getTime() - getLocal('apiCallTimestamp') > apiDurationTimer) {
setLocal('apiCallTimestamp', new Date().getTime());
} else {
// if apiDurationTimer has not elapsed, make no api call and return
return;
}
}
if (!ifExists('flickEvents') || !ifExists('userIdentity') || getLocal('flickEvents').length == 0)
return;
let event: EventPayload = {
batch: getLocal('flickEvents'),
sent_at: new Date().toISOString(),
}
let size = getLocal('flickEvents').length || 0;
send(event, {})
.then((res: any) => {
removeFromStart(size, 'flickEvents')
})
.catch(err => {
console.error('error while sending api request ', err)
// if there is continuous failure to send events, then clear events in LRU manner to avoid excess storage of events in browser's local storage
if (size > 1000) {
removeFromStart(size - 1000, 'flickEvents')
}
})
}