Tracking events
Learn how to track events in your TurboStarter web app.
The implementation strategy for each analytics provider varies depending on whether it's designed for client-side or server-side use. We'll explore both approaches, as they are crucial for ensuring accurate and comprehensive analytics data in your web SaaS application.
Client-side tracking
The client strategy for tracking events, which every provider must implement, is straightforward:
You don't need to worry much about this implementation, as all the providers are already configured for you. However, it's useful to be aware of this structure if you plan to add your own custom provider.
As shown above, each provider must supply two key elements:
Provider
- a component that wraps your app.track
- a function responsible for sending event data to the provider.
To track an event, you simply need to invoke the track
method, passing the event name and an optional data object:
Server-side tracking
The server strategy for tracking events, that every provider has to implement, is even simpler:
You don't need to worry much about this implementation, as all the providers are already configured for you. However, it's useful to be aware of this structure if you plan to add your own custom provider.
This server-side strategy allows you to track events outside of the browser environment, which is particularly useful for scenarios involving server actions or React Server Components.
To track an event on the server side, simply call the track
method, providing the event name and an optional data object:
Ensure correct import!
Make sure to use the correct import for the track
function. We're using the same name for both client and server tracking, but they are different functions. For server-side, just add /server
to the import path (@turbostarter/analytics-web/server
).
Congratulations! You've now mastered event tracking in your TurboStarter web app. With this knowledge, you're well-equipped to analyze user behaviors and gain valuable insights into your application's usage patterns. Happy analyzing! 📊
Last updated on