Revenue tracking
Learn how to easily track your revenue with OpenPanel and how to get it shown directly in your dashboard.
Revenue tracking is a great way to get a better understanding of what your best revenue source is. On this page we'll break down how to get started.
Before we start, we need to know some fundamentals about how OpenPanel and your payment provider work and how we can link a payment to a visitor.
Payment providers
Usually, you create your checkout from your backend, which then returns a payment link that your visitor will be redirected to. When creating the checkout link, you usually add additional fields such as metadata, customer information, or order details. We'll add the device ID information in this metadata field to be able to link your payment to a visitor.
OpenPanel
OpenPanel is a cookieless analytics tool that identifies visitors using a device_id. To link a payment to a visitor, you need to capture their device_id before they complete checkout. This device_id will be stored in your payment provider's metadata, and when the payment webhook arrives, you'll use it to associate the revenue with the correct visitor.
Some typical flows
- Revenue tracking from your backend (not identified)
- Revenue tracking from your backend (identified)
- Revenue tracking from your frontend
- Revenue tracking without linking it to a identity or device
Revenue tracking from your backend (webhook)
This is the most common flow and most secure one. Your backend receives webhooks from your payment provider, and here is the best opportunity to do revenue tracking.
When you create the checkout, you should first call op.fetchDeviceId(), which will return your visitor's current deviceId. Pass this to your checkout endpoint.
Revenue tracking from your backend (webhook) - Identified users
If your visitors are identified (meaning you have called identify with a profileId), this process gets a bit easier. You don't need to pass the deviceId when creating your checkout, and you only need to provide the profileId (in backend) to the revenue call.
When a visitor logs in or is identified, call op.identify() with their unique profileId.
Since the visitor is already identified, you don't need to fetch or pass the deviceId. Just send the checkout data.
Since the user is authenticated, you can get their profileId from the session and store it in metadata for easy retrieval in the webhook.
In the webhook handler, retrieve the profileId from the session metadata.
Revenue tracking from your frontend
This flow tracks revenue directly from your frontend. Since the success page doesn't have access to the payment amount (payment happens on Stripe's side), we track revenue when checkout is initiated and then confirm it on the success page.
When the visitor clicks the checkout button, track the revenue with the amount.
On your success page, flush all pending revenue events. This will send all pending revenues tracked during checkout and clear them from sessionStorage.
Pros:
- Quick way to get going
- No backend required
- Can track revenue immediately when checkout starts
Cons:
- Less accurate (visitor might not complete payment)
- Less "secure" meaning anyone could post revenue data
Revenue tracking without linking it to an identity or device
If you simply want to track revenue totals without linking payments to specific visitors or devices, you can call op.revenue() directly from your backend without providing a deviceId or profileId. This is the simplest approach and works well when you only need aggregate revenue data.
Simply call op.revenue() with the amount. No deviceId or profileId is needed.
Pros:
- Simplest implementation
- No need to capture or pass device IDs
- Works well for aggregate revenue tracking
Cons:
- You can't dive deeper into where this revenue came from. For instance, you won't be able to see which source generates the best revenue, which campaigns are most profitable, or which visitors are your highest-value customers.
- Revenue events won't be linked to specific user journeys or sessions
Available methods
Revenue
The revenue method will create a revenue event. It's important to know that this method will not work if your OpenPanel instance didn't receive a client secret (for security reasons). You can enable frontend revenue tracking within your project settings.
Add a pending revenue
This method will create a pending revenue item and store it in sessionStorage. It will not be sent to OpenPanel until you call flushRevenue(). Pending revenues are automatically restored from sessionStorage when the SDK initializes.
Send all pending revenues
This method will send all pending revenues to OpenPanel and then clear them from sessionStorage. Returns a Promise that resolves when all revenues have been sent.
Clear any pending revenue
This method will clear all pending revenues from memory and sessionStorage without sending them to OpenPanel. Useful if a payment was cancelled or you want to discard pending revenues.