Search code examples
google-tag-managergoogle-datalayer

Trying to pull value from Data Layer in Google tag manager


Trying to pull "hash" value for 'Scramble_ip' event. Any idea how this can be done? I can't use ([n]) event numbers because the number of events varies from page to page. Can I call the event by name? Thank you!

enter image description here


Solution

  • Your problem is that technically you've allowed your value to be overwritten by a different event's value that is using hash key too. Stop giving simple/repetitive names yo your properties and you will be good.

    To fix this your best action would be to change the name of the hash, then make a DL variable in GTM for the new name and then use that.

    If you can't change the name of the variable, then you still can hack around it with either:

    1. a piece of JS that iterates through your DL and pulls your value. So the core of it would be something like:

    function(){
      return dataLayer.find(function(obj){return obj.event==="Scramble_IP"}).hash;
    }

    1. solve it via a mostly UI set up by making a Scramble_IP Data Layer Event trigger and have custom HTML tag using it that would just save the value of your dataLayer Value variable 'hash' to a cookie or a different variable, or a local JS variable, or to another DL variable. As you see fit really.