0
我想爲wooCommerce設置一個dataLayer謝謝你的頁面。看看這裏是我到目前爲止已經完成(該變量沒有完全建立至今):增強的電子商務dataLayer WooCommerce
function dataLayer_google_gtm($order_id) {
// Lets grab the order
$order = wc_get_order($order_id);
// Products
$products = $order->get_items();
?>
<script>
dataLayer.push({
'ecommerce': {
'purchase': {
'actionField':{
'id':'<?php echo $order->get_order_number(); ?>',
'affiliation':'CHACAFOODS',
'revenue':'<?php echo $order->get_order_total(); ?>',
'tax':'<?php echo $order->get_total_tax(); ?>',
'shipping':'<?php echo $order->get_shipping(); ?>',
'coupon':'<?php echo $order->get_order_discount_total(); ?>',
},
window['service'].push('products':,[
<?php
$count = 0;
foreach($products as $item_id => $item) {
$count++;
$product = $order->get_product_from_item($item); ?>
{
'name':<?php echo $item['name']; ?>',
'id':
'price': '<?php echo $order->get_line_subtotal($item); ?>',
'brand':
'category':
'variant':
'quantity': '<?php echo $item['qty']; ?>'
}
<?php if (count($order->get_items()) > $count) { echo ","; } ?>
<?php } ?>
]);
}
}
});
</script>
<?php
}
add_action('woocommerce_order_status_completed', 'prefix_service_conversion_tracking');
誰能告訴我,如果我的結構上看起來像在結果增強型電子商務以下結構?
谷歌數據層結構:
<script>
// Send transaction data with a pageview if available
// when the page loads. Otherwise, use an event when the transaction
// data becomes available.
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
}]
}
}
});
</script>
非常感謝您的幫助!
最好的問候, 安東
非常感謝@webizon。沒想到在這個話題上的答案:) – Toni2708
不客氣!這實際上是我寫的第一篇文章。如果你願意接受這個答案,我可以得到一些積分,我會很高興。乾杯! – webizon