Google Tag Manager Parse Error. Primary Expression

蹲街弑〆低调 提交于 2019-12-11 10:51:20

问题


I am trying to save this Custom Javascript Variable within Google Tag Manager and return this error each time:

Error at line 2, character 4: Parse error. primary expression expected

var prodID = function pID() {
return document.querySelector(".no-display > input[name=product]").value;
}();

Can someone help me? I believe I have a primary expression.


回答1:


To fix the issue in Google tag manger, I simple re wrote the code to be :

function() {
  return document.querySelector(".no-display > input[name=product]").value;
}



回答2:


All code in a custom javascript variable should be within function() {}, or you will get this error.

Here's the documentation from Google:

This field should be a JavaScript function that returns a value using the 'return' statement. If the function does not explicitly return a value, it will return undefined and your container may not behave as expected. Below is an example of this field:

function() {
     var now = new Date();
     return now.getTime();   
}


来源:https://stackoverflow.com/questions/32418777/google-tag-manager-parse-error-primary-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!