OpenCart
Tracking on OpenCart 3 and 4 – via Google Tag Manager and Google Analytics 4, with the tracking code in the theme and a sale line on the confirmation page, or from your server. No module from us.
- Setup
- 10–30 min
- Tracking via
- Google Tag Manager or script + sale line
- You need
- Google Tag Manager with GA4 – or a developer for the theme and confirmation page
What the integration reports
- SalesVia GA4 or a sale line (needs a developer)
- RefundsOnly from your server with an API key
- Personal discount codesWhen GA4 or the sale line sends the code
There is no OpenCart integration from us, but tracking can be set up in three ways. The easiest is Google Tag Manager with our ready-made container and the purchase from Google Analytics 4. If the store doesn't have that, a developer adds the tracking code to the theme and a sale line to the confirmation page – or reports the click and the sale from your server.
Before you start
- The snippets from Program → Setup, where your store ID is filled in. In this guide it says
DIN_BUTIKinstead. - Choose Other under Where do you sell? in Setup. We don't detect OpenCart automatically.
Recommended: Google Tag Manager and Google Analytics 4
Many OpenCart stores have Google Tag Manager and a Google Analytics 4 extension that sends purchase to the data layer from the order confirmation. Then no code is needed in the theme:
- Press Scan your site in Setup. If the scan finds Google Analytics 4 with ecommerce, Automatically from Google Analytics is chosen under How the sale is reported, and the VAT is set from your prices. If it only finds Tag Manager, choose Automatically from Google Analytics yourself if your extension sends
purchase. - Download the Tracking only container under Do you use Google Tag Manager? in Setup and import it with Merge – see Google Tag Manager.
- Publish the container and test (below).
If your extension doesn't send the purchase to Google Analytics 4, you can use the With purchase tag container if the data layer has purchase with ecommerce.transaction_id and ecommerce.value. GA4 extensions for OpenCart send the amount differently – with or without VAT and shipping. Check the amount in the live test, and adjust VAT in the amount and Shipping in the amount in Setup (or in the purchase tag) if it is wrong.
Alternative A: the tracking code in the theme and a sale line
Needs a developer with access to the store's files. Preferably make the changes through an OCMOD extension or a child theme, so an update of OpenCart or the theme doesn't remove them.
1. The tracking code on every page
Add the code just before </head> in the theme's header:
- OpenCart 3:
catalog/view/theme/<theme>/template/common/header.twig - OpenCart 4:
catalog/view/template/common/header.twig(or the theme extension's owncommon/header.twig)
<script src="https://app.affilyflow.com/t/DIN_BUTIK.js" async></script>Clear the theme cache afterwards (in OpenCart 3: the gear on the dashboard → Theme and SASS).
2. The order on the confirmation page
OpenCart's confirmation page (checkout/success) doesn't receive the order number: the controller empties the session and shows the shared common/success.twig. So the controller must read the order before it is emptied. At the top of index() in catalog/controller/checkout/success.php – before the line if (isset($this->session->data['order_id'])) {:
// Affilyflow: the order for the sale line – read before OpenCart empties the session below.
$data['affilyflow'] = false;
if (isset($this->session->data['order_id'])) {
$this->load->model('checkout/order');
$af_order = $this->model_checkout_order->getOrder($this->session->data['order_id']);
if ($af_order) {
$af_base = 0;
foreach ($this->model_checkout_order->getOrderTotals($af_order['order_id']) as $af_total) {
// The goods without VAT (sub_total) minus the discount code (coupon is negative). Not shipping, VAT or gift vouchers.
if ($af_total['code'] == 'sub_total' || $af_total['code'] == 'coupon') {
$af_base += $af_total['value'];
}
}
$data['affilyflow'] = array(
'order' => $af_order['order_id'],
'total' => number_format(max(0, $af_base), 2, '.', ''),
// The order amounts are in the store's default currency.
'currency' => $this->config->get('config_currency'),
'coupon' => isset($this->session->data['coupon']) ? $this->session->data['coupon'] : '',
);
}
}In OpenCart 4 the function is called getTotals instead of getOrderTotals (same model, checkout/order).
3. The sale line
Add the line to common/success.twig (OpenCart 3: catalog/view/theme/<theme>/template/common/success.twig, OpenCart 4: catalog/view/template/common/success.twig). The file is also used by other "thank you" pages, so the line sits inside a condition and only shows after a purchase:
{% if affilyflow %}<div hidden data-affilyflow-order="{{ affilyflow.order|e }}" data-affilyflow-total="{{ affilyflow.total|e }}" data-affilyflow-currency="{{ affilyflow.currency|e }}"{% if affilyflow.coupon %} data-affilyflow-coupon="{{ affilyflow.coupon|e }}"{% endif %}></div>{% endif %}The tracking code reads the line and reports the purchase. The amount is the goods after discount, without VAT and shipping, with a dot as the decimal separator. More about the fields in The sale line. Use only one route – the line or Google Analytics – so the same purchase isn't calculated in two ways.
Alternative B: from your server
If you want the click stored as a cookie from your own server (it lasts longer in Safari) and the sale reported once the order is paid, use the PHP snippets in Server-side tracking. They are generic PHP and need an API key from Settings → API keys. In OpenCart they belong:
- The click: in code that runs on every page before the page is sent – for example an event before the controllers or a file under
catalog/controller/startup/. - The sale: when the order gets a paid status, for example as an event after
addOrderHistory(OpenCart 3) oraddHistory(OpenCart 4) in thecheckout/ordermodel. The event trigger syntax differs between versions – see OpenCart's documentation for your version. Check the order'sorder_status_idagainst your "paid" statuses, and send the order number, the goods without VAT and shipping, and the currency. The same order number only counts once, so a status that changes several times is not a problem. - Refunds: the same way, when the order gets a refunded status – only the server route reports refunds to us.
Test
Go to the Test tracking step in Setup, press Open test link, and place a test order in the tab that opens. Stay in Setup – the live test shows the test click and the purchase with order number and amount and asks whether the amount is right. A test purchase gives no commission and no fee.
If the sale doesn't show up: check that the tracking code (or the GTM tag) is also on the confirmation page, that the theme cache is cleared, and – on the GTM route – that you accepted cookies in the banner. See also Testing and troubleshooting.
Refunds
On the GTM and sale line routes, OpenCart doesn't report refunds to us. You reject a refunded purchase yourself within the approval period – see Approval and refunds. On the server route, your server can report the refund.
Missing something?
Write to us – we answer quickly, and the page gets fixed.