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 ( diff --git a/frontend/src/utils/analytics.ts b/frontend/src/utils/analytics.ts new file mode 100644 index 0000000..c8cc8bf --- /dev/null +++ b/frontend/src/utils/analytics.ts @@ -0,0 +1,71 @@ +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, + 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; + }; +})();