Laravel Stripe UTM tracking: Checkout and Cashier
By Devendra Variya, co-founder of Virling. Published 2026-11-19, updated 2026-11-27.
Laravel encrypts the cookies it reads, and the virling_vid cookie is written by the Virling script in the browser, not by Laravel. So the one Laravel-specific step is to leave it out of encryption. After that, request()->cookie('virling_vid') returns the visitor ID, and the code that creates the Stripe Checkout Session passes it on: as client_reference_id with the plain stripe-php client, or as metadata.virling_vid in Laravel Cashier's checkout options.
The general guide to Stripe Checkout UTM parameters explains why UTM tags are lost at checkout and how Virling joins a payment to the visits behind its visitor ID. The same setup in other stacks is in Symfony Stripe UTM tracking and Rails Stripe UTM tracking. On another provider, see Paddle UTM tracking in Laravel, LemonSqueezy UTM tracking in Laravel and Polar UTM tracking in Laravel.
Load the script from the Blade layout
Add the tag once, in the <head> of the layout your pages extend, usually resources/views/layouts/app.blade.php. With Inertia, that is the root template, resources/views/app.blade.php. Self-hosting? Point src at your own Virling server.
<!-- resources/views/layouts/app.blade.php --> <script defer src="https://app.virling.com/v.js" data-site="yourdomain.com"></script>
Why Laravel returns null for virling_vid
The EncryptCookies middleware in the web group decrypts every incoming cookie. A value it cannot decrypt, such as one written by JavaScript, is replaced with null, so request()->cookie('virling_vid') and $request->cookie('virling_vid') both return null until the name is listed as an exception. In Laravel 11 and later, that list is in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->encryptCookies(except: ['virling_vid']);
})Laravel 10 and older keep it in the $except property of app/Http/Middleware/EncryptCookies.php:
protected $except = [
'virling_vid',
];Plain stripe-php or Cashier
Use the stripe-php client when buyers check out without an account in your app, or when you keep your own billing tables. Cashier fits when a Billable model, usually User, owns the subscription: it creates the Stripe customer, stores subscriptions in its own tables and keeps them in sync through its webhook. Both end in a Checkout Session, so Virling finds the visitor ID the same way.
With stripe-php
Create a StripeClient with your secret key and call checkout->sessions->create(), adding the cookie when there is one:
// app/Http/Controllers/CheckoutController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stripe\StripeClient;
class CheckoutController extends Controller
{
public function store(Request $request)
{
$params = [
'line_items' => [['price' => 'price_...', 'quantity' => 1]],
'mode' => 'subscription',
'success_url' => 'https://yourdomain.com/thanks',
];
if ($vid = $request->cookie('virling_vid')) {
$params['client_reference_id'] = $vid;
}
$stripe = new StripeClient(config('services.stripe.secret'));
$session = $stripe->checkout->sessions->create($params);
return redirect()->away($session->url, 303);
}
}The route is Route::post('/checkout', [CheckoutController::class, 'store'])->name('checkout');, and a Blade form with @csrf posts to it. redirect()->away() sends the browser to Stripe's URL as is, and the 303 makes it follow with a GET:
<form action="{{ route('checkout') }}" method="POST">
@csrf
<button type="submit">Subscribe</button>
</form>With Cashier
Cashier's checkout() takes an array of Checkout Session options and passes them to Stripe. Its result is a Checkout object, which Laravel turns into a redirect when the route returns it:
use Illuminate\Http\Request;
Route::get('/subscribe', function (Request $request) {
$options = [
'success_url' => 'https://yourdomain.com/thanks',
'cancel_url' => 'https://yourdomain.com/pricing',
];
if ($vid = $request->cookie('virling_vid')) {
$options['metadata'] = ['virling_vid' => $vid];
}
return $request->user()
->newSubscription('default', 'price_...')
->checkout($options);
})->middleware('auth');A single charge takes the same options as the second argument of $request->user()->checkout(['price_...' => 1], $options), and a buyer without an account goes through Checkout::guest()->create('price_...', $options). Do not use the subscription builder's withMetadata() for the ID: it fills the subscription's metadata, which Virling does not read.
Each later charge of a Cashier subscription, plan swaps included, keeps the first payment's channel (renewals). The lookup order, including PaymentIntents you create for Stripe Elements, is in the Stripe docs, and the Stripe integration page shows what Virling imports.
Check that it works
Connect a restricted Stripe key that starts with rk_test_ in Virling. Your Laravel app keeps its own test secret key, in services.stripe.secret or, with Cashier, in STRIPE_SECRET, and a price created in test mode. Then run one tagged visit and one test payment, as in test your setup. Visits on a .test address are not recorded for your site, so test on your domain or a subdomain of it.
To see the ID on the session, log it right after it is created and remove the line afterwards. With Cashier, assign the result of checkout($options) to $session, log it, then return it. In storage/logs/laravel.log, client_reference_id or metadata.virling_vid should match window.virling.vid() in the browser console:
logger()->info('checkout', [$session->id, $session->client_reference_id, $session->metadata?->toArray()]);Cashier creates the session for the signed-in user's Stripe customer, so register a new user for each test: a second payment from that customer is a renewal.
Common mistakes
Open an Unattributed payment and look for Visitor under Details. If there is none, checkout did not carry the ID.
| What you see | Cause | Fix |
|---|---|---|
$request->cookie('virling_vid') is null, though the browser has the cookie | EncryptCookies cannot decrypt a cookie Laravel did not set, so it sets the value to null | Add virling_vid to the encryption exceptions (cookies and encryption) |
| Still null after you added the exception | Octane workers keep the old code in memory | Run php artisan octane:reload (Octane) |
| Stripe shows the ID on the subscription, but the payment is Unattributed | It was set with Cashier's withMetadata(), which fills the subscription's metadata | Pass metadata in the options of checkout(), which go to the session (Cashier metadata) |
| No visitor ID when a separate frontend calls your API | The browser leaves cookies out of a fetch to another origin | With the API on your domain or a subdomain, send credentials: "include", set supports_credentials to true in config/cors.php and list the frontend under allowed_origins (CORS and cookies) |
| No visitor ID on sessions created in a queued job | The job runs in a worker, outside the buyer's request, so there is no cookie | Read the cookie in the controller and pass it to the job's constructor (job classes) |
An Inertia page shows a CORS error for checkout.stripe.com | Inertia follows the redirect with an XHR instead of leaving the page | Return Inertia::location($session->url) (external redirects) |
$config must be a string or an array | config('services.stripe.secret') is null: the entry is missing from config/services.php, or the cached configuration is old | Add 'stripe' => ['secret' => env('STRIPE_SECRET')], then run php artisan config:clear (configuration caching) |
The payment has a visitor ID but is Unattributed after a test on a .test address | Valet serves apps on .test, and visits are kept only when the page's host is your site's domain or one of its subdomains | Test on a subdomain of the domain in data-site (script docs) |
| The visitor ID on the payment is your own user or order ID | client_reference_id holds your ID and is read as the visitor ID | Put the visitor ID in the session's metadata.virling_vid, which is read first (Stripe docs) |
Caching an ordinary Blade page is fine, because the script sets the cookie in the browser. Every other cause is in Unattributed revenue. A payment made before a fix usually stays Unattributed, so make a new one to check.
Frequently asked questions
How do I pass UTM parameters to Stripe Checkout in Laravel?
Read the visitor ID with $request->cookie('virling_vid') in the controller that creates the Checkout Session and pass it as client_reference_id. The Virling script records the UTM tags of every visit against that ID, so the payment is joined to all of them.
Why does $request->cookie('virling_vid') return null?
Laravel encrypts cookies by default and returns nothing for a cookie it did not set. Leave virling_vid out of encryption: in Laravel 11 and later with $middleware->encryptCookies(except: ['virling_vid']) in bootstrap/app.php, in older versions by adding it to $except in app/Http/Middleware/EncryptCookies.php.
How do I add the visitor ID with Laravel Cashier?
Pass it in the session options of checkout(), for example newSubscription('default', 'price_...')->checkout(['metadata' => ['virling_vid' => $vid]]). Cashier sends those options to Stripe when it creates the Checkout Session.
Should I use Cashier or the plain stripe-php client?
Either works for Virling, since both create a Checkout Session. Cashier suits subscriptions owned by a Billable user, because it creates the Stripe customer and keeps subscriptions in your database. The stripe-php client suits guest checkout or your own billing tables.
Should I use client_reference_id or metadata.virling_vid?
Use client_reference_id unless it already holds your own user or order ID. In that case put the visitor ID in the session's metadata.virling_vid. Virling reads either.
Does Cashier's withMetadata() attach the visitor ID to the payment?
Not for Virling. withMetadata() sets metadata on the subscription, and Virling does not read the subscription's metadata. Pass 'metadata' => ['virling_vid' => $vid] in the options of checkout(), so it is set on the Checkout Session.
See which campaigns bring Stripe revenue
Virling is open source under AGPL. Self-host it free, or use the hosted version with a 14-day free trial. It connects to Stripe, Paddle, Shopify, WooCommerce, Polar, LemonSqueezy, Dodo, Creem and RevenueCat.