Site logo

[Resolved] Tracking Google Ads Events on a Category and Thank You Page

Home Forums Support [Resolved] Tracking Google Ads Events on a Category and Thank You Page

Home Forums Support Tracking Google Ads Events on a Category and Thank You Page

Viewing 15 posts - 16 through 30 (of 43 total)
  • Author
    Posts
  • #2450562
    David
    Staff
    Customer Support

    Glad to be of help

    #2457588
    Georgi

    Okay, I found a moment to try it.

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

    Is that correct?

    #2457963
    David
    Staff
    Customer Support

    In the functions.php in your Child Theme. Yes 🙂

    This doc explains more on adding PHP:

    https://docs.generatepress.com/article/adding-php/

    #2459435
    Georgi

    Thanks!

    #2459466
    David
    Staff
    Customer Support

    You’re welcome !

    #2464533
    Georgi

    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?

    #2464996
    David
    Staff
    Customer Support

    That unfortunately is not something i can assist with. But i found this woo doc regarding this which you may find of use”

    https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/

    #2465368
    Georgi

    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 );
    	}
    }
    #2466786
    David
    Staff
    Customer Support

    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
    }
    #2468108
    Georgi

    Thanks! The way you wrote it should work?

    #2468198
    David
    Staff
    Customer Support

    I believe so – i haven’t used that hook before for that purpose but its the way i would think to do it 🙂

    #2468499
    Georgi

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

    #2468535
    David
    Staff
    Customer Support

    Ok 🙂

    #2468606
    Georgi

    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”?

    #2468670
    Ying
    Staff
    Customer Support

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

Viewing 15 posts - 16 through 30 (of 43 total)
  • You must be logged in to reply to this topic.