virling Pricing Self-host Demo Changelog Agencies GitHub Start free trial
blog / 2026-11-26

Laravel Polar checkout UTM tracking with metadata

By Devendra Variya, co-founder of Virling. Published 2026-11-26.

Laravel Polar checkout UTM tracking comes down to one value. Load the Virling script in your Blade layout, leave the virling_vid cookie out of Laravel's cookie encryption, then read it with $request->cookie('virling_vid') in the controller that creates the Polar checkout session and set it in metadata. Polar copies checkout metadata to the order, and Virling joins the order to every visit behind that visitor ID, with the UTM tags of each. Checkout links on buy.polar.sh need no code at all.

This post covers Laravel. The same steps in a Next.js app are in Next.js Polar UTM tracking, and with Stripe in Laravel Stripe UTM tracking. For what Polar does with the visitor ID outside Laravel, see the general guide to Polar UTM tracking and the Polar integration page.

Start free trial

Pick the row that matches how you sell:

How you sellWhat to doCode needed
Checkout link on buy.polar.sh in a Blade viewKeep it as a normal <a href> on a page that runs the script. The script adds reference_idNo
Checkout session your controller createsRead the virling_vid cookie and set metadata, as in step 3Yes, on the server
Link in an email, a QR code or on another siteSend people to a page that runs the script, and put the checkout link thereNo

Why Polar checkout loses your UTM tags

UTM tags live on the landing URL. By the time a visitor clicks Buy they are usually a few pages in, and Polar checkout runs on Polar's domain, so nothing from your site travels with them unless it is in the checkout. Passing one visitor ID instead of the tags keeps earlier visits and untagged ones, such as search and referrals, too.

Step 1: add the script to the Blade layout

Paste the tag before </head> in the layout every page extends, usually resources/views/layouts/app.blade.php. If you self-host, replace app.virling.com with your Virling server.

<!-- resources/views/layouts/app.blade.php -->
<script defer src="https://app.virling.com/v.js"
  data-site="yourdomain.com"></script>

The script sets the first-party virling_vid cookie on your domain and records the UTM tags of each visit against it.

Step 2: checkout links, no code

If you sell through a Polar checkout link, keep it as a normal link in your Blade view:

<a href="https://buy.polar.sh/YOUR_CHECKOUT_LINK">Buy now</a>

On a page that runs the script, the script adds reference_id=<visitor ID> to every link to buy.polar.sh when the page loads and when a visitor clicks one, unless the link already has a reference_id. Polar attaches link parameters like reference_id to the checkout session's metadata and copies it to the order, where Virling reads it. Polar lists the parameters a link accepts in its checkout links docs. A virling_vid custom field on the link still works too. The no-code option is explained in full in Polar UTM tracking.

Step 3: checkout sessions from a controller

If your app creates the checkout session, do it in a controller the buyer's browser posts to, so the request carries the cookie. First leave the cookie out of encryption. Laravel encrypts cookies by default, and $request->cookie() returns nothing for a cookie it did not set itself. The script sets virling_vid in the browser, so exclude it. In Laravel 11 and later, in bootstrap/app.php:

->withMiddleware(function (Middleware $middleware) {
    $middleware->encryptCookies(except: ['virling_vid']);
})

In older versions, add it to $except in app/Http/Middleware/EncryptCookies.php:

protected $except = [
    'virling_vid',
];

Then add your Polar token and API address to config/services.php. The token is an Organization Access Token with the checkouts:write scope:

// config/services.php
'polar' => [
    'token' => env('POLAR_ACCESS_TOKEN'),
    'api' => env('POLAR_API_URL', 'https://api.polar.sh'),
],

Create the session with POST /v1/checkouts/ and set the visitor ID in metadata:

// app/Http/Controllers/CheckoutController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class CheckoutController extends Controller
{
    public function __invoke(Request $request)
    {
        $metadata = array_filter(['virling_vid' => $request->cookie('virling_vid')]);

        $checkout = Http::withToken(config('services.polar.token'))
            ->post(config('services.polar.api').'/v1/checkouts/', [
                'products' => ['PRODUCT_ID'],
                'success_url' => url('/thanks'),
                'metadata' => (object) $metadata,
            ])->throw()->json();

        return redirect()->away($checkout['url']);
    }
}

The array_filter leaves the key out when the cookie is missing, since Polar accepts string metadata values of 1 to 500 characters, and the (object) cast keeps an empty metadata as a JSON object. Polar copies the session's metadata to the resulting order and subscription. Its reference for the request, with every field, is create checkout session. If you use a Polar SDK or a Laravel package instead of the HTTP client, set the same virling_vid key in the metadata of its checkout call.

Register the route and post to it from a plain form, so the browser sends the cookie along:

// routes/web.php
use App\Http\Controllers\CheckoutController;

Route::post('/checkout', CheckoutController::class)->name('checkout');

<!-- resources/views/pricing.blade.php -->
<form action="{{ route('checkout') }}" method="POST">
    @csrf
    <button type="submit">Buy</button>
</form>

Virling reads virling_vid from, in order: the order's metadata, the order's custom field data, then the customer's metadata.

Step 4: connect Polar

In Polar, create a second Organization Access Token with the orders:read and refunds:read scopes, plus subscriptions:read so Retention can tell who still pays, and paste it into Virling. Production and sandbox tokens both work. Virling imports orders from the last 90 days on the first run, then new orders every minute. There is no webhook to add to your Laravel app. The full setup is in the Polar docs.

Subscriptions and renewals

Only the first checkout needs the visitor ID: orders with billing reason subscription_cycle or subscription_update, and any later order by the same customer, keep the channel of that customer's first order. If that first order is older than the first import, its renewals are usually Unattributed, as described in renewals.

Test it with the Polar sandbox

Run one tagged visit and one sandbox order as in test your setup, with a token from the Polar sandbox connected in Virling. What differs for Laravel:

  • In your Laravel app, set POLAR_API_URL=https://sandbox-api.polar.sh, a sandbox token with checkouts:write in POLAR_ACCESS_TOKEN, and a sandbox product ID in the controller. It is a different token from the one you give Virling.
  • Press Buy on the form that posts to your controller. The script adds reference_id only to links on buy.polar.sh, so it does not tag sandbox links and step 2 cannot be tested in the sandbox.
  • Use a new email address for each test, because a later payment by the same customer is a renewal and keeps the first payment's channel.
  • On the payment in Virling, Visitor under Details is the visitor ID that came with checkout.

Common mistakes

  • The cookie is still encrypted. $request->cookie('virling_vid') returns null, so the checkout carries no ID. Add it to the encryption exceptions from step 3.
  • The session is created before the buyer arrives. A checkout created in a queued job or a console command has no request and no cookie to read. Create it in the request the buyer's browser sends.
  • Checkout runs on another domain. The cookie is first-party, so the page with the Buy button has to be on the domain the script runs on, or the request does not carry it.
  • The link already had a reference_id. The script keeps the one you set, so it cannot add the visitor ID there. Take it off the link, or use a session with metadata.
  • A redirect drops the UTM tags. If the ad lands on a URL that redirects without keeping the query string, the visit is recorded without its tags. See UTM parameters lost after redirect.

Orders without a visitor ID still count toward revenue, as Unattributed. How the join works from visit to order is in Polar UTM tracking.

Frequently asked questions

How do I pass UTM parameters to a Polar checkout in Laravel?

Pass the Virling visitor ID instead. Read it with $request->cookie('virling_vid') in the controller that creates the checkout session and set it as metadata.virling_vid in the request to POST /v1/checkouts/. Polar copies checkout metadata to the order, and the Virling script records the UTM tags of every visit against that ID, so the order 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.

Do Polar checkout links in a Laravel app need code?

No. Keep the link to buy.polar.sh as a normal link in a Blade view on a page that runs the script. The script adds reference_id with the visitor ID to it, unless it is already there, and Polar stores it in the order's metadata, where Virling reads it.

Do I need a Polar webhook in my Laravel app?

No. Virling reads orders from the Polar API with an organization access token and checks for new orders every minute.

How are Polar renewals credited?

Orders with billing reason subscription_cycle or subscription_update, and any later order by the same customer, keep the channel of that customer's first order. Only the first checkout needs the visitor ID.

What happens if the cookie is missing?

$request->cookie('virling_vid') returns null and array_filter leaves the key out, so the checkout is created without a visitor ID, for example when the script is blocked or runs in cookieless mode. The order still counts toward revenue, as Unattributed.

See which campaigns bring Polar 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.

Start free trial See the demo