自定义 javascript 变量 GTM 中的解析错误

发布于 2025-01-20 20:27:53 字数 1307 浏览 2 评论 0原文

如何基于2个单独的当前变量创建新变量。主要条件是检查数组或小部件选择器的存在,然后返回其值:

    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
          }
        }
      }
    }

第二个是

    function() {
       var array = document.querySelector('[class="btn-new btn-fullwidth popup-trigger"]');
           return {
           'ecommerce': {
          'promoView': {
          "name": "Widget - Personal - " + array.dataset.targetId
          }
        }};
    }

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
          }
        }};
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻安 2025-01-27 20:27:53

可以通过检查第一个选择器结果中的 array.length 来组合这两个函数。

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
      }
    }
  };
}

请参阅检查 array.length > 0 。如果找到该选择器,它将使用 x 返回促销信息。否则,代码将继续并找到另一个选择器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文