【问题标题】:Global Site Tag not working with WordPress, WooCommerce全局站点标签不适用于 WordPress、WooCommerce
【发布时间】:2021-12-19 02:18:08
【问题描述】:

我很想知道下面的代码有什么问题。 我正在使用 WordPress + WooCommerce,当我上传此函数时,我的网站标签使我的网站崩溃。php -> 全局站点标签在头部,由 google 标签 chrome 扩展检测到 -> 转化跟踪代码在functions.php 中,它只需要在订单完成后在感谢页面中可见。

这是我的functions.php页面。

欢迎任何帮助,我刚刚开始使用 PHP。

朱利安,



<?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' );
/*-----------------------------------------------------------------------------------*/

这是一些 woocommerce 和 google 示例页面 https://support.google.com/searchads/answer/9133542?hl=fr https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/#

【问题讨论】:

    标签: php wordpress woocommerce type-conversion


    【解决方案1】:

    应该这样做(有些功能部分您不需要 + 您需要回显脚本,因为您在 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
    }
    

    【讨论】:

    • 非常感谢!事实上,在
    【解决方案2】:

    您的问题是您没有使用 echo 或任何输出脚本的方法。这导致了您的致命错误。

    此外,您的脚本应添加wc_enqueue_js,它可以干净地执行脚本。

    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': '{$order->get_total()}',
            'currency': {$order->get_currency()}',
            'transaction_id': '{$order_id}'
        });";
        wc_enqueue_js($script);    
        
    }
    

    您的函数的其余部分可以清理,但这不是您的问题的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      相关资源
      最近更新 更多