javascriptgoogle-tag-managergoogle-datalayer

Google Tag Manager - custom javascript to extract dataLayer variable


Firstly, apologies for the lack of technical know-how...

I have a datalayer variable in google tag manager which I want to extract information from using custom javascript but I'm not the most technical.

From the picture attached, I want to extract the 'Desc' part of each line separated by comma:

VCD-95K-ZL6-36T-22F, 40% Off Menswear

How would the custom javascript for this look like so I can enter it in google tag manager?

Thank you

enter image description here


Solution

  • Sure, here you go:

    ["Name:code, Desc:whatever desc is, the first desc basically", "Name:auto, Desc:the second description"]
    .map(function(obj){return obj.split(/, desc:/i).slice(-1)[0]}).join(",")

    The first line is just me trying to reproduce what you have in your DLV for testing purposes. The second line is what you'd have to do to your DLV, so your actual code in your custom jsvascript variable would look similar to this:

    function(){
      return {{dlv - ecommerce.purchase.actionField.coupon}}.map(function(obj){return obj.split(/, desc:/i).slice(-1)[0]}).join(",");
    }