Skip to content

Commit

Permalink
refactor: add vercel analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
rickstaa committed Sep 20, 2022
1 parent fd92fae commit fc74f62
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
1 change: 0 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
name="description"
content="A simple emotion picker that displays all the supported GitHub emojis."
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand Down
10 changes: 0 additions & 10 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
Expand Down
43 changes: 43 additions & 0 deletions src/@types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
/**
* @file Contains global type definitions.
*/

/**
* Disable emoji-mart typing warnings.
*/
// TODO: Remove when https://github.com/missive/emoji-mart/issues/576 is merged.
declare module "@emoji-mart/react";

/**
* Extend Navigator with extra types.
*
* @description Define NetworkInformation attribute types. Needed since they are not yet supported (see
* https://stackoverflow.com/a/69676260/8135687). Taken from
* https://github.com/lacolaco/network-information-types/blob/master/index.d.ts
*/
declare interface Navigator extends NavigatorNetworkInformation {}

// http://wicg.github.io/netinfo/#navigatornetworkinformation-interface
declare interface NavigatorNetworkInformation {
readonly connection?: NetworkInformation;
}

// http://wicg.github.io/netinfo/#effectiveconnectiontype-enum
type EffectiveConnectionType = "2g" | "3g" | "4g" | "slow-2g";

// http://wicg.github.io/netinfo/#dom-megabit
type Megabit = number;
// http://wicg.github.io/netinfo/#dom-millisecond
type Millisecond = number;

// http://wicg.github.io/netinfo/#networkinformation-interface
interface NetworkInformation extends EventTarget {
// readonly type?: ConnectionType; // NOTE: Already included in upstream https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts#L10430.
// http://wicg.github.io/netinfo/#effectivetype-attribute
readonly effectiveType?: EffectiveConnectionType;
// http://wicg.github.io/netinfo/#downlinkmax-attribute
readonly downlinkMax?: Megabit;
// http://wicg.github.io/netinfo/#downlink-attribute
readonly downlink?: Megabit;
// http://wicg.github.io/netinfo/#rtt-attribute
readonly rtt?: Millisecond;
// http://wicg.github.io/netinfo/#savedata-attribute
readonly saveData?: boolean;
// http://wicg.github.io/netinfo/#handling-changes-to-the-underlying-connection
onchange?: EventListener;
}
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { sendToVercelAnalytics } from "./vitals";

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
Expand All @@ -15,4 +16,6 @@ root.render(
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
if (process.env.NODE_ENV === "production") {
reportWebVitals(sendToVercelAnalytics);
}
55 changes: 55 additions & 0 deletions src/vitals.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Metric } from "web-vitals";

const vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals";

/**
* Retrieves user connection speed.
*
* @returns A Vitals object.
*/
function getConnectionSpeed() {
return "connection" in navigator &&
navigator.connection &&
"effectiveType" in navigator.connection
? navigator.connection.effectiveType !== undefined
? navigator.connection.effectiveType
: ""
: "";
}
/**
* Send metrics to Vercel analytics.
*
* @param metric
* @returns
*/
export function sendToVercelAnalytics(metric: Metric) {
const analyticsId = process.env.REACT_APP_VERCEL_ANALYTICS_ID;
if (!analyticsId) {
getConnectionSpeed();
return;
}

const body = {
dsn: analyticsId,
id: metric.id,
page: window.location.pathname,
href: window.location.href,
event_name: metric.name,
value: metric.value.toString(),
speed: getConnectionSpeed(),
};

const blob = new Blob([new URLSearchParams(body).toString()], {
// This content type is necessary for `sendBeacon`
type: "application/x-www-form-urlencoded",
});
if (navigator.sendBeacon) {
navigator.sendBeacon(vitalsUrl, blob);
} else
fetch(vitalsUrl, {
body: blob,
method: "POST",
credentials: "omit",
keepalive: true,
});
}

1 comment on commit fc74f62

@vercel
Copy link

@vercel vercel bot commented on fc74f62 Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.