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

Rails Paddle UTM tracking: pass the visitor ID to checkout

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

In a Rails app, the value Virling needs at Paddle checkout is already in the request: cookies[:virling_vid]. Rails Paddle UTM tracking means reading it in a controller, handing it to Paddle.js without tripping over Turbo, or setting it on a transaction your controller creates with Net::HTTP or Faraday.

UTM tags sit on the landing URL, and a buyer is often pages away from it by the time checkout opens. Virling gets around that with one ID: its script records each visit against the virling_vid cookie, and the Paddle sync reads the same ID back from the transaction's custom_data (details in the Paddle docs). Hosted checkout links are covered in Paddle checkout UTM parameters; for Stripe on Rails, see Rails Stripe UTM tracking, and for PHP, Laravel Paddle UTM tracking.

Start free trial

Step 1: add both scripts to the layout

Both tags go in the <head> of the layout, where Turbo keeps them across visits. Self-hosting? Point the v.js URL at your own server. PADDLE_CLIENT_TOKEN is a client-side token, never an API key; sandbox tokens start with test_, which the ERB check uses to pick the environment.

<%# app/views/layouts/application.html.erb %>
<script defer src="https://app.virling.com/v.js"
  data-site="yourdomain.com"></script>
<% paddle_token = ENV.fetch("PADDLE_CLIENT_TOKEN") %>
<script src="https://cdn.paddle.com/paddle/v2/paddle.js"
  data-turbo-track="reload"></script>
<script data-turbo-track="reload">
  <% if paddle_token.start_with?("test_") %>
  Paddle.Environment.set("sandbox");
  <% end %>
  Paddle.Initialize({ token: "<%= paddle_token %>" });
</script>

Step 2: read the cookie in a controller

v.js writes an unsigned cookie, so plain cookies reads it. cookies.signed and cookies.encrypted return nil for it.

# config/routes.rb
get "pricing", to: "pricing#show"

# app/controllers/pricing_controller.rb
class PricingController < ApplicationController
  def show
    @vid = cookies[:virling_vid].presence
  end
end

The view puts the price and the ID on the button. tag.button skips a data- attribute whose value is nil, so a first-time visitor gets a button without data-vid:

<%# app/views/pricing/show.html.erb %>
<%= tag.button "Subscribe", type: "button",
      data: { buy: "pri_...", vid: @vid } %>

Step 3: open the overlay checkout with custom data

Add one click listener to your JavaScript entry file:

// app/javascript/application.js
document.addEventListener("click", (event) => {
  const button = event.target.closest("[data-buy]");
  if (!button) return;
  const vid = window.virling?.vid?.() || button.dataset.vid;
  Paddle.Checkout.open({
    items: [{ priceId: button.dataset.buy, quantity: 1 }],
    ...(vid && { customData: { virling_vid: vid } }),
  });
});

A visitor's very first request reaches Rails before the cookie exists, so the listener asks the script first and falls back to data-vid. The spread adds customData only when there is an ID, since Paddle wants at least one key in it (source: Paddle.Checkout.open). Because the listener sits on document, it survives Turbo swapping the body and is never bound twice. A Remix front end handles the same first-visit gap after its loader runs, as the Remix and Paddle checkout guide shows.

Or create the transaction in a controller

With a server-created transaction, the cookie goes into custom_data in the request body. Net::HTTP from the standard library is enough. Add resource :checkout, only: :create to your routes:

# app/controllers/checkouts_controller.rb
require "net/http"

class CheckoutsController < ApplicationController
  def create
    body = { items: [{ price_id: "pri_...", quantity: 1 }] }
    vid = cookies[:virling_vid]
    body[:custom_data] = { virling_vid: vid } if vid.present?

    response = Net::HTTP.post(
      URI("#{ENV.fetch("PADDLE_API_URL")}/transactions"),
      body.to_json,
      "Authorization" => "Bearer #{ENV.fetch("PADDLE_API_KEY")}",
      "Content-Type" => "application/json"
    )
    transaction = JSON.parse(response.body).fetch("data")
    redirect_to transaction.dig("checkout", "url"),
      allow_other_host: true, status: :see_other
  end
end

If the app already uses Faraday, build the connection with Faraday.new(url: ENV.fetch("PADDLE_API_URL"), headers: ...) and send the same hash with conn.post("transactions", body.to_json). Post to the controller from a view with Turbo off:

<%= button_to "Subscribe", checkout_path,
      form: { data: { turbo: false } } %>

button_to renders a form, and turbo: false makes the browser follow the redirect with a full page load. Paddle.js needs that load to pick up ?_ptxn= on your default payment link and open the checkout (source: Paddle, Pass a transaction to a checkout). Set PADDLE_API_URL to the live or sandbox API base, and use a key with transaction.write (source: Paddle, Create a transaction) that is separate from the read-only key Virling gets.

Run this in the action the buyer's click reaches: an Active Job or a webhook controller has no buyer cookie. One catch: Paddle gives API-made transactions the origin api, while Virling records a free trial only from a $0 subscription transaction of origin web, so trial plans keep the overlay from step 3.

Turbo navigation

  • Visits are counted. Turbo page visits use history.pushState, and the Virling script records each one.
  • Initialize once. Paddle allows one Paddle.Initialize() call on a page (source: Paddle.Initialize). Turbo keeps scripts already in the <head> and evaluates scripts in the <body> on every render (source: Turbo, Building your Turbo application).
  • Two layouts. Moving from a layout without Paddle.js, Turbo adds the missing head scripts, and the inline one can run before Paddle.js has loaded. data-turbo-track="reload" makes that move a full page load.
  • Redirects to a payment link. Paddle.js has to start on the page with ?_ptxn=. A Turbo visit does not start it again, so the form turns Turbo off.

Test it on a Rails staging app

  1. Connect a sandbox key starting with pdl_sdbx_ in Virling, with the permissions in the Paddle docs. Give the staging environment its own test_ token, API key, API URL, price ID and, for the controller flow, default payment link; none carry over from live.
  2. Deploy to a subdomain such as staging.yourdomain.com. Paddle's sandbox works from bin/rails server on localhost, but Virling does not record those visits for your site.
  3. Visit https://staging.yourdomain.com/pricing?utm_source=test&utm_medium=email in a private window, click Subscribe, and pay with 4242 4242 4242 4242 and any future date (source: Paddle, Sandbox).
  4. The transaction in Paddle should show virling_vid in its custom data. After the next sync, it is listed at /app/yourdomain.com/payments with source test and channel Email.

Sandbox payments stay in your reports after you switch to a live key. Stuck? Test your setup walks through each check.

Rails symptoms, causes and fixes

SymptomCauseFix
data-vid is missing on a visitor's first pageRails rendered the page before v.js had set the cookieRead window.virling?.vid?.() on click, as in step 3
cookies.signed[:virling_vid] is nilThe script sets a plain cookie, so there is no signature to check. cookies.encrypted fails the same wayRead cookies[:virling_vid]
cookies is undefinedThe controller inherits from ActionController::API, which leaves out cookiesAdd include ActionController::Cookies
Unrelated payments share one visitor IDThe button is inside a cached fragment or page, so one visitor's data-vid is served to othersKeep the button out of the cache block
The overlay opens twiceA click listener is added again on every turbo:loadAdd one listener on document, as in step 3
Paddle.Initialize runs on every Turbo visitThe script is in the <body>, which Turbo evaluates on every renderMove the script tags to the <head>
After button_to, the payment link loads with no overlayTurbo followed the redirect, so Paddle.js did not start againSet data: { turbo: false } on the form

Anything else that leaves a payment Unattributed is listed in Unattributed revenue.

Frequently asked questions

Should I copy UTM parameters into Paddle custom data from Rails?

You can, but one visitor ID carries more. Virling records the tags of every visit against cookies[:virling_vid], including earlier and untagged visits, so pass that value as virling_vid in customData instead.

Why is cookies[:virling_vid] nil in my Rails controller?

The first request a new visitor makes goes out before v.js has set the cookie. Active Jobs and webhook controllers never see it, and cookieless mode or a blocked script means it is never set. On the client, window.virling?.vid?.() has the ID once the script has run.

Does Turbo break Paddle checkout?

Not if Paddle.js is loaded and initialized in the head and one click listener is added on the document. Turn Turbo off for a form that redirects to Paddle's checkout.url on your own domain, so the page loads fully and Paddle.js opens the checkout.

Can I send the Paddle request with Faraday instead of Net::HTTP?

Yes. Create a connection with Faraday.new using PADDLE_API_URL and the same Authorization and Content-Type headers, then post the same JSON body, with custom_data holding virling_vid, to transactions. Paddle sees the same request either way.

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