javascript数组显示数组的名称而不是内容gtag,购买事件

问题描述

我正在尝试在 gtag purchase 页面Woocommerce Eshop 中创建 Thank you 事件代码

但是当我在浏览器中看到 source 中的代码时,它显示的是数组的名称而不是内容

我可以看到 google dashboard 中的购买情况,但没有跟踪这些项目。

我只能看到购买的总价。

这是我的代码

if( is_wc_endpoint_url( 'order-received' ) ){
        $order_id = absint( get_query_var('order-received') );
        $order = wc_get_order( $order_id );
        $order_items = $order->get_items();
        $items_data  = []; // Initializing
        foreach ( $order_items as $item_id => $item ) {
            $variation_id = $item->get_variation_id();
            $product_id   = $variation_id > 0 ? $variation_id : $item->get_product_id();

            //get the categories tree
            foreach( wp_get_post_terms( $product_id,'product_cat' ) as $term ){
                if( $term ){
                    $categories .= $term->name;
                    $categories .= '/';
                }
            }
            $categories = trim($categories,'/');
            $product = wc_get_product( $product_id );
            // Set specific data for each item in the array
            array_push($items_data,array(
                item_id          => $product->get_sku(),item_name        => $product->get_name(),item_category    => $categories,quantity   => $item->get_quantity(),price      => $item->get_subtotal(),));
            $categories = '';
        }
    ?>
    <script>
        var items_data = <?=json_encode($items_data)?>;
        var my_items = [];
        
        for(var i=0; i<items_data.length; i++){ 
            var item = items_data[i]; console.log(item);
            my_items.push({
                    item_id:item.item_id,item_name:item.item_name,item_category:item.item_category,quantity:item.quantity,price:item.price
            });         
        }       
    </script>

<!-- Event snippet for Website sale conversion page -->
        <script> gtag('event','conversion',{ 'send_to': 'AC-898s89ds/PNdsds78dsy_ds82B','value': '<?PHP echo $order->get_total(); ?>','currency': 'EUR','transaction_id': '<?PHP echo $order_id; ?>' }); </script> 
    <script>        
        console.log({items:my_items}); //this displays correctly the content of the array       
        
gtag('event','purchase',{ 'transaction_id': '<?PHP echo $order_id; ?>','affiliation': 'eartshop.gr','shipping': '<?PHP echo $order->get_shipping_total(); ?>','tax' : '<?PHP echo $order->get_total_tax();  ?>','items': my_items });
</script>

这就是我在 Thank you page 的源代码中看到的:

gtag('event',{ 'transaction_id': '10707','affiliation': 'eshopdomain.com','value': '152.00','shipping': '0.00','tax' : '29.03','items': my_items });

解决方法

您的功能可以优化。使用 woocommerce_payment_completewoocommerce_thankyou 挂钩确保每次订单成功时都会触发该事件。

付款需要 woocommerce_payment_complete 钩子,例如 作为 PayPal 或信用卡(条纹或其他)。客户并不总是 等待站点上的重定向,因此事件可能不会 仅用 woocommerce_thankyou 挂钩激活。

// adds the Google Analytics "purchase" event to the thankyou page
add_action( 'woocommerce_payment_complete','set_google_tag_transaction_conversion_event' );
add_action( 'woocommerce_thankyou','set_google_tag_transaction_conversion_event',99,1 );
function set_google_tag_transaction_conversion_event( $order_id ) {

    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );

?>

    <!-- Event snippet for Website sale conversion page -->
    <script> gtag('event','conversion',{
        'send_to': 'AC-898s89ds/PNdsds78dsy_ds82B','value': '<?php echo $order->get_total(); ?>','currency': 'EUR','transaction_id': '<?php echo $order_id; ?>'
        });
    </script> 
    <!-- Google Tag: Transaction conversion event -->
    <script>
    gtag('event','purchase',{
        "transaction_id": "<?php echo $order_id; ?>","affiliation": "eartshop.gr","value": <?php echo $order->get_total(); ?>,"currency": "EUR","tax": <?php echo $order->get_total_tax(); ?>,"shipping": <?php echo $order->get_shipping_total(); ?>,"items": [
        <?php foreach( $order->get_items() as $item_id => $item_product ) {

            $product = $item_product->get_product();

            $quantity = $item_product->get_quantity();
            $subtotal_line = $item_product->get_subtotal();
            $price = $subtotal_line / $quantity;
            $product_name = $product->get_name();
            $name = str_replace( ',','',$product_name );

            $categories = array();
            //get the categories tree
            foreach( wp_get_post_terms( $product->get_id(),'product_cat' ) as $term ){
                if ( $term ) {
                    $categories[] = $term->name;
                }
            }

            if ( ! empty($categories) ) {
                $product_categories = implode( '/',$categories );
            } else {
                $product_categories = '';
            }

            ?>
            {
                "id": "<?php echo $product->get_sku(); ?>","name": "<?php echo $name; ?>","category": "<?php echo $product_categories; ?>","quantity": <?php echo $quantity; ?>,"price": "<?php echo $price; ?>",},<?php
        } // end foreach order items
        ?>
      ]
    });

    </script>

<?php

}

代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。