【问题标题】:how can i let google ads conversion tracking read the value of the conversion dynamically?如何让谷歌广告转化跟踪动态读取转化价值?
【发布时间】:2021-12-25 10:29:46
【问题描述】:

我正在尝试安装谷歌广告代码来跟踪我的 woocommerce 商店的转化率

这是应安装在订单确认页面中的代码

<!-- Event snippet for Purchase BSS conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
      'value': 0.0,
      'currency': 'USD',
      'transaction_id': ''
  });
</script>

我需要动态的值,

这是我在 function.php 中使用的代码,以便将此代码推送到感谢页面

<?php
// Add custom Theme Functions here
// 
/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {

    // Lets grab the order
    $order = wc_get_order( $order_id );

    /**
     * Put your tracking code here
     * You can get the order total etc e.g. $order->get_total();
     */
    
    ?>
    <script>
  fbq('track', 'Purchase');
        
  gtag('event', 'conversion', {
      'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
      'value': 0.0,
      'currency': 'USD',
      'transaction_id': ''
  });
</script>

<?php
    // This is the 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 );
    }
}

【问题讨论】:

    标签: javascript wordpress woocommerce


    【解决方案1】:

    您不需要所有代码,只需要计算订单总额的一行,您可以将其直接放在脚本中。尝试:

    add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
    
    function my_custom_tracking( $order_id ) {
        $order = wc_get_order( $order_id );    
        ?>
        <script>
            fbq('track', 'Purchase');   
            gtag('event', 'conversion', {
                'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
                'value': <?php echo $order->get_total(); ?>,
                'currency': 'USD',
                'transaction_id': ''
            });
        </script>
        <?php
    }
    

    【讨论】:

    • 你不应该改用 wc_enqueue_js 吗?
    • 这是一个选项,但不是必需的。这个 sn-p 工作正常 :)
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 2022-01-19
    • 2017-06-12
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2023-03-04
    • 2014-12-25
    相关资源
    最近更新 更多