Next.js
Good to know
Keep in mind that all tracking here happens on the client!
Read more about server side tracking in the Server Side Tracking section.
Installation
Install dependencies
Initialize
Add OpenPanelComponent
to your root layout component.
Options
Common options
apiUrl
- The url of the openpanel API or your self-hosted instanceclientId
- The client id of your applicationclientSecret
- The client secret of your application (only required for server-side events)filter
- A function that will be called before sending an event. If it returns false, the event will not be sentdisabled
- If true, the library will not send any eventswaitForProfile
- If true, the library will wait for the profile to be set before sending events
Web options
trackScreenViews
- If true, the library will automatically track screen views (default: false)trackOutgoingLinks
- If true, the library will automatically track outgoing links (default: false)trackAttributes
- If true, you can trigger events by using html attributes (<button type="button" data-track="your_event" />
) (default: false)
NextJS options
profileId
- If you have a user id, you can pass it here to identify the usercdnUrl
- The url to the OpenPanel SDK (default:https://openpanel.dev/op1.js
)filter
- This is a function that will be called before tracking an event. If it returns false the event will not be tracked. Read moreglobalProperties
- This is an object of properties that will be sent with every event.
filter
This options needs to be a stringified function and cannot access any variables outside of the function.
To take advantage of typescript you can do the following. Note toString
Usage
Client components
For client components you can just use the useOpenPanel
hook.
Server components
Since you can't use hooks in server components, you need to create an instance of the SDK. This is exported from @openpanel/nextjs
.
Refer to the Javascript SDK for usage instructions.
Tracking Events
You can track events with two different methods: by calling the op.track( directly or by adding
data-track` attributes to your HTML elements.
Identifying Users
To identify a user, call the `op.identify( method with a unique identifier.
For server components
For server components you can use the IdentifyComponent
component which is exported from @openpanel/nextjs
.
This component is great if you have the user data available on the server side.
Setting Global Properties
To set properties that will be sent with every event:
Creating Aliases
To create an alias for a user:
Incrementing Properties
To increment a numeric property on a user profile.
value
is the amount to increment the property by. If not provided, the property will be incremented by 1.
Decrementing Properties
To decrement a numeric property on a user profile.
value
is the amount to decrement the property by. If not provided, the property will be decremented by 1.
Clearing User Data
To clear the current user's data:
Server side
If you want to track server-side events, you should create an instance of our Javascript SDK. It's exported from @openpanel/nextjs
When using server events it's important that you use a secret to authenticate the request. This is to prevent unauthorized requests since we cannot use cors headers.
You can use the same clientId but you should pass the associated client secret to the SDK.
Serverless & Vercel
If you log events in a serverless environment like Vercel, you can use waitUntil
to ensure the event is logged before the function is done.
Otherwise your function might close before the event is logged. Read more about it here.
Proxy events
With createNextRouteHandler
you can proxy your events through your server, this will ensure all events are tracked since there is a lot of adblockers that block requests to third party domains.
Remember to change the apiUrl
in the OpenPanelComponent
to your own server.