php - Global Site Tag not working with WordPress, WooCommerce
问题描述
I'll love to know what is wrong in the code below. I'm using WordPress + WooCommerce and my site tag makes my website crash when I upload this functions.php -> The global site tag is in the head, detected by the google tag chrome extension -> The conversion tracking code is in functions.php and it need to be visible only in the thank you page when the order is done.
Here is my functions.php page.
Any help welcomed, I'm just getting started with PHP.
Julien,
<?php
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'conversion_tracking' );
function conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
// This is the order total
$order_total = $order->get_total();
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
}
}
<?php
}
/*-----------------------------------------------------------------------------------*/
/* Init theme framework
/*-----------------------------------------------------------------------------------*/
require( 'auxin/auxin-include/auxin.php' );
/*-----------------------------------------------------------------------------------*/
Here is some woocommerce and google example page https://support.google.com/searchads/answer/9133542?hl=fr https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/#
解决方案
This should do (there are some function parts you don't need + you need to echo the script, as you're inside PHP):
add_action( 'woocommerce_thankyou', 'bloomer_conversion_tracking' );
function bloomer_conversion_tracking( $order_id ) {
$order = wc_get_order( $order_id );
?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
<?php
}
推荐阅读
- python - Python - 将 **kwargs 传播到类似于 ES6 传播的字典中
- configuration - 如何将作曲家添加到 winginx?
- google-apps-script - 使用相同函数从不同范围返回数据的方法
- reactjs - 在组件渲染中传递状态的 React 简写
- r - 在 igraph 中使用 closeness() 时的警告消息
- xamarin.android - 从 Firebase 存储中检索图像
- laravel - Twitter OAuth 访问令牌错误:请求令牌丢失
- java - 本地选项集上的 InsertOptionValue 操作
- python - Keras 卡在第一个 Epoch
- python - 挂钩到内置的 python f-string 格式机制