Site logo

Archived topic

Tracking Google Ads Events on a Category and Thank You Page

42 replies · Started by Georgi on December 1, 2022

Viewing posts 16–30 of 43

Glad to be of help

Okay, I found a moment to try it.

I added the PHP snippet you wrote to me to functions.php

Is that correct?

Thanks!

You're welcome !

The scripts are tracking purchases but do you know why I'm not getting the dynamic values of the orders, instead I get the same value for every conversion?

Sorry to bother again, did I put my script correctly in the code?

/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {

	// Lets grab the order
	$order = wc_get_order( $order_id );

	<!-- Event snippet for Покупка от сайта conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-exampleID',
      'value': 1.0,
      'currency': 'BGN',
      'transaction_id': ''
  });
</script>
	 
	// This is the order total
	$order->get_total();
 
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
  		// This will be a product
  		$product = $order->get_product_from_item( $item );
  
  		// This is the products SKU
		$sku = $product->get_sku();
		
		// This is the qty purchased
		$qty = $item['qty'];
		
		// Line item total cost including taxes and rounded
		$total = $order->get_line_total( $item, true, true );
		
		// Line item subtotal (before discounts)
		$subtotal = $order->get_line_subtotal( $item, true, true );
	}
}

No, you need to separate the function in to two parts, a. getting the values and b. printing out the script
For example:


/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {
    
    // Get the order
    $order = wc_get_order( $order_id );
    // get the order total and stotre it in $order_total
    $order_total = $order->get_total(); 

    // Close the PHP so we can print some HTML in the hook
    // inisde that we will echo the $order_total
    ?>
    <!-- Event snippet for Покупка от сайта conversion page -->
    <script>
    gtag('event', 'conversion', {
        'send_to': 'AW-exampleID',
        'value': <?php echo esc_attr( $order_total ); ?>,
        'currency': 'BGN',
        'transaction_id': ''
    });
    </script>
    <!-- End of Event snippet for Покупка от сайта conversion page -->
    <?php
}

Thanks! The way you wrote it should work?

I believe so - i haven't used that hook before for that purpose but its the way i would think to do it :)

Okay, I will try it and report to you so you can know :)

Ok :)

Just a quick question since I'm still not 100% aware of the Hook abilities.

I've this code in my functions.php that sets minimum order amount for checkout

//Set a minimum order amount for checkout
add_action( 'woocommerce_checkout_process', 'bbloomer_wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'bbloomer_wc_minimum_order_amount' );
  
function bbloomer_wc_minimum_order_amount() {
     
    $minimum = 10; // change this to your minimum order amount
 
    if ( WC()->cart->subtotal < $minimum ) {
 
        if( is_cart() ) {
 
            wc_print_notice( 
                sprintf( 'Нужно е да добавите продукти за минимум %s за да извършите поръчката си. Сегашната стойност на продуктите Ви е %s' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error'
            );
 
        } else {
 
            wc_add_notice( 
                sprintf( 'Нужно е да добавите продукти за минимум %s за да извършите поръчката си. Сегашната стойност на продуктите Ви е %s' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error'
            );
 
        }
    }
 
}

In order for it to work in a hook, I need to set a rule for the display to be on the checout page and also select "Execute PHP"?

This code should be added to functions.php, it should not be added to a hook element.

This archived topic is closed to new replies.