I need to set up conversion tracking for Google Ads using Stripe Checkout. My plan is to detect the session_id
query parameter in the URL, remove it, and send a purchase event.
On a successful transaction, the webpage redirects to a URL like this:
success_url: `${FRONTEND_URL}/app/somepath/${someVariable.id}?session_id={CHECKOUT_SESSION_ID}&screen=some_screen`,
Stripe will automatically replace {CHECKOUT_SESSION_ID}
with the actual checkout session ID.
In my React app, I listen for changes in the search parameters and trigger a purchase event when session_id
is detected:
import { useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
function StripeConversionTracker() {
const [searchParams, setSearchParams] = useSearchParams();
useEffect(() => {
const sessionId = searchParams.get('session_id');
if (sessionId) {
// Remove the session_id parameter from URL
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.delete('session_id');
setSearchParams(newSearchParams, { replace: true });
// Send conversion event
if (window.gtag) {
gtag('event', 'purchase', {
transaction_id: sessionId,
// Add any additional parameters needed for your conversion tracking
send_to: 'AW-CONVERSION_ID/CONVERSION_LABEL' // Replace with your actual values
});
}
// You can also track with other analytics services here
// For example, with Facebook Pixel:
if (window.fbq) {
fbq('track', 'Purchase', {
content_ids: [sessionId]
});
}
}
}, [searchParams, setSearchParams]);
return null;
}
export default StripeConversionTracker;
Is this approach safe and reasonable for conversion tracking using Stripe Checkout, or are there better alternatives?