Tracking Coupon Redeem & Discounted Sales in GA4 via OpenCart Data Layer

Standard

Coupon redemptions and discounted sales offer key insights into promotion performance, discount-driven buyers, and ROAS segmentation. Yet, many OpenCart setups fail to accurately send coupon usage data to Google Analytics 4 (GA4)β€”especially when multiple discounts are applied.

🧰 Prerequisites

Component Purpose
OpenCart 3.x or 4.x Storefront
Google Tag Manager Installed across the site
GA4 Configured Through GTM
Promo/Coupon Module Default or custom


🎯 What You’ll Track

  • Coupon code used (e.g., SAVE20)
  • Whether coupon was applied (binary)
  • Final discounted revenue
  • Optional: discount value in currency


🧱 Step 1: Expose Coupon Info in Order Success Page

Edit the controller:
File: catalog/controller/checkout/success.php

Add This Logic:

if (isset($this->session->data['coupon'])) {
$data['coupon_code'] = $this->session->data['coupon'];
} else {
$data['coupon_code'] = '';
}

Then Pass It to the View:

Add $data['coupon_code'] into your success.twig file:


🧾 Step 2: Push Purchase Data with Coupon to dataLayer

In your success.twig:

<script>
window.dataLayer = window.dataLayer || [];

dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: '{{ order_id }}',
value: {{ total }},
currency: '{{ currency }}',
coupon: '{{ coupon_code }}',
items: [
{% for product in products %}
{
item_id: '{{ product.product_id }}',
item_name: '{{ product.name | escape('js') }}',
price: '{{ product.price }}',
quantity: '{{ product.quantity }}'
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
</script>

βœ… This includes the coupon field as part of the GA4 purchase schema.


πŸ”§ Step 3: Configure GTM Variables

Go to GTM β†’ Variables β†’ New:

  • Data Layer Variable: ecommerce.coupon
  • Name it: DLV - ecommerce.coupon


🎯 Step 4: Update GA4 Purchase Event Tag

In GTM, open your GA4 Purchase event tag and add these:

Event Parameters:

Parameter Name Value
coupon {{DLV - ecommerce.coupon}}

πŸ’‘ Optional: Add more if you pass discount value or flags like coupon_used: true.


πŸ” Step 5: Register coupon as a Custom Dimension in GA4

In GA4 Admin β†’ Custom Definitions:

  • Click Create Custom Dimension
    • Name: Coupon Code
    • Scope: Event
    • Event Parameter: coupon

Now the coupon value will show up in GA4 Explorations, Funnel Reports, and Looker Studio dashboards.


πŸ“Š Bonus: Add Discount Amount (Optional)

To track discount amount, extract from order totals.

In your controller (success.php):

$discount_total = 0;
foreach ($order_totals as $total) {
if (strtolower($total['code']) === 'coupon') {
$discount_total = abs($total['value']);
}
}
$data['discount_value'] = $discount_total;

Then add to dataLayer:

discount: {{ discount_value }},

And send via GTM to GA4 as:

Parameter Name Value
discount_value {{DLV - discount}}


πŸ§ͺ Step 6: QA and Testing

Tool What to Check
GTM Preview Mode purchase event contains coupon field
GA4 DebugView Check coupon parameter in purchase
DevTools Console Run dataLayer and inspect contents


πŸ“ˆ Use Cases in GA4

Use Case How to Use
Segment by coupon code usage Compare coupon != null vs null
ROAS by promo Report conversions by coupon in Looker
Abandoned cart re-targeting Filter users who used a coupon but didn’t buy
Identify high-performing campaigns Match utm_campaign + coupon correlation


🧠 Pro Tips

  • Always sanitize coupon strings (no HTML/JS injection)
  • Add a coupon_used: true boolean parameter for filtering
  • Consider hashing coupon if using personal codes (e.g., email-based)


Leave a Reply

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