Model and Migrations
Let’s create a model and migrations first, to store our traffic data.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
urlwe will store the current page url without query params.ref,utm_medium,utm_source,utm_campaigncolumns will contain the values from the eponymous query params.referer- we will store HTTP_REFERER to know from which website user camedatacolumn will contain an array of all query params from the url.uuidcolumn 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 yourbootstrap/app.php file
bootstrap/app.php
app/Http/Kernel.php file’s web middleware group
app/Http/Kernel.php
TrafficMiddleware
As we do not have auth users, we should somehow identify unique users for visits. For that we will useuuid 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.
TrafficMiddleware.php
TrafficMiddleware.php
Match visit with purchase
When user makes a purchase, we need to match the purchase with the visit. For that we will use theuuid 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 theuuid from session and pass to the checkout url.
uuid in the lemon_squeezy_orders table.
Add new column to the lemon_squeezy_orders table.
uuid to the order.

