Track Unique User Visits and Match with Purchases
How to Track Unique User Visits and Match with Purchases in Laravel
It’s very important to understand from where users visiting the website and which visits turned into purchase from users.
In this tutorial I will show you how I track users unique visits and match them with their purchases when you don’t have auth users.
Model and Migrations
Let’s create a model and migrations first, to store our traffic data.
Add necessary columns to traffic migration file.
If you need to track more query params, feel free to add other columns here.
In the App\Models\Traffic.php
cast the “data” to “json” or “array”. In the data column we will store all request params, in case if some of them were not listed separately, to have everything collected.
Columns explained
url
we will store the current page url without query params.ref
,utm_medium
,utm_source
,utm_campaign
columns will contain the values from the eponymous query params.referer
- we will store HTTP_REFERER to know from which website user camedata
column will contain an array of all query params from the url.uuid
column will be the unique identifier for the user, will talk about it more below
Middleware
To track the params on user’s each page visit, we will use a Middleware for that.
Register your TrafficMiddleware
For Laravel 11
Add TrafficMiddleware::class to your bootstrap/app.php
file
For Laravel 10
Add TrafficMiddleware::class to your app/Http/Kernel.php
file’s web middleware group
TrafficMiddleware
As we do not have auth users, we should somehow identify unique users for visits. For that we will use uuid
and store it in session.
I prefer to store it for 30 days, as users do not buy immediately, we still want to know from where they visited website for the first time.
Feel free to change that to any days you want.
Feel free to exclude any uri’s that you don’t want to store.
Full code for middleware. I cover it in try/catch in case something goes wrong, to not interrupt users visits.
Match visit with purchase
This example is made for LemonSqueezy orders, but can be used with any other payments provider.
When user makes a purchase, we need to match the purchase with the visit. For that we will use the uuid
that we stored in session.
LemonSqueezy checkout url accepts custom params, so we will pass the uuid
to the checkout url and LemonSqueezy will return it back to us in the webhook.
Add uuid to the checkout url
In your plans section, get the uuid
from session and pass to the checkout url.
To learn more about LemonSqueezy for Non-Auth Users check out the tutorial here
For Vue users, to be sure that cookie is available for the first visit, create a new route to get the uuid from the cookie and do async request to get the uuid.
To connect our purchase with the visit, we will store the uuid
in the lemon_squeezy_orders table.
Add new column to the lemon_squeezy_orders
table.
Inside the WebhookController, we will match the purchase with the visit. (check LemonSqueezy Webhooks for Non-Auth Users for more details)
Add the uuid
to the order.
That’s it! Now you can track your users visits and match them with their purchases.