javascriptgoogle-tag-managergoogle-datalayer

DataLayer Variable Returns Undefined - JavaScript GTM


GTM is returning 'Undefined' when I use the DataLayer Variable call in my JS.

  1. In the console log of the page I type, google_tag_manager['GTM-XXX'].dataLayer.get('ecommerce.transaction_id') and it returns the correct value.
  2. When I use dataLayer[5].ecommerce.transaction_id it also returns the correct value.enter image description here
  3. Via GTM using {{dlv - ecommerce.transaction.id }} returns 'undefined'. I added a console log to see the output for debug reasons.

enter image description here

Here is what the GTM Variable Config looks like: enter image description here


Solution

  • Don't use console logs to debug GTM. Use GTM preview.

    In GTM preview. select the event at which you expect your variable to appear, go to variables and see its value. It's supposed to be undefined there. Now go to your datalayer and see what's there. The transaction is likely to not be there at the moment of your query. Go through events, see where the dataLayer event push happens. That's where you're supposed to access your variable.

    If not, add screenshots from the preview.

    Also you should indicate whether this is GA4 EEC DL structure.