Skip to main content

Step 2: Create Variable in Google Tag Manager

In this step, you'll create a variable in Google Tag Manager that contains the value you want to pass to Ternair.

Instructions​

  1. Open Google Tag Manager
  2. Go to Variables in the left menu
  3. Under "User-Defined Variables", click New
  4. Give the variable a clear name (e.g., dlv - Product ID)
  5. Click Variable Configuration
  6. Choose the correct type:
    • Data Layer Variable — for values directly from the dataLayer
    • Custom JavaScript — for processed or derived values
  7. Configure the variable as needed
  8. Click Save

Variable types​

Data Layer Variable​

Use this type when the value is directly available in the dataLayer. For example:

  • ecommerce.items.0.item_id
  • user.id
  • page.category

Custom JavaScript​

Use this type when you need to process or derive the value. For example, when you need to extract a value from an array.

Example: Custom JavaScript variable​

function() {
var ecommerce = {{dlv – ecommerce.items}};
if (ecommerce && ecommerce.length > 0) {
return ecommerce[0].product_id;
}
return null;
}

This function:

  1. Retrieves the ecommerce items array from the dataLayer
  2. Checks if there are items
  3. Returns the product_id of the first item
  4. Returns null if there are no items
Tip

Give your variables a consistent naming convention, for example with prefix dlv - for Data Layer variables or js - for JavaScript variables.

Next step​

Go to Step 3: Link Variable to connect the variable to the Ternair tag.