Using GTM Lookup Tables to Handle Product Variants & Categories in OpenCart

Standard

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:

  1. Variable Type: Lookup Table
  2. Input Variable: DLV - variant_id
  3. Map Examples:

Input Output
12 Red
13 Blue
14 Green

  1. 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

  1. Create GA4 Event Tag: view_item
  2. 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


Leave a Reply

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