Skip to content

Commit 76e4fe5

Browse files
Refactoring analytics (#311)
* exampple * send call type in function * remove heap check --------- Co-authored-by: itielshwartz <[email protected]>
1 parent 95ea5e4 commit 76e4fe5

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pkg/dashboard/static/analytics.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
const xhr = new XMLHttpRequest();
2-
3-
2+
const TRACK_EVENT_TYPE = "track"
3+
const IDENTIFY_EVENT_TYPE = "identify"
4+
const BASE_ANALYTIC_MSG = {
5+
method: "POST",
6+
mode: "cors",
7+
cache: "no-cache",
8+
headers: {
9+
"Content-Type": "application/json",
10+
"api-key": "[email protected]",
11+
},
12+
redirect: "follow",
13+
referrerPolicy: "no-referrer"
14+
}
415
xhr.onload = function () {
516

617
if (xhr.readyState === XMLHttpRequest.DONE) {
@@ -75,44 +86,31 @@ function sendStats(name, prop) {
7586
}
7687

7788
function enableSegmentBackend(version, ClusterMode) {
78-
sendToSegmentThroughAPI("helm dashboard loaded", {version, 'installationMode': ClusterMode ? "cluster" : "local"})
89+
sendToSegmentThroughAPI("helm dashboard loaded", {version, 'installationMode': ClusterMode ? "cluster" : "local"}, TRACK_EVENT_TYPE)
7990
}
8091

81-
function sendToSegmentThroughAPI(eventName, properties) {
82-
if (window.heap) {
92+
function sendToSegmentThroughAPI(eventName, properties, segmentCallType) {
8393
const userId = getUserId();
8494
try {
85-
sendData(properties, "track", userId, eventName);
95+
sendData(properties, segmentCallType, userId, eventName);
8696
} catch (e) {
8797
console.log("failed sending data to segment", e);
8898
}
89-
}
9099
}
91100

92101
function sendData(data, eventType, userId, eventName) {
93102
const body = createBody(eventType, userId, data, eventName);
94-
ANALYTICS_ADMIN_USER_EMAIL = "[email protected]"
95-
const auth_skipper = ANALYTICS_ADMIN_USER_EMAIL;
96103
return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, {
97-
method: "POST",
98-
mode: "cors",
99-
cache: "no-cache",
100-
//credentials: "include",
101-
headers: {
102-
"Content-Type": "application/json",
103-
"api-key": auth_skipper,
104-
},
105-
redirect: "follow",
106-
referrerPolicy: "no-referrer",
104+
...BASE_ANALYTIC_MSG,
107105
body: JSON.stringify(body),
108106
});
109107
}
110108

111109
function createBody(segmentCallType, userId, params, eventName) {
112110
const data = {userId: userId};
113-
if (segmentCallType === "identify") {
111+
if (segmentCallType === IDENTIFY_EVENT_TYPE) {
114112
data["traits"] = params;
115-
} else if (segmentCallType === "track") {
113+
} else if (segmentCallType === TRACK_EVENT_TYPE) {
116114
if (!eventName) {
117115
throw new Error("no eventName parameter on segment track call");
118116
}

0 commit comments

Comments
 (0)