Track ecommerce with GA

1. Define and encode each Enhanced Ecommerce interaction into the site's data layer.

Define all the key ecommerce interactions on the site, such as impressions, product views, cart additions, checkouts, and purchases. Follow a guide like Simo Ahava’s enhanced ecommerce guide for GTM to write specifications to building dataLayer objects out of these interactions –follow the documentation diligently. Even a small mistake in the dataLayer object could cause the Enhanced Ecommerce data to be ignored by Google Analytics. Make sure each dataLayer object contains the event key specific to each interaction. For example, addToCart for cart additions, or purchase for purchases.

2. Create a new Event tag for each ecommerce interaction type and use Google Analytics Events to collect data instead of Page View tags.

Set each tag to fire on the corresponding dataLayer.push() using a Custom Event trigger.

3. Create a Google Analytics Settings variable for your Enhanced Ecommerce tags with the same configurations as your non-ecommerce Settings variable.

Tick the Enable Enhanced Ecommerce Features and Use Data Layer checkboxes. Set all of your Enhanced Ecommerce Event tags to use this new Google Analytics Settings variable. The Ecommerce settings in the Google Analytics Settings variable

4. Add product-scoped custom dimensions or custom metrics to send additional, non-standard metadata to Google Analytics.

Use custom dimensions to add additional metadata to products, such as product size, color, or variant SKU. Use custom metrics to add numeric data to products for calculation, such as product profit margin, or stock quantity. Open Google Analytics for the property that needs the custom dimension or metric. Go to Admin > Property column > Custom Definitions, select either Custom Dimensions or Custom Metrics, and select Product as the scope. Make note of the Index number assigned to the new definition. Update the dataLayer object for the products, so that the custom dimension or metric is added directly into the product array objects with the key dimensionXX or metricXX, where XX is the index number of the custom definition. In the following example, a new product-scoped custom dimension in index 5 with the value Black has been added to the object: { ecommerce: { purchase: { actionField: { id: 't1' }, products: [{ id: 'p1', dimension5: 'Black' // Product-scoped custom dimension in index 5 }] } } }

5. Use a custom JavaScript variable to change the ecommerce data in the data layer without having to modify the dataLayer object itself.

Create a new dataLayer variable for the Data Layer Variable - ecommerce, and set the variable to Version 1 from the corresponding setting. Create a new custom JavaScript variable that modifies the ecommerce object from the data layer variable and returns the modified object. In the tag or Google Analytics Settings variable settings, select the variable from the drop-down instead of ticking Use Data Layer. For example, this code adds a product-scoped custom dimension to the ecommerce object without modifying the dataLayer object: function() { // Create a copy of the variable to avoid // modifying the source object by reference var ecom = JSON.parse(JSON.stringify({{Data Layer Variable - ecommerce}})); try { ecom.purchase.products.forEach(function(prod) { prod.dimension10 = {{get discount}}(; }); } catch(e) {} return { ecommerce: ecom }; }