Custom Variables on Magento Product Details Pages: A Comprehensive Guide
Magento is a powerful e-commerce platform that offers extensive customization options, allowing you to track detailed information about your visitors’ behavior using Google Analytics. In this post, we’ll dive into how to set up custom variables for product views in Magento 1.4.0.1.
Understanding the Issue
The core problem lies in the asynchronous tracking code provided by Google Analytics and integrated into the default Magento theme. This code is inserted within the <head> section of your pages, and it consists of two main parts:
- The
_gaqarray initialization and population with account details and pageview tracking. - A function that loads the ga.js script asynchronously.
If you try to add a custom variable directly in your product view template (e.g., app/design/frontend/default/my_package/template/catalog/product/view.phtml), it will be inserted after the _gaq.push(['_trackPageview']); line. According to Google’s documentation, this positioning means that the custom variable won’t make it into your Analytics data.
Analyzing the Default Tracking Code
Let’s take a closer look at the default tracking code in Magento:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The key lines to note are the _gaq initialization and population, followed by the _trackPageview push. The asynchronous script loader function is defined afterward.
Solving the Problem: Two Approaches
To resolve this issue, we need to insert our custom variable setting before the ['_trackPageview'] line but only on product pages. Here are two approaches to achieve this:
Approach 1: Editing the Core GA Module (Not Recommended)
While it’s possible to edit the core Magento Google Analytics module located at app/code/core/Mage/GoogleAnalytics/Block/Ga.php, this approach is highly discouraged by Magento as it breaks the upgrade path and can cause issues in future updates.
Approach 2: Using a Layout Update XML File
The recommended way is to create a new layout update XML file that adds your custom variable setting only on product pages. To do this:
-
Navigate to
app/design/frontend/default/your_theme/layout. -
Create a new XML file named something like
ga_custom_var.xml. -
Open the file and add the following content:
<layout version="0.1.0">
<default>
<reference name="head">
<action method="addJs"><script>var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>
</script></action>
</reference>
</default>
</layout>
Note that this code assumes you have the following variables available:
$_product: The product object being viewed.$_helper: An instance of Magento’s catalog helper.
- Save the file and clear your cache in Magento (
var/cache/flush).
This file uses the addJs action to insert a new script containing your custom variable setting right before the existing _gaq.push(['_trackPageview']).
Conclusion
Setting up custom variables on Magento product detail pages requires careful positioning of the tracking code. By understanding the structure of the default Google Analytics module and using layout update XML files, you can successfully pass valuable product data to Google Analytics without modifying core files.
Remember that custom variables provide deep insights into visitor behavior, allowing you to make data-driven decisions about your store’s performance and user experience. For more advanced tracking techniques and Magento development best practices, consider exploring the resources at Laravel Company.