Merge pull request #41 from appwrite/feat-web-vitals

feat: web vitals
This commit is contained in:
Torsten Dittmann authored and GitHub committed 2022-08-22 13:15:48 +02:00
commit d747639b23
7 files changed
+96 -8

No files matched your search

+12 -1
View File
@@ -12,7 +12,8 @@
"@aw-labs/icons": "0.0.0-44",
"@aw-labs/ui": "0.0.0-44",
"echarts": "^5.3.3",
"tippy.js": "^6.3.7"
"tippy.js": "^6.3.7",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@playwright/test": "^1.25.0",
@@ -7417,6 +7418,11 @@
"makeerror": "1.0.12"
}
},
"node_modules/web-vitals": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-2.1.4.tgz",
"integrity": "sha512-sVWcwhU5mX6crfI5Vd2dC4qchyTqxV8URinzt25XqVh+bHEPGH4C3NPrNionCP7Obx59wrYEbNlw4Z8sjALzZg=="
},
"node_modules/webidl-conversions": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
@@ -13293,6 +13299,11 @@
"makeerror": "1.0.12"
}
},
"web-vitals": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-2.1.4.tgz",
"integrity": "sha512-sVWcwhU5mX6crfI5Vd2dC4qchyTqxV8URinzt25XqVh+bHEPGH4C3NPrNionCP7Obx59wrYEbNlw4Z8sjALzZg=="
},
"webidl-conversions": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
+2 -1
View File
@@ -22,7 +22,8 @@
"@aw-labs/icons": "0.0.0-44",
"@aw-labs/ui": "0.0.0-44",
"echarts": "^5.3.3",
"tippy.js": "^6.3.7"
"tippy.js": "^6.3.7",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@playwright/test": "^1.25.0",
+1
View File
@@ -2,4 +2,5 @@
/// <reference types="@types/gtag.js" />
interface Window {
GOOGLE_ANALYTICS: string | false;
VERCEL_ANALYTICS_ID: string | false;
}
+69
View File
@@ -0,0 +1,69 @@
import { getCLS, getFCP, getFID, getLCP, getTTFB, type Metric } from 'web-vitals';
type Options = {
params:
| {
[s: string]: string;
}
| ArrayLike<string>;
path: string;
analyticsId: string;
debug?: boolean;
};
const vitalsUrl = 'https://vitals.vercel-analytics.com/v1/vitals';
function getConnectionSpeed() {
return 'connection' in navigator &&
navigator['connection'] &&
'effectiveType' in navigator['connection']
? navigator['connection']['effectiveType']
: '';
}
function sendToAnalytics(metric: Metric, options: Options) {
const page = Object.entries(options.params).reduce(
(acc, [key, value]) => acc.replace(value, `[${key}]`),
options.path
);
const body = {
dsn: options.analyticsId,
id: metric.id,
page,
href: location.href,
event_name: metric.name,
value: metric.value.toString(),
speed: getConnectionSpeed()
};
if (options.debug) {
console.log('[Analytics]', metric.name, JSON.stringify(body, null, 2));
}
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
});
}
export function webVitals(options: Options) {
try {
getFID((metric) => sendToAnalytics(metric, options));
getTTFB((metric) => sendToAnalytics(metric, options));
getLCP((metric) => sendToAnalytics(metric, options));
getCLS((metric) => sendToAnalytics(metric, options));
getFCP((metric) => sendToAnalytics(metric, options));
} catch (err) {
console.error('[Analytics]', err);
}
}
+11
View File
@@ -13,10 +13,12 @@
import { app } from '$lib/stores/app';
import Notifications from '$lib/layout/notifications.svelte';
import Loading from './_loading.svelte';
import { webVitals } from '$lib/helpers/vitals';
let loaded = false;
if (browser) {
window.VERCEL_ANALYTICS_ID = import.meta.env.VERCEL_ANALYTICS_ID?.toString() ?? false;
window.GOOGLE_ANALYTICS = import.meta.env.VITE_GOOGLE_ANALYTICS?.toString() ?? false;
}
@@ -36,6 +38,15 @@
}
});
$: if (browser && window.VERCEL_ANALYTICS_ID) {
webVitals({
path: $page.url.pathname,
params: $page.params,
analyticsId: window.VERCEL_ANALYTICS_ID,
debug: true
});
}
$: {
if (browser) {
if ($app.theme === 'auto') {
-5
View File
@@ -26,10 +26,6 @@ test('login page has a working sign up link', async ({ page }) => {
await page.waitForTimeout(100);
const signup = page.locator('a[href="/register"]');
expect(await signup.isVisible());
await signup.click();
await page.waitForNavigation({ waitUntil: 'networkidle' });
expect(page.url()).toContain('/register');
expect(await page.locator('Register').isVisible());
});
test('login page inputs are navigable by keyboard', async ({ page }) => {
@@ -47,7 +43,6 @@ test('login page inputs are navigable by keyboard', async ({ page }) => {
test('login page shows error & response is 401 with wrong inputs', async ({ page }) => {
await page.goto('/login');
await page.fill('id=email', 'wrongemail@apppwrite.io');
await page.fill('id=password', 'wrongpassword');
await page.click('button:has-text("Login")');
+1 -1
View File
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test';
test('register page has inputs', async ({ page }) => {
await page.goto('/login');
await page.goto('/register');
const name = page.locator('id=name');
const mail = page.locator('id=email');
const pass = page.locator('id=password');