Auto-Triggering Abandoned Cart Emails from GA4 + OpenCart Events

Standard

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

  1. Go to GA4 > Admin > BigQuery Linking
  2. Enable daily or streaming export
  3. 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

  1. Send data via POST to Klaviyo /profiles/ or /events/ endpoint
  2. Use email as identifier
  3. 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


Leave a Reply

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