1

How can i create new variable based on 2 separate current variable. The main condition is to check the existance of array or widget selector, and then return it's value:

    function() {
      var array = Array.prototype.slice.apply(document.querySelectorAll("div.widget-message.active.active-target a"));
      var length = array.length;
      var x = array.map(function(cn, index) {
        var nameId = cn.href.split("?")[0].split("/").slice(-1)[0].replace("-", " ");
        var urlParams = {{cjs - utility - url params}}(cn.href);
        var name = urlParams.promo_name;
        var creative = urlParams.promo_content;
        return {
          "name": "Widget - Personal - " + name,
          "id": "Widget - Personal - " + nameId + "-" + index,
          "position": "" + (index + 1) + "/" + length,
          "creative" : creative,
          "metric3": 1
        }
      });
      return {
        'ecommerce': {
          'promoView': {
            'promotions': x
          }
        }
      }
    }

and the second one is

    function() {
       var array = document.querySelector('[class="btn-new btn-fullwidth popup-trigger"]');
           return {
           'ecommerce': {
          'promoView': {
          "name": "Widget - Personal - " + array.dataset.targetId
          }
        }};
    }
5
  • if (var array = ... does not make sense for an if statement. When should the if be true, when length > 0 I presume? Commented Apr 11, 2022 at 12:51
  • i've changed the input conditions. My final goal was to create a new variable that would check both selectors. Commented Apr 11, 2022 at 14:00
  • Okay what would the combined variable do? Contain x AND the other selector's name? Or just one or the other? You can only return one thing in the function. Can you just have separate data layer variables for them and use them separately in TagManager? Or what it is that you want to combine? By the way, put the three ` in a separate line so the formatting shows. I'll edit for you. Commented Apr 11, 2022 at 14:23
  • thank you Peter for your help. So, the new function should check if the first selector is valid (exists on page) and return x value, or if it is not valid, it should check the 2nd condition (2selector) and return it's value. Commented Apr 12, 2022 at 8:16
  • I have posted a possible answer for you. Keep in mind StackOverflow is not a code writing service. You should hire a developer instead of fishing for code. Commented Apr 12, 2022 at 9:25

1 Answer 1

1

Combining both functions can be done by checking array.length in the result of the first selector.

function() {
  
  var array = Array.prototype.slice.apply(document.querySelectorAll("div.widget-message.active.active-target a"));
  var x = array.map(function(cn, index) {
    var nameId = cn.href.split("?")[0].split("/").slice(-1)[0].replace("-", " ");
    var urlParams = {{cjs - utility - url params}}(cn.href);
    return {
      "name": "Widget - Personal - " + urlParams.promo_name,
      "id": "Widget - Personal - " + nameId + "-" + index,
      "position": "" + (index + 1) + "/" + array.length,
      "creative" : urlParams.promo_content,
      "metric3": 1
    }
  });

  if (array.length > 0) {
    return {
      'ecommerce': {
        'promoView': {
          'promotions': x
        }
      }
    }
  }
  
  array = document.querySelector('[class="btn-new btn-fullwidth popup-trigger"]');
  return {
    'ecommerce': {
      'promoView': {
        "name": "Widget - Personal - " + array.dataset.targetId
      }
    }
  };
}

See checking for array.length > 0. If that selector is found it will return the promotions using x. Otherwise the code continues and finds the other selector.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, glad to help! Vote up and mark it as the answer if you like 👍🏻

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.