• Hey Team, since last 2-3 weeks my events are not fired properly especially on thankyou page. this is affecting my campaigns and conversion tracking. please look into this on my site as this is severely affecting my business.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’ve got the same exact problem.
    Any other event seems ok, but the purchase event is not fired anymore. there isn’t any js error on the page, and the server isn’t loggin any problem.

    Thread Starter khuzemahoztech

    (@khuzemahoztech)

    function add_datalayer_script() {
    if (is_order_received_page()) {
    global $wp;

        // Get the order ID from the URL
        $order_id = apply_filters('woocommerce_thankyou_order_id', absint($wp->query_vars['order-received']));
        if (!$order_id) {
            return;
        }
    
        // Get the order object
        $order = wc_get_order($order_id);
        if (!$order) {
            return;
        }
    
        // Prepare order data
        $order_data = [
            'order_id' => $order->get_id(),
            'order_total' => $order->get_total(),
            'currency' => $order->get_currency(),
            'products' => []
        ];
    
        // Loop through order items
        foreach ($order->get_items() as $item) {
            $product = $item->get_product();
            if (!$product) {
                continue;
            }
    
            $order_data['products'][] = [
                'id' => $product->get_id(),
                'name' => $product->get_name(),
                'price' => $product->get_price(),
                'quantity' => $item->get_quantity()
            ];
        }
    
        // Output the data layer script
        ?>
        <script>
            window.dataLayer = window.dataLayer || [];
            window.dataLayer.push({
                'event': 'purchase',
                'order': <?php echo json_encode($order_data); ?>
            });
        </script>
        <?php
    }

    }
    add_action(‘wp_footer’, ‘add_datalayer_script’);

    I added this custom code and it started

    But we need the plugin to add this automatically

    Thanks, this seems to be a nice workaround, I post the entire code, I added a couple of console.log to debug it.

    Also, to view the order page, I’ve used this plugin: thank you page viewer

    function add_datalayer_script()
    {
    if (is_order_received_page()) {
    global $wp;

    // Get the order ID from the URL
    $order_id = apply_filters('woocommerce_thankyou_order_id', absint($wp->query_vars['order-received']));
    if (!$order_id) {
    return;
    }

    // Get the order object
    $order = wc_get_order($order_id);
    if (!$order) {
    return;
    }

    // Prepare order data
    $order_data = [
    'order_id' => $order->get_id(),
    'order_total' => $order->get_total(),
    'currency' => $order->get_currency(),
    'products' => []
    ];

    // Loop through order items
    foreach ($order->get_items() as $item) {
    $product = $item->get_product();
    if (!$product) {
    continue;
    }

    $order_data['products'][] = [
    'id' => $product->get_id(),
    'name' => $product->get_name(),
    'price' => $product->get_price(),
    'quantity' => $item->get_quantity()
    ];
    }

    // Output the data layer script
    ?>
    <script>
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
    'event': 'purchase',
    'order': <?php echo json_encode($order_data); ?>
    });
    // console.log("dataLayer pushed");
    // console.log(window.dataLayer);
    </script>
    <?php
    }else{
    ?>
    <script>
    //console.log("not a purchase page");
    </script>
    <?php
    }
    }
    add_action('wp_footer', 'add_datalayer_script');

    Hello,

    What would happen if the customer refresh the page ? Will the push be sent again and produce multiple conversion on GA4/GAds ?

    Thanks

    I’m using this code that seems to work but no guarantee :


    if ( ! function_exists( 'fc_gtm4wp_fix_gateway_conversions' ) ) {
    function fc_gtm4wp_fix_gateway_conversions(): void
    {
    global $wp;

    $order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars[ 'order-received' ] ) );
    if ( ! $order_id ) {
    return;
    }

    $order = wc_get_order( $order_id );

    if ( ! isset( $order ) ) {
    return;
    }

    $customer_ip = get_post_meta( $order_id, '_customer_ip_address', true );
    if ( $customer_ip !== $_SERVER[ 'REMOTE_ADDR' ] ) {
    $order->update_meta_data( '_ga_tracked', 0 );
    $order->save();
    }
    }

    add_action( 'wp_footer', 'fc_gtm4wp_fix_gateway_conversions', PHP_INT_MAX );
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.