How to return a constant variable from custom js?

We are trying to capture click url in a custom js variable. The following returns undefined. Would anyone be able to help us out and kindly point out our error please :slight_smile:

function() {

    if ({{Click Url}}) {

        return {{Click Url}}.split(':')[1];



If we return a constant, say “a”, the function returns a. If we return {{Click Url}} without the if clause we still get undefined.

Have we missed some point in your documentation?

Thank you!

Hi @panukuulu, thanks for reaching out! There are two things to be considered here:

  • At the moment we don’t support using Tag Manager variables inside the Tag Manager variables, that’s why the setup above doesn’t work.

  • The built-in Tag Manager variable you’ve used above has all the characteristics of a custom variable (it can be used both as a trigger condition and as a variable in a tag).

Please let me know if using the split() method to get rid of the protocol (http: or https:) is the only reason you’ve decided to go for a custom js variable or is there anything else you would like to achieve? Once that’s clarified - we will be able to recommend the most convenient solution.

Thank you! :slight_smile:


Thank you for your response. Yes, split is the only reason.

The solution we found is creating a tag that picks up the click url, formats that and passes it to the data layer. We can then read the formatted value to a data layer variable and use that.

Is this a sensible solution?

Ok, thanks for clarifying this one for me. In this case - I’d go with a Custom JS variable. Please use the following code:

function() {
    document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = || e.srcElement,
        url = target.href.replace(/^https?\:\/\//i, "");
      window.clickUrlTrimed = url;
}, false);
  return window.clickUrlTrimed;

Please test it and let me know if that’s what you was looking for :slight_smile: