使用 Javascript IF 函数创建 Google 跟踪代码管理器变量
Using Javascript IF function to create Google Tag Manager Variable
我正在尝试在 Google 跟踪代码管理器中创建自定义变量。我需要它在以下情况下显示 0 值...
客户使用包含字词 'Gift Voucher'
的折扣代码进行购买
AND
它产生负收入。
否则它应该只显示标准收入(有时需要负收入,因此为什么它应该只在使用礼券时显示为 0)
我尝试了以下方法
function () { if ({{Discount Code Used}} = str.includes("Gift Voucher") && {{Revenue}}<0 ) {return 0; } else { return {{Revenue}}; }}
但是这是返回一个未定义的值
有人能帮忙吗?
你的前半部分if
条件有点不对:
{{Discount Code Used}} = str.includes('Gift Voucher')
字符串的.includes()
方法的正确使用方法是这样的:
{{Discount Code Used}}.includes('Gift Voucher')
我建议使用 .indexOf()
instead of .includes()
,因为它受到更多浏览器的支持。
{{Discount Code Used}}.indexOf('Gift Voucher') !== -1
如果它仍然返回 undefined
,请仔细检查 Discount Code Used
和 Revenue
是否设置为正确的值。
我正在尝试在 Google 跟踪代码管理器中创建自定义变量。我需要它在以下情况下显示 0 值...
客户使用包含字词 'Gift Voucher'
的折扣代码进行购买
AND
它产生负收入。
否则它应该只显示标准收入(有时需要负收入,因此为什么它应该只在使用礼券时显示为 0)
我尝试了以下方法
function () { if ({{Discount Code Used}} = str.includes("Gift Voucher") && {{Revenue}}<0 ) {return 0; } else { return {{Revenue}}; }}
但是这是返回一个未定义的值
有人能帮忙吗?
你的前半部分if
条件有点不对:
{{Discount Code Used}} = str.includes('Gift Voucher')
字符串的.includes()
方法的正确使用方法是这样的:
{{Discount Code Used}}.includes('Gift Voucher')
我建议使用 .indexOf()
instead of .includes()
,因为它受到更多浏览器的支持。
{{Discount Code Used}}.indexOf('Gift Voucher') !== -1
如果它仍然返回 undefined
,请仔细检查 Discount Code Used
和 Revenue
是否设置为正确的值。