You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use vercel analytics in astro but I'd have to create a library for that I see that you have a nice set of Components that Allow the user to integrate analytics from other sites. I'm going to just give you the code for how to integrate Vercel Analytics if you want more info look into the docs.
You need to install web-vitals in order to use the code below.
Code needed for Web Analytics
import{Metric,onCLS,onFCP,onFID,onLCP,onTTFB}from'web-vitals';constvitalsUrl='https://vitals.vercel-analytics.com/v1/vitals';functiongetConnectionSpeed(){return'connection'innavigator&&navigator['connection']&&typeofnavigator['connection']==="object"&&'effectiveType'innavigator['connection']
? navigator['connection']['effectiveType']
: '';}typeOptions={params: {[s: string]: any;}|ArrayLike<any>;path: string;analyticsId: string;debug: boolean;}functionsendToAnalytics(metric:Metric,options:Options){constpage=Object.entries(options.params).reduce((acc,[key,value])=>acc.replace(value,`[${key}]`),options.path,);constbody={dsn: options.analyticsId,// qPgJqYH9LQX5o31Ormk8iWhCxZOid: metric.id,// v2-1653884975443-1839479248192
page,// /blog/[slug]href: location.href,// https://my-app.vercel.app/blog/my-testevent_name: metric.name,// TTFBvalue: metric.value.toString(),// 60.20000000298023speed: getConnectionSpeed(),// 4g};if(options.debug){console.log('[Analytics]',metric.name,JSON.stringify(body,null,2));}constblob=newBlob([newURLSearchParams(bodyasRecord<string,any>).toString()],{// This content type is necessary for `sendBeacon`type: 'application/x-www-form-urlencoded',});if(navigator.sendBeacon){navigator.sendBeacon(vitalsUrl,blob);}elsefetch(vitalsUrl,{body: blob,method: 'POST',credentials: 'omit',keepalive: true,});}exportfunctionwebVitals(options:Options){try{onFID((metric)=>sendToAnalytics(metric,options));onTTFB((metric)=>sendToAnalytics(metric,options));onLCP((metric)=>sendToAnalytics(metric,options));onCLS((metric)=>sendToAnalytics(metric,options));onFCP((metric)=>sendToAnalytics(metric,options));}catch(err){console.error('[Analytics]',err);}}
Astro Component
<script>// Replace this with the one that comes from the file with the script that I have written above. import{webVitals}from"~/utils/vitals";constanalyticsId=import.meta.env.PUBLIC_VERCEL_ANALYTICS_ID;if(analyticsId){webVitals({
analyticsId,path: location.pathname,params: location.search,debug: !import.meta.env.PROD,});}</script>
This discussion was converted from issue #4 on June 22, 2023 21:15.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to use vercel analytics in astro but I'd have to create a library for that I see that you have a nice set of Components that Allow the user to integrate analytics from other sites. I'm going to just give you the code for how to integrate Vercel Analytics if you want more info look into the docs.
You need to install
web-vitals
in order to use the code below.Code needed for Web Analytics
Astro Component
Beta Was this translation helpful? Give feedback.
All reactions