PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

[FIXED] how to capture dynamic conversion values on Google ads?

 July 22, 2022     google-ads-script, google-tag-manager, woocommerce     No comments   

Issue

I am trying to get dynamic the order total price on my thankyou page(the page seeing the user after a successful order) to the google ads report through the goole-tag-manager chrome extension.

The google ads snippet I am using on my thankyou page is this:

*I have follow the google support before (https://support.google.com/google-ads/answer/6095947?hl=en)

<!-- Event snippet for Purchase conversion page -->
            <script>   
            gtag('event', 'conversion', {
                'send_to': 'My Conversion ID/My Conversion Label',
                'value': 11.50,
                'currency': 'EUR',
                'transaction_id': ''
            });

            </script>

and the order total amount is the " 'value': 11.50, " So instead of 11.50 I am placing the below code

document.querySelector("#cost>.woocommerce-Price-amount.amount").innerText.match(/^.{1}(.*).{0}/i)[1].trim();

so I can take the total for each order every time dynamicaly by scaning the dom, but doesn't work. Are my thoughts right? Is this the right way to do it by addin code manually on my site without use gootle tag manager? Any Ideas about how to do it?


Solution

Well its actually quite simple when you do it with php.

// Hook the function in head.
add_action('wp_head', 'bks_head_social_tracking_code');

function bks_head_social_tracking_code() {
    // Check if its thank you page.
    if(is_wc_endpoint_url( 'order-received' )) {
        // Get order object.
        $order = wc_get_order(get_query_var('order-received'));

        // Get total.
        $order_total = $order->get_total();

        // Prepare.
        $output = "
      <script>
          gtag('event', 'conversion', {
                'send_to': 'My Conversion ID/My Conversion Label',
                'value': ". $order_total .", // Use dynamically.
                'currency': 'EUR',
                'transaction_id': ''
            });

    </script>";
         // echo.
        echo $output;
    }
}

You can make the currency dynamic too. You can use this to to get different kinds of data. https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/



Answered By - bhanu
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing