Promo code tracking is crucial for understanding what drives conversions, repeat purchases, and ROAS efficiency. With GA4 and GTM, you can track promo code usage during checkout in OpenCart using custom event parameters—even without modifying the checkout flow too heavily.
🧰 Prerequisites
Tool/Platform | Purpose |
---|---|
OpenCart 3.x or 4.x | eCommerce platform |
Google Tag Manager | Installed sitewide |
Google Analytics 4 | Configured and linked with GTM |
GA4 Admin Access | To register custom dimensions |
🎯 Goal
Track when a promo code is used in OpenCart, and pass that value to GA4 in the purchase
event using a custom event parameter (e.g., promo_code
).
📋 Step 1: Extract Promo Code from Order (Server-Side)
OpenCart stores the promo/discount code in the order_total
table when the coupon is applied. We’ll expose this to the front-end.
Edit success.twig
:
In catalog/view/theme/YOUR_THEME/template/checkout/success.twig
, add this snippet where you prepare your purchase event:
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: '{{ order_id }}',
value: {{ total }},
currency: '{{ currency }}',
items: [
{% for product in products %}
{
item_id: '{{ product.product_id }}',
item_name: '{{ product.name }}',
price: '{{ product.price }}',
quantity: '{{ product.quantity }}'
}{% if not loop.last %},{% endif %}
{% endfor %}
],
coupon: '{{ coupon if coupon else "" }}' // This assumes your controller is passing `coupon`
}
});
</script>
✅ If
coupon
isn’t available, you can extract it in thesuccess()
controller (catalog/controller/checkout/success.php
) and pass it to the view:
$data['coupon'] = $this->session->data['coupon'] ?? '';
🛠️ Step 2: Configure Data Layer Variable in GTM
- Go to GTM > Variables
- Add a new Data Layer Variable
- Name:
DLV - ecommerce.coupon
- Data Layer Variable Name:
ecommerce.coupon
- Name:
This variable will pick up the promo code value from the purchase
dataLayer.
📨 Step 3: Modify GA4 Purchase Event Tag in GTM
- Go to Tags > GA4 Event Tag >
purchase
- In Event Parameters, add:
Parameter Name | Value |
---|---|
coupon |
{{DLV - ecommerce.coupon}} |
- Make sure
ecommerce
object is passed in the GA4 tag’s advanced configuration if using Enhanced Ecommerce-style payloads.
🧾 Step 4: Register coupon
as a Custom Dimension in GA4
- Go to GA4 Admin > Custom Definitions
- Click Create Custom Dimension
- Dimension Name:
Coupon Code
- Scope: Event
- Event Parameter:
coupon
- Dimension Name:
This will enable coupon reporting across GA4 Explorations and Reports.
🔍 Step 5: QA the Data
Use the following tools to debug:
Tool | Use |
---|---|
GTM Preview Mode | Check if ecommerce.coupon is set correctly |
GA4 DebugView | Ensure the coupon parameter appears |
Dev Console | Run console.log(dataLayer) on thank you page |
🧠 Pro Tips
Tip | Benefit |
---|---|
Hash promo codes (optional) | Prevent competitor scraping in payloads |
Send coupon with add_to_cart too |
For abandoned cart recovery logic |
Use coupon != null in GA4 filters |
To build promo-based segments |
Compare coupon usage vs. AOV |
Identify discount-based value behavior |
📈 Strategic Uses
- Create GA4 audiences: Users who used
SUMMER20
or any code - Measure ROAS of specific promos
- Compare cart abandonment rate of promo vs. non-promo users
- Trigger remarketing based on coupon behavior