Map data using a variable

1. Create new data layer variables to reference items and keys in the dataLayer.

For example, if the dataLayer object is encoded for Enhanced Ecommerce, you would create a data layer variable for the ecommerce key (choose Version 1 as the Data Layer Variable version). If the dataLayer object is encoded for GA4, you would create variables for items and any other keys you may need, such as transaction_id. At minimum, you’ll want to at least reference the products/items and any additional metadata such as product lists and transaction details.

2. Create a custom JavaScript variable to map variables to their correct or updated format.

Take the object in the dataLayer – such as {{ecommerce}} in the example below – and return it once it’s modified. For example, if you wanted to update the affiliation key in the purchase object, you would add: function() { // Create a copy of the ecommerce key in dataLayer var ecommerce = JSON.parse(JSON.stringify({{ecommerce}} || '{}')); if (ecommerce.purchase && ecommerce.purchase.actionField) { ecommerce.purchase.actionField.affiliation = 'My Updated Store'; } return {ecommerce: ecommerce}; } Use JSON.parse(JSON.stringify({{variable}})) to create a copy of the object to avoid any changes made to the custom JavaScript variable being reflected in the original object.

3. Enable Enhanced Ecommerce settings for the Universal Analytics tag(s) you'll be using to send the data to Google Analytics servers.

Open the Universal Analytics tag you’ll use to send the Enhanced Ecommerce data to Google. Navigate to More Settings > Ecommerce. Enable Enhanced Ecommerce features. Do not tick Use Data Layer. Select the variable you created in the previous step under Read Data from Variable.

4. Use Preview Mode in Google Tags Manager to confirm that data is being mapped to the variable.

Navigate to Preview Mode. Perform the action that triggers the Enhanced Ecommerce tag to fire (such as loading a product page or adding an item to your cart). Select the trigger event for the Enhanced Ecommerce tag in Preview Mode. Open the Variables tab and check that the variable you created earlier returns the correct {ecommerce: ...} object. Use a tool like the Google Analytics Debugger browser extension to check the outgoing requests and confirm that the data is being mapped.