Skip to content

Commit

Permalink
adds analytics for checking if front-token has been removed or not
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Jun 22, 2024
1 parent 6216565 commit 5f4fb78
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import { getAnalytics } from "./utils";

export enum HTTP_REQUEST_ERROR {
SESSION_EXPIRED,
Expand Down Expand Up @@ -100,10 +101,11 @@ export async function simpleGETRequest(url: string, userConfig: any = {}, versio
"api-version": version + ""
}
};

let frontTokenExists = cookieExists("sFrontToken");
let response = await axios.get(url, userConfig);
let data = await response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data, headers };
}

Expand All @@ -119,9 +121,11 @@ export async function simplePOSTRequest(url: string, data: any, userConfig: POST
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
let response = await axios.post(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers, status: response.status, statusText: response.statusText };
}

Expand All @@ -137,10 +141,11 @@ export async function simplePATCHRequest(url: string, data: any, userConfig: PAT
"api-version": version + ""
}
};

let frontTokenExists = cookieExists("sFrontToken");
let response = await axios.patch(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers };
}

Expand All @@ -156,10 +161,11 @@ export async function simplePUTRequest(url: string, data: any, userConfig: POSTR
"api-version": version + ""
}
};

let frontTokenExists = cookieExists("sFrontToken");
let response = await axios.put(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers };
}

Expand All @@ -178,10 +184,49 @@ export async function simpleDELETERequest(url: string, userConfig: DELETERequest
"api-version": version + ""
}
};

let frontTokenExists = cookieExists("sFrontToken");
delete userConfig.params;
let response = await axios.delete(url, userConfig);
let data = await response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data, headers };
}

function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: boolean, headers: any) {
if (!frontTokenExists) {
return;
}
let updatedFrontTokenExists = cookieExists("sFrontToken");
if (!updatedFrontTokenExists) {
// this means it was removed between the api call!
// send analytics
sendAuthAnalytics("front_token_removed", {
url,
headers
});
}
}

function cookieExists(name: string) {
const cookies = document.cookie;
const regex = new RegExp("(^|; )" + encodeURIComponent(name) + "=");
return regex.test(cookies);
}

const sendAuthAnalytics = (eventName: string, payload: Record<string, unknown>, version = "v1") => {
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log("mocked event send:", eventName, version, payload);
return;
}
stAnalytics.sendEvent(
eventName,
{
type: "auth",
...payload
},
version
);
});
};

0 comments on commit 5f4fb78

Please sign in to comment.