加载两次 WooCommerce 谢谢页面会重复转换跟踪吗?
Loading twice WooCommerce thankyou page would duplicate Conversion tracking?
我在我的 woocommerce 商店的 tankyou 页面中实施了一个自定义脚本来跟踪 google 广告转化。这是我的实现:
add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
function pixel_analytics_conversion_track_script( $order_id ) {
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order instanceof WC_Order ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
console.log('PURCHACE EVENT');
/* Track conversion on facebook Pixel */
fbq('track', 'Purchase',
{
value: <?php echo $order_total ?>,
currency: "<?php echo $order_currency ?>"
});
/* Track conversion on Google Ads */
gtag('event', 'conversion',
{
'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC',
'value': <?php echo $order_total ?>,
'currency': "<?php echo $order_currency ?>",
'transaction_id': "<?php echo $order_id ?>"
});
});
</script>
<?php
}
}
}
}
代码运行良好,但有些数据不准确,我认为如果用户两次访问感谢页面,上面的代码可能会重复转换。我们有一封订单确认电子邮件,其中包含 link 到 woocommerce 的感谢页面。
如您所见,我发送了 transaction_id
参数,所以我的问题是:
如果用户加载感谢页面两次或 N 次,即使您发送 transaction_id
参数,转化也会在 Google 广告中重复出现?
您可以使用订单自定义元数据来避免重复的转化跟踪,如下所示:
add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
function pixel_analytics_conversion_track_script( $order_id ) {
// Avoid if pixel analytics conversion track script has been run before
if ( $order_id > 0 && 'done' !== get_post_meta( $order_id, '_pixel_tracking', true ) ) {
$order = wc_get_order( $order_id );
if ( is_a($order, 'WC_Order') ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
console.log('PURCHACE EVENT');
/* Track conversion on facebook Pixel */
fbq('track', 'Purchase',
{
value: <?php echo $order_total ?>,
currency: "<?php echo $order_currency ?>"
});
/* Track conversion on Google Ads */
gtag('event', 'conversion',
{
'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC',
'value': <?php echo $order_total ?>,
'currency': "<?php echo $order_currency ?>",
'transaction_id': "<?php echo $order_id ?>"
});
});
</script>
<?php
// Flag the order (with custom meta data) to avoid pixel analytics conversion track script run multiple times.
update_post_meta( $order_id, '_pixel_tracking', true );
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。
我在我的 woocommerce 商店的 tankyou 页面中实施了一个自定义脚本来跟踪 google 广告转化。这是我的实现:
add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
function pixel_analytics_conversion_track_script( $order_id ) {
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order instanceof WC_Order ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
console.log('PURCHACE EVENT');
/* Track conversion on facebook Pixel */
fbq('track', 'Purchase',
{
value: <?php echo $order_total ?>,
currency: "<?php echo $order_currency ?>"
});
/* Track conversion on Google Ads */
gtag('event', 'conversion',
{
'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC',
'value': <?php echo $order_total ?>,
'currency': "<?php echo $order_currency ?>",
'transaction_id': "<?php echo $order_id ?>"
});
});
</script>
<?php
}
}
}
}
代码运行良好,但有些数据不准确,我认为如果用户两次访问感谢页面,上面的代码可能会重复转换。我们有一封订单确认电子邮件,其中包含 link 到 woocommerce 的感谢页面。
如您所见,我发送了 transaction_id
参数,所以我的问题是:
如果用户加载感谢页面两次或 N 次,即使您发送 transaction_id
参数,转化也会在 Google 广告中重复出现?
您可以使用订单自定义元数据来避免重复的转化跟踪,如下所示:
add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
function pixel_analytics_conversion_track_script( $order_id ) {
// Avoid if pixel analytics conversion track script has been run before
if ( $order_id > 0 && 'done' !== get_post_meta( $order_id, '_pixel_tracking', true ) ) {
$order = wc_get_order( $order_id );
if ( is_a($order, 'WC_Order') ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
console.log('PURCHACE EVENT');
/* Track conversion on facebook Pixel */
fbq('track', 'Purchase',
{
value: <?php echo $order_total ?>,
currency: "<?php echo $order_currency ?>"
});
/* Track conversion on Google Ads */
gtag('event', 'conversion',
{
'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC',
'value': <?php echo $order_total ?>,
'currency': "<?php echo $order_currency ?>",
'transaction_id': "<?php echo $order_id ?>"
});
});
</script>
<?php
// Flag the order (with custom meta data) to avoid pixel analytics conversion track script run multiple times.
update_post_meta( $order_id, '_pixel_tracking', true );
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。