Home > Enterprise >  How to add Google Ads conversion purchase event to my WooCommerce Thank you Page
How to add Google Ads conversion purchase event to my WooCommerce Thank you Page

Time:03-21

I saw this 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.

This is what my functions.php file looks like. 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?

CodePudding user response:

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(). Default priority is = 10

CodePudding user response:

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.

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' );
        }
    }
}
  • Related