如何将增强型电子商务数据添加到WooCommerce单个产品页面中的数据层?



>我在WooCommerce文件中向我的子函数添加了以下代码.php并且不会将数据发送到数据层。有人可以告诉我错误在哪里吗?

目标是添加 google.https://developers.google.com/tag-manager/enhanced-ecommerce 在此处描述的展示数据层信息

下面数据层中的数据只是示例数据。

/**
* Add impression data layer to single product pages
*/
add_action( 'woocommerce_single_product_summary', 'custom_impdl');
function custom_impdl($product_id) {

	// Lets grab the product
	$product = wc_get_product( $product_id );
	?>
	<script>
// Measures product impressions and also tracks a standard
// pageview for the tag configuration.
// Product impressions are sent by pushing an impressions object
// containing one or more impressionFieldObjects.
dataLayer.push({
'event': 'product data',
	'ecommerce': {
'currencyCode': 'EUR',                       // Local currency is optional.
'impressions': [
{
'name': 'Triblend Android T-Shirt',       // Name or ID is required.
'id': '12345',
'price': '15.25',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Gray',
'list': 'Search Results',
'position': 1
},
{
'name': 'Donut Friday Scented T-Shirt',
'id': '67890',
'price': '33.75',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'list': 'Search Results',
'position': 2
}]
}
});
</script>
<?php
}

首先将脚本更改为:

<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'product data',
'ecommerce': {
'currencyCode': 'EUR', 
'impressions': [
{
'name': 'Triblend Android T-Shirt',
'id': '12345',
'price': '15.25',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Gray',
'list': 'Search Results',
'position': 1
},
{
'name': 'Donut Friday Scented T-Shirt',
'id': '67890',
'price': '33.75',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'list': 'Search Results',
'position': 2
}]
}
});
</script>

您忘了添加窗口.数据层 = 窗口.数据层 ||[]; window.dataLayer.push({到你的脚本。

所以你的完整代码应该看起来像这样:

<?php    
/**
* Add impression data layer to single product pages
*/

add_action( 'woocommerce_single_product_summary', 'custom_impdl');
function custom_impdl($product_id) {

// Lets grab the product
$product = wc_get_product( $product_id );
?>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'product data',
'ecommerce': {
'currencyCode': 'EUR', 
'impressions': [
{
'name': 'Triblend Android T-Shirt',
'id': '12345',
'price': '15.25',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Gray',
'list': 'Search Results',
'position': 1
},
{
'name': 'Donut Friday Scented T-Shirt',
'id': '67890',
'price': '33.75',
'brand': 'Google',
'category': 'Apparel',
'variant': 'Black',
'list': 'Search Results',
'position': 2
}]
}
});
</script>
<?php
}

最新更新