如果特定产品ID,则不包括GTM购买活动的价值
我有一个WordPress/WooCommerce网站,该网站使用GTM4WP插件将网站数据发送到GTM Datalayer。在GTM中,我已经成功设置了一个购买事件,该事件将所有数据发送到GA4接口。到目前为止,一切都很好。
现在,我的网站上有两个项目(ID 224112和159324),我不想在我的GA4帐户中看到收入。 (他们触发购买事件的事实并不是问题,而是收入价值应该为零。)
我一直在搜索数小时并发现我可以执行自定义的JavaScript功能,应该可以解决问题。除了我不太熟练JavaScript ...
这是我发现的指示:
//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();
}
这两个项目的价值都不同,我还运行了一个多货币网站,因此我们唯一可以使用的就是ID。
当我通过GTM预览模式进行测试购买时,我可以在购买事件的数据层中看到以下值(我xxx xxx删除了一些不需要的东西):
{
event: "purchase",
gtm: {uniqueEventId: 11, start: 1654376017109},
visitorLoginState: "logged-in",
visitorType: "XXX",
pagePostType: "page",
pagePostType2: "single-page",
browserName: "Chrome",
browserVersion: "102.0.5005.61",
browserEngineName: "Blink",
browserEngineVersion: "",
deviceType: "desktop",
deviceManufacturer: "Apple",
deviceModel: "Macintosh",
cartContent: {
totals: {
applied_coupons: [],
discount_total: 0,
subtotal: 0,
total: 0
},
items: []
},
orderData: {
attributes: {
date: "2022-06-04T22:53:18+00:00",
order_number: "226813",
order_key: "wc_order_Z6HrzhCqQ3pTS",
payment_method: "pensopay",
payment_method_title: "Credit/Debit card",
shipping_method: "3-5 days",
status: "processing",
coupons: ""
},
totals: {
currency: "DKK",
discount_total: "0",
discount_tax: "0",
shipping_total: "40",
shipping_tax: "0",
cart_tax: "0",
total: "90.00",
total_tax: "0",
total_discount: "0",
subtotal: "50",
tax_totals: []
},
customer: {
id: 3,
billing: {
first_name: "XXX",
last_name: "XXX",
company: "",
address_1: "XXX",
address_2: "",
city: "XXX",
state: "",
postcode: "XXX",
country: "DK",
email: "XXX",
emailhash: "dafb4416fe8aa737d5b09733b4d3310f6ca31276bebe6dd24" +
"bcbe1651fe4abde",
phone: "XXX"
},
shipping: {
first_name: "XXX",
last_name: "XXX",
company: "",
address_1: "XXX",
address_2: "",
city: "XXX",
state: "",
postcode: "XXX",
country: "DK"
}
},
items: [
{
id: 224112,
name: "Materials",
sku: 224112,
category: "XXX",
price: 50,
stocklevel: null,
brand: "XXX",
quantity: 1
}
]
},
new_customer: false,
ecommerce: {
currencyCode: "DKK",
purchase: {
actionField: {
id: "226813",
affiliation: "",
revenue: 90,
tax: 0,
shipping: 40,
coupon: ""
},
products: [
{
id: 224112,
name: "Materials",
sku: 224112,
category: "XXX",
price: 50,
stocklevel: null,
brand: "XXX",
quantity: 1
}
]
},
currency: "DKK",
items: [
{
item_id: 224112,
item_name: "Materials",
item_brand: "XXX",
price: 50,
item_category: "XXX",
quantity: 1,
google_business_vertical: "retail",
id: "224112"
}
],
transaction_id: "226813",
affiliation: "",
value: 90,
tax: 0,
shipping: 40,
coupon: ""
}
}
I have a wordpress/woocommerce website which uses the GTM4WP plugin to send the website data to the GTM datalayer. In GTM I have successfully setup a purchase event which sends all data to the GA4 interface. So far so good.
Now there are two items (ID 224112 and 159324) on my website for which I do NOT want to see the revenue in my GA4 account. (The fact that they trigger a purchase event is not so much the problem but the revenue value should be ZERO.)
I have been searching for hours and found out I can do a custom Javascript function which should do the trick. Except I'm not very skilled with Javascript...
This is the instruction I found:
//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();
}
The value of both of these items can be different, I also run a multicurrency website, so the only thing we can go by is the ID.
When I do a test-purchase through the GTM preview mode I can see the following values in the data layer of the purchase event (I XXXed out a few things which are not needed here):
{
event: "purchase",
gtm: {uniqueEventId: 11, start: 1654376017109},
visitorLoginState: "logged-in",
visitorType: "XXX",
pagePostType: "page",
pagePostType2: "single-page",
browserName: "Chrome",
browserVersion: "102.0.5005.61",
browserEngineName: "Blink",
browserEngineVersion: "",
deviceType: "desktop",
deviceManufacturer: "Apple",
deviceModel: "Macintosh",
cartContent: {
totals: {
applied_coupons: [],
discount_total: 0,
subtotal: 0,
total: 0
},
items: []
},
orderData: {
attributes: {
date: "2022-06-04T22:53:18+00:00",
order_number: "226813",
order_key: "wc_order_Z6HrzhCqQ3pTS",
payment_method: "pensopay",
payment_method_title: "Credit/Debit card",
shipping_method: "3-5 days",
status: "processing",
coupons: ""
},
totals: {
currency: "DKK",
discount_total: "0",
discount_tax: "0",
shipping_total: "40",
shipping_tax: "0",
cart_tax: "0",
total: "90.00",
total_tax: "0",
total_discount: "0",
subtotal: "50",
tax_totals: []
},
customer: {
id: 3,
billing: {
first_name: "XXX",
last_name: "XXX",
company: "",
address_1: "XXX",
address_2: "",
city: "XXX",
state: "",
postcode: "XXX",
country: "DK",
email: "XXX",
emailhash: "dafb4416fe8aa737d5b09733b4d3310f6ca31276bebe6dd24" +
"bcbe1651fe4abde",
phone: "XXX"
},
shipping: {
first_name: "XXX",
last_name: "XXX",
company: "",
address_1: "XXX",
address_2: "",
city: "XXX",
state: "",
postcode: "XXX",
country: "DK"
}
},
items: [
{
id: 224112,
name: "Materials",
sku: 224112,
category: "XXX",
price: 50,
stocklevel: null,
brand: "XXX",
quantity: 1
}
]
},
new_customer: false,
ecommerce: {
currencyCode: "DKK",
purchase: {
actionField: {
id: "226813",
affiliation: "",
revenue: 90,
tax: 0,
shipping: 40,
coupon: ""
},
products: [
{
id: 224112,
name: "Materials",
sku: 224112,
category: "XXX",
price: 50,
stocklevel: null,
brand: "XXX",
quantity: 1
}
]
},
currency: "DKK",
items: [
{
item_id: 224112,
item_name: "Materials",
item_brand: "XXX",
price: 50,
item_category: "XXX",
quantity: 1,
google_business_vertical: "retail",
id: "224112"
}
],
transaction_id: "226813",
affiliation: "",
value: 90,
tax: 0,
shipping: 40,
coupon: ""
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,最好首先不允许您不想在DL中拥有的东西。由于您只使用插件,因此我建议更改插件逻辑以忽略指定的产品,或更改数据。只需将这些产品的价格设置为适当的值即可。
另一种方法是通过GA报告。只需从报告中排除您不感兴趣的产品ID即可。
我建议这些方法是因为这实际上不是GTM问题。通过GTM解决此问题将是一个黑客,我们试图避免生产中的黑客攻击。
现在,我们经历了这一点,是的,有几种方法可以通过GTM进行修复。一个显而易见的是,将仅在EEC触发器中添加CJS阻滞剂,以检查这些产品是否在电子商务对象中,并阻止事物如果在那里的情况。但这意味着将其阻止,以便在同一呼叫中可以跟踪的任何其他产品。
您还可以使用CJS变量重建整个电子商务对象,可以在此处找到详细的说明: https://www.simoahava.com/analytics/enhanced-ecommerce-with-a-custom-javascript-variable/ 如果您使用通用分析。
GA4也有类似的解决方案。一个简单的。在GA4中,您只需为您的EEC标签设置事件参数来覆盖DL EEC,该参数设置了
items
属性。您将使用CJS作为值。在您的CJ中,您应该通过获取DL中的内容并对其进行清洁,返回适当的物品。Well, it would be best to first not allow things that you don't want to have in DL. Since you just use plugins, then I would suggest to either make a change in the plugin logic to ignore specified products, or change the data. Just set the price for those products to the appropriate values.
Another approach to it would be through GA reporting. Just exclude the product ids you're not interested in from the reports.
I suggest these approaches because this is not a GTM issue really. Fixing this through GTM would be a hack and we try to avoid hacks in production.
Now that we went through that, yes, there are a few ways to fix it through GTM. An obvious one would be just to add a CJS blocker to your EEC trigger that would check if these products are in the ecommerce object and block the thing from firing if they're there. But this means blocking it for any other products that may be tracked in the same call.
You can also rebuild the whole ecommerce object with a CJS variable, the detailed explanation can be found here: https://www.simoahava.com/analytics/enhanced-ecommerce-with-a-custom-javascript-variable/ in case you're using Universal Analytics.
There's a similar solution for GA4. A simpler one. In GA4, you override your DL EEC push by just setting an event parameter for your EEC tag that sets the
items
property. You would use a CJS as a value. In your CJS you should return a proper array of items by taking what's in the DL and sanitizing it.