Track Stripe revenue by traffic source in an AI-built app
By Devendra Variya, co-founder of Virling. Published 2026-11-19, updated 2026-12-01.
If you built your app with Lovable, Bolt, Replit or Cursor and added Stripe, you can see which channel brings paying users in four steps:
- Add your site in Virling and connect Stripe with a read-only restricted key.
- Give your coding assistant the prompt below. It adds the Virling script to every page and passes the visitor ID from the
virling_vidcookie to the Checkout Session asclient_reference_id. - Make one test payment from a page that runs the script.
- Open the payments list: each payment shows the channel of the visit behind it.
Why Stripe alone cannot tell you the channel
Stripe knows who paid and how much. It does not know that the buyer first came from a Reddit thread or an AI answer, because Checkout runs on Stripe's domain and only stores what your server passes when it creates the session. UTM tags on the landing URL are gone by then. Passing one visitor ID fixes that: Stripe keeps the ID, and Virling joins the payment to every visit behind it, including untagged ones such as search and referrals. The full method is in how to track Stripe revenue by traffic source. If your app takes payment through Creem or Dodo Payments instead, the same visitor ID goes into the checkout's metadata: see Creem for AI apps or revenue by source for AI apps on Dodo Payments. If you built the app in Bubble rather than with a coding assistant, revenue tracking for a Bubble app shows where the script goes and how the visitor ID reaches Stripe.
Step 1: add your site and connect Stripe
Sign up, add your domain, and paste a Stripe restricted key with Read access on Charges, Checkout Sessions, Customers, Events, Invoices, Subscriptions and PaymentIntents. Virling refuses secret keys, so it can never write to your Stripe account. The steps are in the Stripe docs. If your app sells through Polar instead, Polar for AI apps shows where the visitor ID goes on a Polar checkout.
Step 2: paste this prompt into your coding assistant
Replace yourdomain.com with your domain, exactly as you added it. If you self-host Virling, replace app.virling.com with your server; the setup screen in the app shows the exact tag for your site.
Add Virling revenue tracking to this app. Make only these two changes.
1. Add this tag to the <head> of every page, before </head>, so it loads on every route:
<script defer src="https://app.virling.com/v.js" data-site="yourdomain.com"></script>
Keep defer. data-site is my domain without www. and without a path.
2. Find the server code that creates the Stripe Checkout Session and pass the visitor ID:
- Read the virling_vid cookie from the request and set it as client_reference_id on the session.
- If client_reference_id is already used for something else, leave it and set metadata: { virling_vid: ... } on the session instead.
- If the code that creates the session runs on another domain and does not receive my cookies (for example a Supabase Edge Function), read the ID in the browser with window.virling?.vid?.() and send it in the request body. That function returns nothing until the deferred script has loaded, so wait up to 2 seconds for window.virling.vid before reading it, and skip it if it is still missing.
- Do not change prices, products, success_url or anything else in checkout.
- buy.stripe.com Payment Links need no change: the Virling script adds client_reference_id to them.The prompt follows the script docs and checkout docs. Review the diff before you accept it: the change to checkout should be one or two lines.
What the change should look like
On a Node server, the Checkout Session gets one new line:
const session = await stripe.checkout.sessions.create({
client_reference_id: req.cookies.virling_vid,
line_items: [{ price: "price_...", quantity: 1 }],
mode: "subscription",
success_url: "https://yourdomain.com/thanks",
});If the session is created on another domain, the browser reads the ID and sends it along. The wait covers the deferred script loading after your checkout code:
async function visitorId() {
for (let i = 0; i < 20 && !window.virling?.vid; i++) {
await new Promise((r) => setTimeout(r, 100));
}
return window.virling?.vid?.() || null;
}The checkout snippet generator writes this code for your provider and setup if you would rather check it by hand.
What to check in the code before going live
An assistant can report that it is done and still miss a page or a second checkout. Search the project for v.js, virling_vid and checkout.sessions.create, and read what you find:
- The tag appears once. It belongs in the shared HTML head or root layout. A second copy sends its own pageview.
- It is a plain external script. Not bundled, not imported and not
type="module", because the script readsdata-sitefrom its own tag. data-siteis your real domain, withoutwww.and without a path, notyourdomain.comor the builder's preview address. There is nodata-cookieless, which would remove the visitor ID.- Every checkout passes the ID. A pricing page, an upgrade button and a modal may each create their own session. Each
checkout.sessions.createcall needs the line. - The ID is the visitor's. It comes from the
virling_vidcookie orwindow.virling.vid(), not from a new random ID, your user ID or a value typed in as an example. - Checkout works without it. Block the script and start a checkout. It should open as usual, with no visitor ID set.
- Any content security policy allows it. If the app sends one, the Virling server must be in
script-srcandconnect-src. - No key is in the code. The restricted key for Virling goes into Virling only, and your Stripe secret key stays on the server, never in browser code.
- Nothing else changed. Prices, products,
success_urland webhook code should be the same as before.
Step 3: check it with a test payment
Open your site in a new tab and make a test payment from a page that runs the script. After the next sync, open the payments list in the app. The payment should show a channel instead of Unattributed; open it to see the visits before it. Payments without a visitor ID still count toward revenue, as Unattributed.
The script does not track on localhost, and events from pages on a host other than your domain are dropped, so test on your deployed site, not in the preview inside your builder.
Symptom, cause and fix
Open a payment in the payments list and look for Visitor under Details: it is the visitor ID, from checkout or from an email match. Then find the row that matches.
| Symptom | Likely cause | Fix |
|---|---|---|
| No visits on the published site | The tag is missing from the published head, or it was bundled and cannot find data-site | Put the plain tag in the shared head, publish again, and look for a request to /api/event answered with 202 |
| No visits while testing | You are on localhost, or on a preview address that is not your domain | Test on the published domain or one of its subdomains |
| Two pageviews for every page | The tag is in the page twice, such as in a layout and a component | Keep one copy, in the root layout |
| Only the first page is counted | The router keeps the route in the hash, such as /#/pricing | Switch the router to history mode, with real paths |
| Every payment is Unattributed, with no visitor ID | The function that creates the session is on another domain and gets no cookie, or never reads it | Read the ID in the browser and send it in the request body, as above |
| Some payments have no visitor ID | A second checkout does not pass it, the code ran before the deferred script loaded, or the visitor blocks the script | Add the line to every checkout and wait up to 2 seconds for the ID. Blocked visitors stay Unattributed |
req.cookies is undefined and checkout fails | Express does not parse cookies unless a parser such as cookie-parser is set up | Read virling_vid from the request's Cookie header, and leave the field out when it is missing |
| A visitor ID is shown, but it is your own user ID | The app already used client_reference_id, and it is read as the visitor ID | Keep it and set metadata.virling_vid on the session, which is read first |
After a fix, make a new test payment with a new customer: one made before the fix usually stays Unattributed, and a later payment by the same customer keeps the first payment's channel. The steps are in test your setup.
Step 4: ask which channel pays
Once payments carry a channel, you can read revenue by channel, referrer and campaign in the app, or ask your AI assistant. Virling serves a read-only MCP endpoint, so Claude and other assistants that speak MCP can answer "which channel made the most money last month?". The setup is in ask Claude which channel pays.
With payments credited, you can launch and see what it earned. How to measure Product Hunt launch revenue covers a launch day and the weeks after it, Indie Hackers revenue tracking covers posts and milestones, and sharing revenue by channel publicly turns the result into a page your readers can open.
Frequently asked questions
Do I need to write code myself?
No. Paste the prompt into your coding assistant and it makes the two changes: the script tag in the head of every page, and the visitor ID on the Checkout Session. Then check the result with one test payment.
My checkout runs in a Supabase Edge Function. Does the cookie reach it?
Usually not. The virling_vid cookie is set on your domain, so a function on another domain does not receive it. Read the ID in the browser with window.virling?.vid?.() and send it in the request body, then pass it as client_reference_id.
What if I use Stripe Payment Links?
They need no code. With the Virling script on the page, it adds client_reference_id to every buy.stripe.com link when the page loads and again on click, and never replaces a value that is already there.
My app already sets client_reference_id. What then?
Keep it, and put the visitor ID in metadata.virling_vid on the Checkout Session instead. Virling reads metadata.virling_vid first, then client_reference_id.
Can I ask my AI assistant which channel pays?
Yes. Virling serves a read-only MCP endpoint, so Claude and other assistants that speak MCP can answer questions such as which channel made the most revenue last month.
Why are my payments still Unattributed after the assistant added the code?
Usually because the Checkout Session has no visitor ID. Either the function that creates it runs on another domain and does not receive the cookie, or a second checkout in the app was not changed. Fix that and make a new test payment, since ones made before the fix usually stay Unattributed.
Can I test it in my builder's preview?
Only if the preview runs on your domain or one of its subdomains. The script does not track on localhost by default, and events from pages on another host are dropped, so publish the app and test on your own domain.
See which channel brings paying users
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.