Attribute revenue with Stripe Checkout API
Pass the Datasense visitor and session as metadata on a Stripe Checkout Session for accurate attribution.
Make sure you've connected your Stripe account first.
Pass datasense_visitor_id and datasense_session_id (the datasense_vid and datasense_sid cookies the script sets) as metadata when you create the checkout:
app/api/checkout/route.js
import { cookies } from "next/headers"
export async function POST() {
const cookieStore = await cookies()
const visit = {
datasense_visitor_id: cookieStore.get("datasense_vid")?.value,
datasense_session_id: cookieStore.get("datasense_sid")?.value,
}
const session = await stripe.checkout.sessions.create({
mode: "payment", // or "subscription"
line_items: [/* … */],
success_url: "https://example.com/thanks",
metadata: visit,
// For subscriptions, renewals find the visit through the customer. To be explicit:
// subscription_data: { metadata: visit },
})
return Response.json({ url: session.url })
}Once the provider is connected and the metadata is passed, Datasense attributes revenue to the right channels on its own. No webhook code needed.
After a successful payment, you'll see it on the dashboard with its source, campaign, landing page, country and device.
On the cookieless script there's no cookie to read on the server: send window.datasense.sessionId() from the browser with the checkout request, as in this variant:
app/api/checkout/route.js
export async function POST(request) {
// The cookieless script keeps the session in the tab: send it from the
// browser with the checkout request, as { sessionId: window.datasense?.sessionId?.() }
const { sessionId } = await request.json()
const visit = { datasense_session_id: sessionId }
// …the rest as above
}