Search code examples
javascriptgoogle-tag-manager

How to get specific element from page to variable in Google Tag Manager


I have this kind of cookie in code on page. I would like to get language out of it.

I mean how could I this function return in this example "en" in GTM variable:

function() {return ...}

Code in page looks like:

<script type="text/javascript" id="wtml-cookie-js-extra">
var wtml_cookies = {"wp-wtml_current_language":{"value":"en","expires":1,"path":"\/"}};
var wtml_cookies = {"wp-wtml_current_language":{"value":"en","expires":1,"path":"\/"}};</script>

Solution

  • Global variables like that are accessible in the window object. This code should do it:

    function() {
      return window.wtml_cookies["wp-wtml_current_language"]["value"];
    }