Passing Product Data into the Data Layer in Magento 2.1
Hello, developers! Today we’re going to dive into an essential aspect of integrating Google Analytics Enhanced Ecommerce tracking with your Magento 2.1 store - passing product data into the data layer.
The data layer is a JavaScript object that lives on your page and contains important data points about your site’s content and user interactions. For e-commerce sites, this often includes details like product IDs, names, prices, and more.
Why Data Layer Integration Matters
Before we get into the technicalities, let me explain why correctly passing data to the data layer is crucial:
- Cleaner and More Maintainable Code: By using a data layer, you can avoid hard-coding event parameters directly into your Google Tag Manager (GTM) tags.
- Easier Tracking Customization: Data layers allow for more flexibility in tracking custom events without modifying GTM scripts.
- Enhanced Ecommerce Functionality: To use Enhanced Ecommerce features like product impressions, clicks, adds to cart, and purchases in GA, you need to pass the relevant data through a data layer.
Passing Product Data: A Simple Approach
Now let’s discuss how to get those important product details into your data layer. The good news is Magento 2 has core functionality that you can utilize without touching any core files!
Here are the steps:
- Enable the
web/default/design/datapushconfig setting to ‘All’ or ‘JSON-LD’. This tells Magento to push structured data onto the page in a format readable by JavaScript.php bin/magento config:set --scope=default web/default/design/datapush "json-ld" - Now, add this script snippet to your
head.phtmlfile located underapp/design/frontend/{package}/{theme}/Magento_Theme/templates. This will fetch the product details from Magento’s generated data and push them into a data layer variable.<script type="text/javascript"> var dataLayer = dataLayer || []; if (typeof data === 'undefined') { data = {}; } dataLayer.push({ 'event': 'datapush', 'ecommerce': { 'currencyCode': 'USD', 'detail': { 'products': data.product } } }); </script>
What Data is Being Pushed?
The data.product variable contains an array of product objects with the following properties:
nameidcategorypricebrand- And more, depending on how you’ve configured your products
You can access these variables in your Google Tag Manager tags using the $value.{{Product ID}}.{{Variable Name}} syntax.
Conclusion: Data Layer Integration Simplified
As you can see, passing product data into the data layer doesn’t have to involve core file modifications. By utilizing Magento’s built-in data push functionality and a bit of JavaScript, you can easily get your products’ details ready for Enhanced Ecommerce tracking in Google Analytics.
Remember, properly setting up your data layer is just one step towards successful e-commerce analytics integration. You’ll also need to configure the right triggers and variables in GTM to capture those events.
If you found this guide helpful, feel free to explore more advanced topics like custom event tracking or cross-domain tracking on our Laravel Company blog. Until next time!
Happy coding!