简体   繁体   English

在带有 querySelectors 的 Google Tag Manager 中使用 IF 条件

[英]Using IF conditional in Google Tag Manager with querySelectors

Issue问题

Keep getting 'undefined' as returned google tag manager variable.不断获取“未定义”作为返回的谷歌标签管理器变量。

Description描述

I am trying to write a custom javascript variable to pull 'data-name' IF the 'data-form-title' returns ""我正在尝试编写一个自定义 javascript 变量来拉取'data-name'如果'data-form-title'返回“”

I have a website I am trying to tag and need to grab the form names.我有一个要标记的网站,需要获取表单名称。 The problem came into play where some of the forms' names come back as "" so I wanted to use IF/Then logic to choose another attribute if the first came back empty.问题出现了,其中一些表单的名称返回为“”,因此我想使用 IF/Then 逻辑来选择另一个属性,如果第一个返回为空。

Form that has proper form title https://www.mcgeetoyotaofclaremont.com/popup-availability具有正确表格标题的表格https://www.mcgeetoyotaofclaremont.com/popup-availability

Form that has empty "" form title ( 'text yourself a link' ) https://www.mcgeetoyotaofclaremont.com/vehicle-details/new-2020-toyota-yaris-hatchback-le-claremont-nh-id-33375400#具有空“”表单标题的表单( “给自己发链接”https://www.mcgeetoyotaofclaremont.com/vehicle-details/new-2020-toyota-yaris-hatchback-le-claremont-nh-id-33375400#

What I've Tried我试过的

I've tested both querySelectors and they both work on their own.我已经测试了两个 querySelector,它们都可以独立工作。 It's when I try to make the IF condition that I run into issues.当我尝试使 IF 条件遇到问题时。

I have also tried var answer = 'Unknown' and then replacing the variable with either formtitle or datatitle , depending on the conditional, so that the script only had 1 return in the function.我也试过var answer = 'Unknown'然后根据条件用formtitledatatitle替换变量,这样脚本在函数中只有 1 个返回。

For the life of me this seems simple and when I cross-check other examples (such as taking the name out of the function) it seems it should work fine.对于我的生活来说,这似乎很简单,当我交叉检查其他示例(例如从函数中取出名称)时,它似乎应该可以正常工作。

Current Code当前代码

function() {

var formtitle = document.querySelectorAll('form[data-form-title]')[0].attributes['data-form-title'].nodeValue;

var datatitle = document.querySelectorAll('form[data-form-title]')[0].attributes['data-name'].nodeValue;


if (formtitle != ""){
    return formtitle;
    } else {
    return datatitle;
    }   
}

When I try to run your current code on your forms I get an error: Uncaught TypeError: Cannot read property 'nodeValue' of undefined .当我尝试在表单上运行当前代码时,出现错误: Uncaught TypeError: Cannot read property 'nodeValue' of undefined This error would make your GTM function return undefined every time.此错误会使您的 GTM 函数每次都返回未定义。

the problem seems to be this line of code:问题似乎是这行代码:

var datatitle = document.querySelectorAll('form[data-form-title]')[0].attributes['data-name'].nodeValue;

there is no data-name attribute.没有data-name属性。 So you either need to make sure you are trying to access the right attribute or you can simply surround your variables in try-catch blocks like this:因此,您要么需要确保尝试访问正确的属性,要么只需将变量包围在 try-catch 块中,如下所示:

function() {
  var formtitle = document.querySelectorAll('form[data-form-title]')[0].attributes['data-form-title'].nodeValue;

  var datatitle = '';
  try{
    datatitle = document.querySelectorAll('form[data-form-title]')[0].attributes['data-name'].nodeValue;
  }catch(err){}


  if (formtitle != ""){
    return formtitle;
  } else {
    return datatitle;
  }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM