vercel / analytics

Privacy-friendly, real-time traffic insights
https://vercel.com/analytics
Mozilla Public License 2.0
422 stars 26 forks source link

BeforeSend don't work in app directory RSC #91

Closed thibaultleouay closed 1 year ago

thibaultleouay commented 1 year ago

When using beforeSend in my root layout it throws this error


1 of 1 unhandled error

Unhandled Runtime Error
Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".
  <... beforeSend={function}>
                  ^^^^^^^^^^
tobiaslins commented 1 year ago

Hey @thibaultleouay!

Because it's not possible to pass functions from a server-component to a client component, you need to create a client component wrapper to make before send work.

"use client"
import { Analytics } from '@vercel/analytics/react'`

export const VercelAnalytics = () => {
   return <Analytics beforeSend={(event) => {
      // do something
      return event
   }} />
}

Now you can use this new client component in your root layout! <VercelAnalytics />