Recovering abandoned carts is one of the highest ROI email automations in eCommerce. When combined with OpenCartβs event data and GA4βs behavioral insights, you can build a powerful system that auto-triggers emails only to engaged, identifiable users who abandon without converting.
π§° Prerequisites
Platform/Tool | Purpose |
---|---|
OpenCart v3.x / 4.x | Storefront + editable theme files |
Google Tag Manager | Pushing events into GA4 |
GA4 + BigQuery | Event collection and audience queries |
Email Platform (e.g. Klaviyo) | Email automation via API or CSV import |
π Flow Overview
OpenCart β dataLayer β GTM β GA4 β BigQuery β Query abandoned carts β CRM β Email
π¦ Step 1: Push add_to_cart
Event from OpenCart
Update product.twig
or JS add-to-cart handler to send data to GTM:
<script>
document.querySelector('.btn-add-to-cart').addEventListener('click', function () {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
event: "add_to_cart",
product_id: "{{ product.product_id }}",
product_name: "{{ product.name }}",
product_price: "{{ product.price }}",
email: "{{ email if customer_logged_in else '' }}"
});
});
</script>
β If email is not available on frontend, collect it at checkout entry or via login field.
π³ Step 2: Push purchase
Event on Success Page
Edit success.twig
(after order placement):
<script>
dataLayer = dataLayer || [];
dataLayer.push({
event: 'purchase',
transaction_id: '{{ order_id }}',
email: '{{ email if email else customer.email }}',
value: {{ total }}
});
</script>
π οΈ Step 3: Configure GA4 Tags in GTM
Create two GA4 Event Tags:
A. GA4 β add_to_cart
- Event Name:
add_to_cart
- Parameters:
product_id
:{{DLV - product_id}}
product_name
:{{DLV - product_name}}
product_price
:{{DLV - product_price}}
email
:{{DLV - email}}
(if available)
- Trigger: Custom Event =
add_to_cart
B. GA4 β purchase
- Event Name:
purchase
- Parameters:
transaction_id
:{{DLV - transaction_id}}
value
:{{DLV - value}}
email
:{{DLV - email}}
π§ Step 4: Store GA4 Events in BigQuery
- Go to GA4 > Admin > BigQuery Linking
- Enable daily or streaming export
- GA4 data will populate in:
project.dataset.events_*
π Step 5: Write SQL Query to Find Abandoned Carts
Abandoned cart = add_to_cart
exists but purchase
doesnβt.
WITH cart_events AS (
SELECT
user_pseudo_id,
event_date,
MAX(IF(event_name = 'add_to_cart', 1, 0)) AS added_to_cart,
MAX(IF(event_name = 'purchase', 1, 0)) AS purchased,
MAX(IF(event_name = 'add_to_cart', value.string_value) FILTER(WHERE key = 'email')) AS email
FROM
`project.dataset.events_*`
WHERE
_TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY))
AND FORMAT_DATE('%Y%m%d', CURRENT_DATE())
GROUP BY user_pseudo_id, event_date
)
SELECT *
FROM cart_events
WHERE added_to_cart = 1 AND purchased = 0 AND email IS NOT NULL
β Export the result to CSV or use directly in Zapier/Cloud Function for automation.
π¬ Step 6: Trigger Emails from CRM (Klaviyo, Mailchimp, etc.)
Option A: Klaviyo via API
- Send data via POST to Klaviyo
/profiles/
or/events/
endpoint - Use
email
as identifier - Trigger a flow in Klaviyo for new abandoned cart entry
{
"data": {
"type": "event",
"attributes": {
"profile": {
"email": "user@example.com"
},
"metric": {
"name": "Abandoned Cart"
},
"properties": {
"product_name": "OpenCart T-shirt",
"cart_value": "29.99"
},
"timestamp": "2025-06-04T12:00:00Z"
}
}
}
Option B: CSV Import (Manual / Zapier)
- Download your BigQuery output
- Import into Mailchimp/Klaviyo segment
- Trigger automation from the list
β Optional: Add Deduplication Logic
To prevent re-triggering for same cart:
- Store sent
user_pseudo_id
+event_date
in Firestore or Sheets - On next query, exclude previously used users
π§ͺ Step 7: QA the Journey
Tool | Use |
---|---|
GTM Preview | Ensure event firing |
GA4 DebugView | Validate add_to_cart and purchase |
BigQuery | Check email field population |
Klaviyo Logs | Confirm abandoned cart event appears |
π Bonus: Add Engagement Thresholds
To avoid low-quality email triggers:
- Add filter:
session_engaged = TRUE
in GA4 events - Or
engagement_time_msec > 10000