Search code examples
javascriptphpwordpresswoocommercegoogle-ads-api

Loading twice WooCommerce thankyou page would duplicate Conversion tracking?


I implemented a custom script in tankyou page in my woocommerce store to track google ads conversions. This is my implementation:

add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
    function pixel_analytics_conversion_track_script( $order_id ) {
        if ( $order_id > 0 ) {
            $order = wc_get_order( $order_id );
            if ( $order instanceof WC_Order ) {
                $order_id               = $order->get_id(); // order id
                $order_key              = $order->get_order_key(); // order key
                $order_total            = $order->get_total(); // order total
                $order_currency         = $order->get_currency(); // order currency
                $order_payment_method   = $order->get_payment_method(); // order payment method
                $order_shipping_country = $order->get_shipping_country(); // order shipping country
                $order_billing_country  = $order->get_billing_country(); // order billing country
                $order_status           = $order->get_status(); // order status
                ?>
                <script type="text/javascript">
                    jQuery(document).ready(function( $ ){
                        console.log('PURCHACE EVENT');
                        /* Track conversion on facebook Pixel */
                        fbq('track', 'Purchase',
                        {
                            value: <?php echo $order_total ?>,
                            currency: "<?php echo $order_currency ?>"
                        });
                    
                        /* Track conversion on Google Ads */
                        gtag('event', 'conversion', 
                        { 
                            'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC', 
                            'value': <?php echo $order_total ?>, 
                            'currency': "<?php echo $order_currency ?>", 
                            'transaction_id': "<?php echo $order_id ?>"
                        });
                    });
                </script>
                <?php
            }
        }
    }
}

The code works very well but some data is not precise and I think that probably the code above duplicates conversion if user goes to thank you page twice. We have an order confirmation email that has a link to the thankyou page of woocommerce.

As you can see Im sending the transaction_id parameter, so my question:

If the user loads twice or N times the thank you page the conversion will appear dupplicated in Google ads even if you send the transaction_id parameter?


Solution

  • Update: Added compatibility for HPOS

    You can use order custom metadata to avoid duplicated Conversion tracking as follows:

    add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 );
    if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {
        function pixel_analytics_conversion_track_script( $order_id ) {
            $order = wc_get_order( $order_id ); // Get order object
    
            // Avoid if pixel analytics conversion track script has been run before
            if ( is_a($order, 'WC_Order') && !$order->get_meta('_pixel_tracking') ) {
                $order_id               = $order->get_id(); // order id
                $order_key              = $order->get_order_key(); // order key
                $order_total            = $order->get_total(); // order total
                $order_currency         = $order->get_currency(); // order currency
                $order_payment_method   = $order->get_payment_method(); // order payment method
                $order_shipping_country = $order->get_shipping_country(); // order shipping country
                $order_billing_country  = $order->get_billing_country(); // order billing country
                $order_status           = $order->get_status(); // order status
                ?>
                <script type="text/javascript">
                    jQuery(document).ready(function( $ ){
                        console.log('PURCHACE EVENT');
                        /* Track conversion on facebook Pixel */
                        fbq('track', 'Purchase',
                        {
                            value: <?php echo $order_total ?>,
                            currency: "<?php echo $order_currency ?>"
                        });
                    
                        /* Track conversion on Google Ads */
                        gtag('event', 'conversion', 
                        { 
                            'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC', 
                            'value': <?php echo $order_total ?>, 
                            'currency': "<?php echo $order_currency ?>", 
                            'transaction_id': "<?php echo $order_id ?>"
                        });
                    });
                </script>
                <?php
                // Flag the order (with custom meta data) to avoid pixel analytics conversion track script run multiple times.
                $order->update_meta_data('_pixel_tracking', true); 
                $order->save(); 
            }
        
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.