Search code examples
google-analyticsgoogle-tag-managergoogle-datalayer

Manipulate dataLayer using Google tag manager


Is it possible to get a value from the dataLayer, modify it and push it back to the dataLayer?

I used this custom HTML for that, but it returns NaN (Not a Number), thoughts?

<script type="text/javascript">
(function() {
  amsdataLayer.push({
    'roomcount' : 6*('roomcount').float()
  });
    
})(); 
</script>


Solution

  • If roomcount is currently in the dataLayer (for precisions sake: if it is in the dataLayer and you access it via a GTM variable) you need to use double curly brackets to access it, i.e. {{roomcount}}. Currently you are applying your calls to the literal string "roomcount", which does not result in a number (also, is there really a float() method for numbers in JS ? Currently I can only think of parseFloat which has a different syntax).

    Also you need a (native or custom) event to make GTM aware of changes to the dataLayer.