Template: Object Keys Remapper

GTM variable template that renames object keys without custom JavaScript. Transform your dataLayer structure to match GA4 or any analytics platform requirements. Handles arrays, single objects, and nested object structures.

GTM Object Remapper

General Description

Renaming Object Keys in GTM: The Solution with “Object Keys Remapper” Template

Anyone working with Google Tag Manager often faces a common challenge: data available in the dataLayer or other variables doesn’t always match the structure required by analytics tools like Google Analytics 4. For example, an e-commerce product array might use keys like product_name and product_price, while GA4 expects item_name and price.

The “Object Keys Remapper” variable template was created precisely to solve this problem elegantly and without writing custom JavaScript code. Let’s see how it works.

Resources

GitHub Repository
Template Gallery

What Does the Template Do?

In short, this template takes input data (an object or array of objects) and modifies their key names according to a mapping table defined by the user.

Its great strength is flexibility:

  • Handles Arrays of Objects: Ideal for GA4’s items array. Example: [{...}, {...}].
  • Handles Single Objects: Useful if you need to modify a single data block. Example: {...}.
  • Handles Objects of Objects: If data is structured as an object containing other objects, the template automatically converts them to an array. Example: { item1: {...}, item2: {...} }.

The template normalizes the input and always returns an array of objects with renamed keys, ready to use in your tags.


Template Configuration

Using this template is very simple. Once added to your GTM workspace, you’ll need to configure two main parameters.

1. Input Data

In this field, insert the GTM variable containing the data to be processed. Typically this will be:

  • A data layer variable reading a product array (e.g., dlv - ecommerce.items).
  • Another custom variable returning an object or array.

2. Map Keys

This is a table where you define the “translation” rules. For each row, you must specify:

  • Original Key: The current key name in the original object (e.g., name).
  • New Key: The new name you want to assign to that key (e.g., item_name).

You can add as many rows as needed to map all necessary keys.

Practical Example

Imagine your dataLayer contains this object:

{
  product_id: 'TSHIRT-123',
  name: 'Logo T-Shirt',
  cost: 19.99,
  brand_name: 'MyBrand'
}

And you need to send it to GA4 with this structure:

{
  item_id: 'TSHIRT-123',
  item_name: 'Logo T-Shirt',
  price: 19.99,
  item_brand: 'MyBrand'
}

Your Map Keys configuration would be:

Original KeyNew Key
nameitem_name
costprice
brand_nameitem_brand
product_iditem_id

The variable will return the correctly formatted object ready for use.


Code Analysis

For the curious, the template’s core is sandboxed JavaScript code that performs the following steps:

  1. Input Normalization: Checks the input data type (inputArray).
    • If it’s a single object, places it in an array: {} becomes [{}].
    • If it’s an object of objects, extracts the values and creates an array: {a:{}, b:{}} becomes [{}, {}].
    • If it’s already an array, uses it directly.
  2. Mapping: Applies a loop (.map()) to the normalized array. For each object within, runs another loop (.forEach()) on the rules defined in the remapperValues table.
  3. Key Replacement: If a match is found (object.hasOwnProperty(row.oldKey)), copies the value to the new key (object[row.newKey] = object[row.oldKey]) and sets the old key to undefined to effectively remove it.
  4. Output: Returns the new array with all modified objects.

Benefits and Conclusions

Using the Object Keys Remapper template offers several advantages:

  • Less Custom Code: Avoids writing and maintaining complex custom JavaScript variables.
  • Maintainability: Makes the GTM container cleaner, more readable, and easier to manage.
  • Reusability: Once configured, it can be used in multiple tags.
  • Reliability: Automatically handles different data formats, reducing error risk.

In conclusion, it’s an indispensable utility tool for anyone needing to manipulate and standardize data within Google Tag Manager, especially in e-commerce contexts and external API integrations.