Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analytics #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/components/MainView/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/Routes/PrivateRoutes.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Switch>
<Route path="/test" component={TestView} />
Expand Down
77 changes: 77 additions & 0 deletions frontend/src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -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",
ElisarEisenbach marked this conversation as resolved.
Show resolved Hide resolved
headers: {
"Content-Type": "application/json",
"api-key": "[email protected]",
},
redirect: "follow",
referrerPolicy: "no-referrer" as ReferrerPolicy,
}


function createBody(
segmentCallType: string,
params: { [p: string]: unknown },
eventName: string | undefined
): Record<string, unknown> {
const data: Record<string, unknown> = { 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<Response | undefined> {
const body = createBody(eventType, data, eventName);
return fetch(`https://api.komodor.com/analytics/segment/${eventType}`, {
...BASE_ANALYTIC_MSG,
body: JSON.stringify(body),
});
}





ElisarEisenbach marked this conversation as resolved.
Show resolved Hide resolved

const getUserId = (() => {
let userId: string = ''
return () => {
if (!userId) {
userId = randomUUID();
}
return userId;
};
})();