From 4cbea8d46ef7104a1ce50f1416e7c7fc8c1ec500 Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Mon, 24 Apr 2023 23:15:42 +0300 Subject: [PATCH 1/4] add analytics.ts --- frontend/src/utils/analytics.ts | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 frontend/src/utils/analytics.ts diff --git a/frontend/src/utils/analytics.ts b/frontend/src/utils/analytics.ts new file mode 100644 index 0000000..8967de2 --- /dev/null +++ b/frontend/src/utils/analytics.ts @@ -0,0 +1,77 @@ +import {randomUUID} from "crypto"; + + +const TRACK_EVENT_TYPE = "track" +const IDENTIFY_EVENT_TYPE = "identify" +const BASE_ANALYTIC_MSG: RequestInit = { + method: "POST", + mode: "cors" as RequestMode, + cache: "no-cache" as RequestCache, + //credentials: "include", + headers: { + "Content-Type": "application/json", + "api-key": "komodor.analytics@admin.com", + }, + redirect: "follow", + referrerPolicy: "no-referrer" as ReferrerPolicy, +} + + +function createBody( + segmentCallType: string, + params: { [p: string]: unknown }, + eventName: string | undefined +): Record { + const data: Record = { userId: getUserId() }; + if (segmentCallType === IDENTIFY_EVENT_TYPE) { + data["traits"] = params; + } else if (segmentCallType === TRACK_EVENT_TYPE) { + if (!eventName) { + throw new Error("no eventName parameter on segment track call"); + } + data["properties"] = params; + data["eventName"] = eventName; + } + return data; +} + + +export default function sendToSegment( + eventName: string, + properties: { [p: string]: unknown }) { + sendToSegmentThroughAPI(eventName, properties); +} + +function sendToSegmentThroughAPI( + eventName: string, + properties: { [p: string]: unknown } +) { + sendData(properties, TRACK_EVENT_TYPE, eventName); +} + +function sendData( + data: { [p: string]: unknown }, + eventType: string, + eventName?: string +): Promise { + const body = createBody(eventType, data, eventName); + return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, { + ...BASE_ANALYTIC_MSG, + body: JSON.stringify(body), + }); +} + + + + + + +const getUserId = (() => { + let userId: string = '' + return () => { + if (!userId) { + userId = randomUUID(); + } + return userId; + }; +})(); \ No newline at end of file From c70524a7c36b162bfce78366b86ab0633cbebadd Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Mon, 8 May 2023 16:43:27 +0300 Subject: [PATCH 2/4] use sendAnalytics function --- frontend/src/components/MainView/index.tsx | 2 +- frontend/src/components/Routes/PrivateRoutes.tsx | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/MainView/index.tsx b/frontend/src/components/MainView/index.tsx index 2d06808..ca4d132 100644 --- a/frontend/src/components/MainView/index.tsx +++ b/frontend/src/components/MainView/index.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useState } from "react"; +import React, {useCallback, useState} from "react"; import styled from "styled-components"; import { callAPI } from "../../services/api-service"; import MainViewHeader from "./MainViewHeader"; diff --git a/frontend/src/components/Routes/PrivateRoutes.tsx b/frontend/src/components/Routes/PrivateRoutes.tsx index 1f7ab90..7ed758b 100644 --- a/frontend/src/components/Routes/PrivateRoutes.tsx +++ b/frontend/src/components/Routes/PrivateRoutes.tsx @@ -1,10 +1,14 @@ -import React from "react"; +import React, {useEffect} from "react"; import { Switch, Route } from "react-router-dom"; import MainView from "../MainView"; import TestView from "../TestView"; +import sendToSegment from "../../utils/analytics"; const PrivateRoutes: React.FC = () => { + useEffect(() => { + sendToSegment("validKubeInitiallized", {}) + }, []) return ( From 9187b8ee1551e51787a2556540226153fa6d82a2 Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Mon, 8 May 2023 16:54:15 +0300 Subject: [PATCH 3/4] formatting --- frontend/src/utils/analytics.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/frontend/src/utils/analytics.ts b/frontend/src/utils/analytics.ts index 8967de2..d795717 100644 --- a/frontend/src/utils/analytics.ts +++ b/frontend/src/utils/analytics.ts @@ -7,7 +7,6 @@ const BASE_ANALYTIC_MSG: RequestInit = { method: "POST", mode: "cors" as RequestMode, cache: "no-cache" as RequestCache, - //credentials: "include", headers: { "Content-Type": "application/json", "api-key": "komodor.analytics@admin.com", @@ -22,7 +21,7 @@ function createBody( params: { [p: string]: unknown }, eventName: string | undefined ): Record { - const data: Record = { userId: getUserId() }; + const data: Record = {userId: getUserId()}; if (segmentCallType === IDENTIFY_EVENT_TYPE) { data["traits"] = params; } else if (segmentCallType === TRACK_EVENT_TYPE) { @@ -61,11 +60,6 @@ function sendData( }); } - - - - - const getUserId = (() => { let userId: string = '' return () => { From aedce96458776d2f047d4a781c722bacfea56000 Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Mon, 8 May 2023 16:55:27 +0300 Subject: [PATCH 4/4] add last empty line to file --- frontend/src/utils/analytics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/utils/analytics.ts b/frontend/src/utils/analytics.ts index d795717..c8cc8bf 100644 --- a/frontend/src/utils/analytics.ts +++ b/frontend/src/utils/analytics.ts @@ -68,4 +68,4 @@ const getUserId = (() => { } return userId; }; -})(); \ No newline at end of file +})();