Cross-Domain Tracking in GA4 for OpenCart with Payment Gateway Redirects

Standard

One of the most common pitfalls in eCommerce GA4 setups is loss of attribution due to payment gateway redirects. Platforms like Razorpay, PayPal, CCAvenue, Stripe, etc., often take users offsite and redirect back—causing GA4 to assign the conversion to “referral” or “direct.”

🧰 Prerequisites

Tool Purpose
OpenCart 3.x / 4.x eCommerce platform
GTM Installed Web container across store
GA4 Config Tag With cross-domain support enabled
Payment Gateway URL Known return domain (e.g., razorpay.com)


⚠️ Problem Overview

Before:

  1. User visits OpenCart → Adds to cart
  2. Goes to Razorpay (redirect)
  3. Returns to /checkout/success
  4. GA4 reports the sale as (referral) / razorpay.com


✅ Goal

Ensure the GA4 session persists through the redirect so that the original source/medium (e.g., google / cpc) is preserved when the user returns post-payment.


📦 Step 1: Modify GA4 Config Tag in GTM for Cross-Domain Support

In Google Tag Manager:

  1. Open your GA4 Config Tag.
  2. Under Fields to Set, click “Add Row”.

Field Name Value
linker true

  1. Under Cross-Domain Tracking, expand the settings:
    • Check ✅ “Enable linking across domains”
    • Add your gateway domains:

razorpay.com, paypal.com, secure.2checkout.com, stripe.com

These are the domains where users temporarily leave and return.


🧱 Step 2: Modify Outbound Payment Links to Include Linker Parameters

To ensure GA4 passes the gl linker parameter (e.g., _gl=...), decorate outbound links.

In OpenCart’s template or JS files where gateways are invoked (e.g., in Razorpay iframe/JS SDK or form actions):

Option A: Using Auto Linker (preferred)

Add this in the head section of your header.twig:

<script>
window.gtag = window.gtag || function() {};
gtag('set', 'linker', {
'domains': ['razorpay.com', 'paypal.com']
});
</script>


Option B: Manual Linker Injection (if form posts)

If using form redirects, modify your form:

<form id="payment-form" action="https://secure.razorpay.com/checkout" method="POST">
<!-- Fields -->
</form>

<script>
gtag('get', 'GA_MEASUREMENT_ID', 'linker_param', function(linkerParam) {
const form = document.getElementById('payment-form');
if (form) {
form.action += (form.action.includes('?') ? '&' : '?') + linkerParam;
}
});
</script>

Replace GA_MEASUREMENT_ID with your actual GA4 property ID (e.g., G-XXXXXXX).


🛬 Step 3: Add Referral Exclusions in GA4

In GA4 Admin:

  1. Go to Data Streams > Web Stream > More Tagging Settings
  2. Click List unwanted referrals
  3. Add domains such as:

razorpay.com
paypal.com
stripe.com

This ensures GA4 doesn’t consider the return from these domains as a new referral.


🧪 Step 4: Debug and Validate

Use GA4 DebugView:

  • Go to DebugView in GA4 → Watch the full funnel
  • Validate that source/medium (e.g. google / cpc) persists across redirect

Chrome DevTools:

  • Inspect if _gl= parameter is appended to the redirect URL
  • Use GTM Preview mode to confirm linker settings


🧠 Pro Tips

Tip Benefit
Use server-side GTM Further hardens attribution on the backend
Send session_id from GA4 Used for BigQuery attribution stitching
Add hidden input for UTM Persist through redirect if needed
GA4 audiences Build segments like “users who dropped after gateway redirect”


🧭 Attribution Scenarios Without Cross-Domain Setup

Case Outcome
User comes from Google Ads, converts after Razorpay Shows as referral / razorpay.com unless handled
User from Meta ad returns post-payment Appears as new session, original source lost


🎯 After Setup: What You Unlock

  • Correct source / medium attribution for paid and organic
  • Reliable conversion path reports
  • Lower (direct) and (referral) attribution anomalies
  • Better modeled conversion attribution in GA4 reports


Leave a Reply

Your email address will not be published. Required fields are marked *