Refactoring analytics (#311)

* exampple

* send call type in function

* remove heap check

---------

Co-authored-by: itielshwartz <itielshw@gmail.com>
This commit is contained in:
ElisarEisenbach
2023-03-30 14:42:35 +03:00
committed by GitHub
parent 95ea5e4d6d
commit 76e4fe51b5

View File

@@ -1,6 +1,17 @@
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const TRACK_EVENT_TYPE = "track"
const IDENTIFY_EVENT_TYPE = "identify"
const BASE_ANALYTIC_MSG = {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"api-key": "komodor.analytics@admin.com",
},
redirect: "follow",
referrerPolicy: "no-referrer"
}
xhr.onload = function () { xhr.onload = function () {
if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.readyState === XMLHttpRequest.DONE) {
@@ -75,44 +86,31 @@ function sendStats(name, prop) {
} }
function enableSegmentBackend(version, ClusterMode) { function enableSegmentBackend(version, ClusterMode) {
sendToSegmentThroughAPI("helm dashboard loaded", {version, 'installationMode': ClusterMode ? "cluster" : "local"}) sendToSegmentThroughAPI("helm dashboard loaded", {version, 'installationMode': ClusterMode ? "cluster" : "local"}, TRACK_EVENT_TYPE)
} }
function sendToSegmentThroughAPI(eventName, properties) { function sendToSegmentThroughAPI(eventName, properties, segmentCallType) {
if (window.heap) {
const userId = getUserId(); const userId = getUserId();
try { try {
sendData(properties, "track", userId, eventName); sendData(properties, segmentCallType, userId, eventName);
} catch (e) { } catch (e) {
console.log("failed sending data to segment", e); console.log("failed sending data to segment", e);
} }
}
} }
function sendData(data, eventType, userId, eventName) { function sendData(data, eventType, userId, eventName) {
const body = createBody(eventType, userId, data, eventName); const body = createBody(eventType, userId, data, eventName);
ANALYTICS_ADMIN_USER_EMAIL = "komodor.analytics@admin.com"
const auth_skipper = ANALYTICS_ADMIN_USER_EMAIL;
return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, { return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, {
method: "POST", ...BASE_ANALYTIC_MSG,
mode: "cors",
cache: "no-cache",
//credentials: "include",
headers: {
"Content-Type": "application/json",
"api-key": auth_skipper,
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
} }
function createBody(segmentCallType, userId, params, eventName) { function createBody(segmentCallType, userId, params, eventName) {
const data = {userId: userId}; const data = {userId: userId};
if (segmentCallType === "identify") { if (segmentCallType === IDENTIFY_EVENT_TYPE) {
data["traits"] = params; data["traits"] = params;
} else if (segmentCallType === "track") { } else if (segmentCallType === TRACK_EVENT_TYPE) {
if (!eventName) { if (!eventName) {
throw new Error("no eventName parameter on segment track call"); throw new Error("no eventName parameter on segment track call");
} }