I have an image tag that I need to populate with the information from the eCommerce purchase. I need the individual items that are stored as a 'product' object in the Tagmanager datalayer. As per Google Analytics Enhanced Ecommerce tracking:
我有一个图像标签,我需要填写电子商务购买的信息。我需要在Tagmanager数据层中存储为“产品”对象的各个项目。根据Google Analytics增强型电子商务跟踪:
dataLayer.push({
'ecommerce': {
'purchase': {
'actionField': {
'id': 'T12345', // Transaction ID. Required for purchases and refunds.
'affiliation': 'Online Store',
'revenue': '35.43', // Total transaction value (incl. tax and shipping)
'tax':'4.90',
'shipping': '5.99',
'coupon': 'SUMMER_SALE'
},
'products': [{ // List of productFieldObjects.
'name': 'Triblend Android T-Shirt', // Name or ID is required.
'id': '12345',
'price': '15.25',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Gray',
'quantity': 1,
'coupon': '' // Optional fields may be omitted or set to empty string.
},
{
'name': 'Donut Friday Scented T-Shirt',
'id': '67890',
'price': '33.75',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'quantity': 1
}]
}
}
});
So now I need to iterate through the products to build the image URL like:
所以现在我需要遍历产品来构建图像URL,如:
http://some-domain.com/?p1=12345&v1=15.25&p2=67890&v2=33.75...
Because the data is already in the dataLayer, I should be able to write some JavaScript to retrieve the product object, iterate over it's elements and create the string part that I need to append to the image URL. Makes sense, no?
因为数据已经在dataLayer中,所以我应该能够编写一些JavaScript来检索产品对象,迭代它的元素并创建我需要附加到图像URL的字符串部分。有道理,不是吗?
BUT: I do not know how to retrieve the product element within a tag. I tried creating the product object as a variable in tag manager, but that doesn't seem to work. I tried accessing it via:
但是:我不知道如何检索标签中的product元素。我尝试将产品对象创建为标记管理器中的变量,但这似乎不起作用。我尝试通过以下方式访问它
google_tag_manager["GTM-uniqueid"].dataLayer.get('ecommerce').purchase.products
but again that doesn't work.
但这又不起作用。
2 个解决方案
#1
0
One option would be to include an event in the dataLayer, for example:
一种选择是在dataLayer中包含一个事件,例如:
dataLayer.push({
'event': 'ecomm event',
'ecommerce': {
'purchase': {
'actionField': {
'id': 'T12345', // Transaction ID. Required for purchases and refunds.
// .... your original stuff
}]
}
}
});
In GTM, you would create a variable that would loop through that dataLayer when that event happens, and return the product names, something like this (you can tidy it up a bit):
在GTM中,您将创建一个变量,在该事件发生时循环遍历该dataLayer,并返回产品名称,如下所示(您可以稍微整理一下):
function(){
var dl = {{DL - Ecomm products}};
var prodNames = '';
for (var i=0; i<dl.length; i++){
prodNames += dl[i].name + '|';
}
return prodNames;
}
where {{DL - Ecomm products}}
is a dataLayer variable with a value of ecommerce.purchase.products
.
其中{{DL - Ecomm产品}}是一个dataLayer变量,其值为ecommerce.purchase.products。
#2
0
For those that struggle with the same:
对于那些与之斗争的人:
- Create a DL Variable as ecommerce.purchase.products
- Create a Custom JS Variable in GTM as:
创建DL变量作为ecommerce.purchase.products
在GTM中创建自定义JS变量:
function() { var prods = {{DL - products}}; var src = "http://YOUR URL/?a=1"; var len = prods.length; for (i=0; i < len; i++) { var b = i+1; src = src + "&ITEM" + b + "=" + prods[i].id + "&AMT" + b + "=" + prods[i].price + "&QTY" + b + "=" + prods[i].quantity; } src = src + "&CURRENCY=GBP"; return src; }
- Traffic the image tag as your Custom JavaScript Variable.
将图片代码作为自定义JavaScript变量进行投放。
Done
PS: Sorry about the bad formatting.
PS:抱歉格式不正确。
#1
0
One option would be to include an event in the dataLayer, for example:
一种选择是在dataLayer中包含一个事件,例如:
dataLayer.push({
'event': 'ecomm event',
'ecommerce': {
'purchase': {
'actionField': {
'id': 'T12345', // Transaction ID. Required for purchases and refunds.
// .... your original stuff
}]
}
}
});
In GTM, you would create a variable that would loop through that dataLayer when that event happens, and return the product names, something like this (you can tidy it up a bit):
在GTM中,您将创建一个变量,在该事件发生时循环遍历该dataLayer,并返回产品名称,如下所示(您可以稍微整理一下):
function(){
var dl = {{DL - Ecomm products}};
var prodNames = '';
for (var i=0; i<dl.length; i++){
prodNames += dl[i].name + '|';
}
return prodNames;
}
where {{DL - Ecomm products}}
is a dataLayer variable with a value of ecommerce.purchase.products
.
其中{{DL - Ecomm产品}}是一个dataLayer变量,其值为ecommerce.purchase.products。
#2
0
For those that struggle with the same:
对于那些与之斗争的人:
- Create a DL Variable as ecommerce.purchase.products
- Create a Custom JS Variable in GTM as:
创建DL变量作为ecommerce.purchase.products
在GTM中创建自定义JS变量:
function() { var prods = {{DL - products}}; var src = "http://YOUR URL/?a=1"; var len = prods.length; for (i=0; i < len; i++) { var b = i+1; src = src + "&ITEM" + b + "=" + prods[i].id + "&AMT" + b + "=" + prods[i].price + "&QTY" + b + "=" + prods[i].quantity; } src = src + "&CURRENCY=GBP"; return src; }
- Traffic the image tag as your Custom JavaScript Variable.
将图片代码作为自定义JavaScript变量进行投放。
Done
PS: Sorry about the bad formatting.
PS:抱歉格式不正确。