Add Baidu Statistics E-commerce Analytics Tracking Code in WooCommerce
Add Baidu statistics in WooCommerce is very simple, directly in the code before adding Baidu statistics JS code can be, some themes and plug-ins also provide a direct option to add statistics code in the background, directly paste the Baidu statistics code to the corresponding settings inside the option can be.
However, if you want to use the e-commerce analytics of Baidu Statistics, you need the webmaster to have some code writing skills and have some knowledge of WooCommerce.
According to Baidu Statistics documentation, the e-commerce analytics code needs to be added after the order completion page, Baidu Statistics asynchronous JS code. The e-commerce analytics code needs to get some order data, such as skuID, product category, price, quantity, etc. We need to extract this data from the WooCommerce order and add it to the e-commerce analytics JS.
获取百度电商分析需要的订单数据
The following code is used on the order completion page, so we can just use the $order_id variable.
$order = wc_get_order($order_id);
$items = $order->get_items();
$product_js = [];
foreach ($items as $item_id => $item_data) {
$_product = wc_get_product($item_data->get_data()[ 'product_id' ]);
$pro_cat_array = wp_get_post_terms($_product->ID, 'product_cat');
$sku = $sku = $_product->get_sku();
$qty = $item_data->get_quantity();
$pro_cat = implode(',', $pro_cat_array);
$product_name = $_product->get_name();
$pro_price = $item_data->get_data()[ 'total' ];
$product_js[] = [
'skuId' => $sku,
'Quantity' => $qty,
];
}添加订单数据到百度电商分析代码中
获取了上面的订单数据之后,我们只需把数据添加到百度电商分析代码中即可,如下:
_hmt.push([
'_trackOrder', {
'orderId' : '',
'orderTotal': 'get_total(); ? >',
'item' : ,, 'item' : <?
}
]);
</script完成上面的代码之后,我们使用 woocommerce_thankyou Hooks add the JS code above to the order completion page to do so.