Frontend Instrumentation – JavaScript
Complete guide to instrument front-end applications with the Faro SDK, sending data to Elven’s collector-fe.
Overview
This guide aims to instruct the instrumentation of front-end applications using Grafana’s Faro SDK, with the collector-fe, an exclusive component of Elven Observability. You’ll be able to track:
User sessions and navigation
Custom events
Structured logs
Performance metrics (such as load time)
Installation
Using NPM:
npm install @grafana/faro-web-sdk
npm install @grafana/faro-web-tracingUsing Yarn:
yarn add @grafana/faro-web-sdk
yarn add @grafana/faro-web-tracingIf you’re using React with React Router:
Basic usage
Best practice: create a separate file like faro.ts to configure the SDK and import it in the entry point (index.tsx or App.tsx).
Setup with React Router (optional)
Logged-in user synchronization
Example with Next.js
When using the Faro SDK in Next.js applications, it’s necessary to ensure that instrumentation happens only on the client side, since window is not available during server-side rendering. The ideal way to configure it is inside Next’s special file, _app.tsx.
Exemplo de uso em pages/_app.tsx
pages/_app.tsxTip: Use variables like NEXT_PUBLIC_FARO_URL in .env.local to keep the token and tenant out of the source code.
Custom events, logs, and metrics
Custom events:
Custom logs:
Custom metrics:
Best practices
Store FARO_URL in .env (REACT_APP_FARO_URL).
Never send sensitive data in events or logs.
Prefer importing faro.ts at the start of the application.
Use samplingRate according to your app’s volume (e.g., 0.1 in production).
Troubleshooting
Events are not being received
Invalid or expired token
Check the JWT
Nothing appears on the dashboard
Incorrect URL or CORS blocked
Check the URL and CORS of the collector-fe
Session does not persist
Blocked cookies or persistent: false
Activate persistent: true
Duplicated logs
initializeFaro being called multiple times
Move to a single file and import once
Authentication and collector URL
The submission URL follows the format:
Real example:
Refer to: collector-fe – Installation Guide
Testing your instrumentation
Access the app using the browser.
Check in DevTools if the call to collector-fe returns 2xx.
Access the Grafana dashboard and filter by events from your tenant.
Made with love by Elven Observability
If you have any questions or suggestions, feel free to contact us.: elven.works.
Last updated
Was this helpful?

