google-tag-managergoogle-datalayer

How to you combine two dataLayer variables into one in Google Tag Manager


There are two dataLayer variables on the page. How do you combine both of them into one single variable so that it can be pushed into analytics ?

dataLayer current format PageName: AIR_SEARCH_PAGE Flow:BOOKING

I want to create another variable which is XYZ and the result to be displayed as BOOKING:AIR_SEARCH_PAGE. How to achieve this ? XYZ:BOOKING:AIR_SEARCH_PAGE


Solution

  • There are several options here:

    1. Combine the strings through GTM. For example, looks like you have the following dataLayer:

      dataLayer = [{
      'PageName': 'AIR_SEARCH_PAGE',
      'Flow': 'BOOKING'
      }]
      

      In GTM, you could create a DL variable for 'PageName', and another one for 'Flow', and then when you need to combine them, or add other text around it, you can say:

      XYZ: {{Flow}}: {{PageName}}
      

      so this would render as "XYZ: BOOKING: AIR_SEARCH_PAGE"

    2. Use the JS string manipulation and join the strings together and then push to the dataLayer again.

    3. Push a new parameter into the dataLayer that combines the strings for you.

      dataLayer.push({
         'xyz': 'BOOKING:AIR_SEARCH_PAGE'
      })
      

      and then use that new variable through GTM.

    I think the first method may be best.