Script Tag
The OpenPanel Web SDK allows you to track user behavior on your website using a simple script tag. This guide provides instructions for installing and using the Web SDK in your project.
Installation
Just insert this snippet and replace YOUR_CLIENT_ID
with your client id.
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)
Usage
Tracking Events
You can track events with two different methods: by calling the window.op('track')
directly or by adding data-track
attributes to your HTML elements.
Identifying Users
To identify a user, call the window.op('identify')
method with a unique identifier.
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:
Advanced Usage
Filtering events
You can filter out events by adding a filter
property to the init
method.
Below is an example of how to disable tracking for users who have a disable_tracking
item in their local storage.
Using the Web SDK with NPM
Typescript
Getting ts errors when using the SDK? You can add a custom type definition file to your project.
Simple
Just paste this code in any of your .d.ts
files.
Strict typing (from sdk)
Step 1: Install the SDK
Step 2: Create a type definition file
Create a op.d.ts
file and paste the following code: