This commit is contained in:
parent
3ceae8677e
commit
d8cbffcfd5
1 changed files with 18 additions and 27 deletions
|
|
@ -1,31 +1,22 @@
|
||||||
const baseUri = 'https://insights.decla.red';
|
import { track } from '../analytics';
|
||||||
const type = 'sdf-2d-demo';
|
|
||||||
|
|
||||||
export const handleInsights = async (initialData: any): Promise<(data: any) => void> => {
|
export const handleInsights = async (initialData: any): Promise<(data: any) => void> => {
|
||||||
try {
|
track('Session Insights', { props: toStringProps(initialData) });
|
||||||
const sessionId = await createSession(initialData);
|
|
||||||
return (data) => createFrame(sessionId, data);
|
let performanceReported = false;
|
||||||
} catch {
|
return (data) => {
|
||||||
return () => null;
|
if (performanceReported) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
performanceReported = true;
|
||||||
|
track('Performance Insights', { props: toStringProps(data) });
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const createSession = async (data: any): Promise<string> => {
|
const toStringProps = (data: Record<string, unknown>): Record<string, string> =>
|
||||||
const response = await sendPostRequest(`${baseUri}/${type}/sessions/`, data);
|
Object.entries(data).reduce<Record<string, string>>((props, [key, value]) => {
|
||||||
const { sessionId } = await response.json();
|
if (value !== undefined && value !== null) {
|
||||||
return sessionId;
|
props[key] = String(value);
|
||||||
};
|
}
|
||||||
|
return props;
|
||||||
const createFrame = (sessionId: string, data: any): Promise<unknown> =>
|
}, {});
|
||||||
sendPostRequest(`${baseUri}/${type}/sessions/${sessionId}`, data).catch();
|
|
||||||
|
|
||||||
const sendPostRequest = async (uri: string, data: any): Promise<Response> =>
|
|
||||||
await fetch(uri, {
|
|
||||||
method: 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
redirect: 'follow',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue