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
- Name:
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: trueboolean parameter for filtering - Consider hashing coupon if using personal codes (e.g., email-based)
