While Google Analytics 4 (GA4) provides a powerful foundation for eCommerce tracking, relying solely on default dimensions and metrics can limit your insight into store performance, customer behavior, and marketing ROI.
To unlock deeper, customized analysis in OpenCart, you should configure Custom Dimensions and Metrics in GA4. With the flexibility of Google Tag Manager (GTM) and OpenCartโs PHP-based architecture, you can pass dynamic store-specific values like user type, SKU margin, discount source, or payment method.
๐ฏ What Youโll Achieve
Type | Examples |
---|---|
Custom Dimensions | User type, shipping method, product brand, coupon source |
Custom Metrics | Product margin, return rate, shipping cost, loyalty points |
๐งฐ Prerequisites
- OpenCart Admin access
- GTM Web container installed
- GA4 property created
- Ability to modify OpenCart PHP & Twig templates
- Optional: Server-Side GTM for extended privacy control
๐ Step-by-Step: Implementing Custom Dimensions & Metrics in OpenCart
๐น Step 1: Identify Data Points to Send
Decide what store-specific attributes are useful for performance segmentation.
Name | Type | Scope | Use Case |
---|---|---|---|
user_type |
Dimension | user | Compare guest vs. logged-in behavior |
brand |
Dimension | item | Filter by brand performance |
shipping_cost |
Metric | event | Understand true revenue |
product_margin |
Metric | item | Analyze profitability by product |
๐น Step 2: Create Custom Dimensions & Metrics in GA4
In GA4 Admin โ Custom definitions:
Create Custom Dimensions:
Name | Scope | Parameter Name |
---|---|---|
User Type | User | user_type |
Brand | Item | brand |
Create Custom Metrics:
Name | Scope | Parameter Name | Unit |
---|---|---|---|
Shipping Cost | Event | shipping_cost |
Currency |
Product Margin | Item | product_margin |
Currency |
โ These definitions must match exact parameter names used in GTM.
๐น Step 3: Inject Data from OpenCart Backend
Example 1: Add user_type
on all pages
In catalog/controller/common/header.php
:
$data['user_type'] = $this->customer->isLogged() ? 'logged_in' : 'guest';
In header.twig
:
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
user_type: '{{ user_type }}'
});
</script>
Example 2: Add brand
and product_margin
to product pages
In catalog/controller/product/product.php
:
$data['gtm_product_details'] = json_encode([
'event' => 'view_item',
'ecommerce' => [
'currency' => $this->session->data['currency'],
'value' => $product_info['price'],
'items' => [[
'item_id' => $product_info['product_id'],
'item_name' => $product_info['name'],
'price' => $product_info['price'],
'brand' => $product_info['manufacturer'], // Custom Dimension
'product_margin' => $product_info['price'] - $product_info['cost'] // Custom Metric
]]
]
]);
In product.twig
:
{% if gtm_product_details %}
<script>
dataLayer.push({{ gtm_product_details|raw }});
</script>
{% endif %}
โ
This enriches view_item
events with brand and margin data.
๐น Step 4: Push Custom Metrics on Purchase Event
In checkout/success.php
:
$items = [];
foreach ($order_products as $product) {
$items[] = [
'item_id' => $product['product_id'],
'item_name' => $product['name'],
'price' => $product['price'],
'quantity' => $product['quantity'],
'brand' => $product['model'],
'product_margin' => $product['price'] - 10 // Use real cost field if available
];
}
$data['gtm_purchase'] = json_encode([
'event' => 'purchase',
'ecommerce' => [
'transaction_id' => $order_info['order_id'],
'value' => $order_info['total'],
'currency' => $order_info['currency_code'],
'shipping_cost' => $order_info['shipping'], // Custom Metric
'items' => $items
]
]);
In success.twig
:
{% if gtm_purchase %}
<script>
dataLayer.push({{ gtm_purchase|raw }});
</script>
{% endif %}
๐น Step 5: Create GTM Variables
In GTM Web Container:
- Data Layer Variables:
Name | Variable | Scope |
---|---|---|
DLV - user_type |
user_type |
User |
DLV - brand |
ecommerce.items.0.brand |
Item |
DLV - product_margin |
ecommerce.items.0.product_margin |
Item |
DLV - shipping_cost |
ecommerce.shipping_cost |
Event |
๐น Step 6: Configure GA4 Tags with Custom Parameters
Example: GA4 Event Tag for view_item
- Event Name:
view_item
- Parameters:
currency
:{{DLV - ecommerce.currency}}
value
:{{DLV - ecommerce.value}}
items
:{{DLV - ecommerce.items}}
- Custom Parameters:
brand
:{{DLV - brand}}
product_margin
:{{DLV - product_margin}}
โ
Repeat similar logic for add_to_cart
, purchase
, etc.
๐น Step 7: Use Custom Dimensions & Metrics in GA4 Reports
In GA4 Explore โ Free Form / Funnel:
- Add custom dimension:
brand
oruser_type
- Use metrics like
product_margin
orshipping_cost
- Segment users by login state or high-margin product buyers
- Build comparison between logged-in vs guest conversion rates
๐น Step 8: Consent-Safe Data Collection (Optional)
Only push custom metrics if consent is granted:
{% if consent_analytics %}
<script>
dataLayer.push({
user_type: '{{ user_type }}'
});
</script>
{% endif %}
Use CMP or cookie-based consent control.
๐ Reporting Use Cases with Custom Dimensions/Metrics
Use Case | Dimensions | Metrics |
---|---|---|
Track guest vs. logged-in conversion rate | user_type |
purchase count |
Compare margin vs revenue by product | brand |
product_margin , revenue |
Analyze shipping cost impact on abandonment | shipping_cost |
begin_checkout , purchase |
Segment product performance by brand | brand |
items purchased , revenue |
๐งช Testing & Debugging
- Use GTM Preview Mode
- Check
dataLayer
values via browser console - Use GA4 DebugView to validate parameter transmission
- Validate metrics appear in Explore under custom dimensions
๐ฆ Summary Table
Step | Action |
---|---|
1 | Choose dimensions/metrics aligned with business goals |
2 | Register them in GA4 Custom Definitions |
3 | Extract values dynamically in PHP |
4 | Inject into dataLayer via Twig |
5 | Create DLVs in GTM |
6 | Map them to GA4 Event Tags |
7 | Use in Explore, Funnels, and Audience reports |
8 | QA with GTM Preview and GA4 DebugView |