In OpenCart, products often come in variants (like size or color), and belong to nested categories. For granular insights in Google Analytics 4 (GA4), your tracking should capture and report these dynamically.
Using Google Tag Manager (GTM) Lookup Tables, we can normalize raw data from OpenCart (like internal category IDs or variant slugs) into human-readable, GA4-optimized valuesβwithout hardcoding in PHP.
β Why Use GTM Lookup Tables?
Problem | Solution |
---|---|
Variant codes (e.g. color=12 ) |
Map to Red , Blue , etc. in GTM |
Category IDs (e.g. cat_002 ) |
Normalize to readable names |
Reduce PHP logic | Decouple category mapping from server code |
Update mappings without code deployment | Update directly in GTM |
π§° Prerequisites
- OpenCart 3.x or 4.x
- Web GTM container installed and working
- GA4 Configuration Tag setup
- Product and category IDs available in your OpenCart database
- Basic access to modify
.twig
and PHP controller files
πΉ Step 1: Capture Product Variant & Category Data in dataLayer
In catalog/controller/product/product.php
:
$product_id = (int)$this->request->get['product_id'];
$product_info = $this->model_catalog_product->getProduct($product_id);
$options = $this->model_catalog_product->getProductOptions($product_id);
$categories = $this->model_catalog_product->getCategories($product_id);
// Flatten category ID list
$category_ids = array_column($categories, 'category_id');
$data['gtm_view_item'] = 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'],
'variant_id' => isset($this->request->get['variant']) ? $this->request->get['variant'] : '',
'category_id' => !empty($category_ids) ? $category_ids[0] : ''
]]
]
]);
In product.twig
:
{% if gtm_view_item %}
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({{ gtm_view_item|raw }});
</script>
{% endif %}
β
You now have variant_id
and category_id
in the dataLayer
.
πΉ Step 2: Create Data Layer Variables in GTM
Variable Name | Type | Data Layer Key |
---|---|---|
DLV - variant_id |
Data Layer Variable | ecommerce.items.0.variant_id |
DLV - category_id |
Data Layer Variable | ecommerce.items.0.category_id |
πΉ Step 3: Create Lookup Table for Variants
In GTM:
- Variable Type: Lookup Table
- Input Variable:
DLV - variant_id
- Map Examples:
Input | Output |
---|---|
12 |
Red |
13 |
Blue |
14 |
Green |
- Name it:
Lookup - Variant Label
Repeat this for categories:
Input | Output |
---|---|
5 |
Shoes |
7 |
Electronics > Cameras |
12 |
Clothing > Men > Shirts |
Name it: Lookup - Category Path
πΉ Step 4: Update GA4 view_item
Tag in GTM
- Create GA4 Event Tag:
view_item
- Event Parameters:
Parameter | Value |
---|---|
item_id |
{{ecommerce.items.0.item_id}} |
item_name |
{{ecommerce.items.0.item_name}} |
price |
{{ecommerce.items.0.price}} |
variant |
{{Lookup - Variant Label}} |
item_category |
{{Lookup - Category Path}} |
β You now report clean, readable variant/category values to GA4.
πΉ Step 5: (Optional) Add Variant Selection Click Tracking
In GTM:
- Trigger: Click β CSS Selector
.product-option select
- Tag: GA4 Event
select_item
variant_id
:{{Click Element}}.value
- Use another lookup table to resolve variant label
dataLayer.push({
event: 'select_item',
variant_id: this.value
});
β This helps analyze which variants users engage with most before purchase.
πΉ Step 6: Reporting in GA4
Go to GA4 β Explore:
- Use
item_category
,variant
,item_id
as dimensions - Use
view_item
,add_to_cart
,purchase
as metrics - Build a funnel comparison: Variant engagement β Purchase rate
π Bonus: Keep Lookup Tables Future-Proof
- Use default value like “Unknown” to prevent data loss
- Update only in GTM without changing code
- For >50 mappings, consider using GTM custom templates or BigQuery JOINs
π§ͺ Debug & QA
- Open product page and inspect
dataLayer
- In GTM Preview, confirm
Lookup - Variant Label
resolves correctly - Use GA4 DebugView to validate correct values for variant/category
π§ Summary
Step | Action |
---|---|
1 | Push variant_id and category_id to dataLayer |
2 | Create GTM variables for raw values |
3 | Build Lookup Tables for clean labels |
4 | Use Lookup outputs in GA4 Event Tags |
5 | Track clicks for variant selection (optional) |
6 | Report on variant/category performance in GA4 |
7 | Maintain mappings easily inside GTM |