Skip to content

Commit

Permalink
Refactoring analytics (#311)
Browse files Browse the repository at this point in the history
* exampple

* send call type in function

* remove heap check

---------

Co-authored-by: itielshwartz <[email protected]>
  • Loading branch information
ElisarEisenbach and itielshwartz authored Mar 30, 2023
1 parent 95ea5e4 commit 76e4fe5
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions pkg/dashboard/static/analytics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
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": "[email protected]",
},
redirect: "follow",
referrerPolicy: "no-referrer"
}
xhr.onload = function () {

if (xhr.readyState === XMLHttpRequest.DONE) {
Expand Down Expand Up @@ -75,44 +86,31 @@ function sendStats(name, prop) {
}

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) {
if (window.heap) {
function sendToSegmentThroughAPI(eventName, properties, segmentCallType) {
const userId = getUserId();
try {
sendData(properties, "track", userId, eventName);
sendData(properties, segmentCallType, userId, eventName);
} catch (e) {
console.log("failed sending data to segment", e);
}
}
}

function sendData(data, eventType, userId, eventName) {
const body = createBody(eventType, userId, data, eventName);
ANALYTICS_ADMIN_USER_EMAIL = "[email protected]"
const auth_skipper = ANALYTICS_ADMIN_USER_EMAIL;
return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, {
method: "POST",
mode: "cors",
cache: "no-cache",
//credentials: "include",
headers: {
"Content-Type": "application/json",
"api-key": auth_skipper,
},
redirect: "follow",
referrerPolicy: "no-referrer",
...BASE_ANALYTIC_MSG,
body: JSON.stringify(body),
});
}

function createBody(segmentCallType, userId, params, eventName) {
const data = {userId: userId};
if (segmentCallType === "identify") {
if (segmentCallType === IDENTIFY_EVENT_TYPE) {
data["traits"] = params;
} else if (segmentCallType === "track") {
} else if (segmentCallType === TRACK_EVENT_TYPE) {
if (!eventName) {
throw new Error("no eventName parameter on segment track call");
}
Expand Down

0 comments on commit 76e4fe5

Please sign in to comment.