简体   繁体   English

如何将 Google Ads 转化购买事件添加到我的 WooCommerce 感谢页面

[英]How to add Google Ads conversion purchase event to my WooCommerce Thank you Page

I saw this Adding Google Ads Event Snippet To Conversion Page (thank you.php)我看到了这个Adding Google Ads Event Snippet To Conversion Page (thank you.php)

But I have a different situation.但我有不同的情况。 I am using " Auto Complete Processing WooCommerce orders on Thank you Page" and "Redirect WooCommerce checkout page" all these are inside my functions.php file.我正在使用“感谢页面上的自动完成处理 WooCommerce 订单”和“重定向 WooCommerce 结帐页面”所有这些都在我的 functions.php 文件中。

This is what my functions.php file looks like.这就是我的 functions.php 文件的样子。 I hid my site with " ********* "我用“*********”隐藏了我的网站

//Auto Complete Processing WooCommerce orders on Thankyou Page

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    if ( $order->has_status('processing') ) {
        $order->update_status( 'completed' );
    }
   
}

// Redirect WooCommerce checkout page to ******************** after the payament
add_action( 'woocommerce_thankyou', 'pfwp_redirect_woo_checkout');
function pfwp_redirect_woo_checkout( $order_id ){
    $order = wc_get_order( $order_id );
    $url = 'https://*********/*********/*********/';
    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
    }
}

And I want to add the Event snippet inside the Thank You for google ads.我想在感谢谷歌广告中添加事件片段。

<!-- Event snippet for Purchase conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-***********/********kDENy8vL4o',
      'value': 1.0,
      'currency': 'SAR',
      'transaction_id': ''
  });
</script>
<!-- End Event snippet for Purchase conversion page -->


Because I am redirecting the thank you page to another page, the script will trigger?因为我将感谢页面重定向到另一个页面,脚本会触发吗? Or not?或不?

And how and where do I add the Event snippet at the functions.php because I have a lot of code that control WooCommerce?以及如何以及在何处添加事件代码段到 functions.php,因为我有很多控制 WooCommerce 的代码?

If you add your script with the other tutorial:如果您将脚本与其他教程一起添加:

add_action( 'woocommerce_thankyou', 'ds_checkout_analytics' );

And your redirection with:以及您的重定向:

add_action( 'woocommerce_thankyou', 'pfwp_redirect_woo_checkout');

Simply do this to make the tracking trigger before the redirection:只需执行此操作即可在重定向之前触发跟踪:

add_action( 'woocommerce_thankyou', 'pfwp_redirect_woo_checkout', 12 );
add_action( 'woocommerce_thankyou', 'ds_checkout_analytics', 11 );

By specifying the hook "priority", we've just told WordPress to run ds_checkout_analytics() before pfwp_redirect_woo_checkout().通过指定钩子“优先级”,我们刚刚告诉 WordPress 在 pfwp_redirect_woo_checkout() 之前运行 ds_checkout_analytics()。 Default priority is = 10默认优先级 = 10

Since you're redirecting to another page, you have to add the conversion code on that page.由于您要重定向到另一个页面,因此必须在该页面上添加转换代码。 To do that, I set a transient to pass that value.为此,我设置了一个瞬态来传递该值。

Your functions for thank_you can be combined. thank_you的功能可以组合。

This is tested and should work for you.这已经过测试,应该适合你。

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    if ( $order->has_status( 'processing' ) ) {
        $order->update_status( 'completed' );
    }
    $url = 'https://*******/'; // Your redirect URL.
    if ( ! $order->has_status( 'failed' ) ) {
        ob_start();
        set_transient( 'wc_order_total_for_google', $order->get_total( 'edit' ), 1 * MINUTE_IN_SECONDS );
        wp_safe_redirect( $url );
        exit;
    }
}

add_action( 'wp_print_footer_scripts', 'dd_add_conversion_code' );
function dd_add_conversion_code() {
    if ( is_page( your_custom_page_id ) ) { //Set your custom page ID here.
        if ( false !== get_transient( 'wc_order_total_for_google' ) ) {
            $order_total = get_transient( 'wc_order_total_for_google' );
            ?>
            <!-- Event snippet for Purchase conversion page -->
            <script>
                gtag('event', 'conversion', {
                    'send_to': 'AW-***********/********kDENy8vL4o',
                    'value': <?php echo esc_attr( $order_total ); ?>,
                    'currency': 'SAR',
                    'transaction_id': ''
                });
            </script>
            <!-- End Event snippet for Purchase conversion page -->
            <?php
            delete_transient( 'wc_order_total_for_google' );
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在提交按钮上使用ajax实现Google Adwords“转换像素”(没有“谢谢”页面) - How to implement a Google Adwords “conversion pixel” using ajax on a submit button (no “thank you” page) 如何在没有物理谢谢页面的情况下执行Google Adwords转换跟踪 - How to execute Google Adwords conversion tracking without physical thank you page 将Facebook像素购买事件代码添加到WooCommerce购买完成页面 - Add Facebook Pixel Purchase Event Code to WooCommerce purchase completion page 避免在Woocommerce谢谢页面中加载脚本 - Avoid loading a script in Woocommerce thank you page Google跟踪代码管理器表单-谢谢页面 - Google Tag Manager Form - Thank You Page 如何在点击时或没有单独的“谢谢”页面时实施 Facebook 转化跟踪 - How to implement Facebook conversion tracking onclick or when there's no separate 'Thank you' page Woocommerce获取订单感谢您的页面并传递数据javascript片段 - Woocommerce Get Orders on Thank you page and pass data javascript snippet 将 javascript 代码插入 woocommerce 感谢页面的头部标签中 - Insert javascript code into the head tags of the woocommerce thank you page 如何在没有单独的感谢页面的情况下设置联系表单的Google Analytics(分析)目标跟踪? - How to set up Google Analytics goal tracking of a contact form with no separate thank you page? 如何在js中提交表单后添加重定向到感谢页面 - How do I add a redirect to thank you page after form submission in js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM