Map data using a variable

Business Benefits

Make it easier for your organization to deploy an Enhanced Ecommerce dataLayer.


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.

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.

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

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

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

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

Last edited by @hesh_fekry 2023-11-14T11:10:34Z