Adding Google Ads Event Snippet To Conversion Page (thank you.php)
I have some google ads running on a Wordpress site running WooCommerce and I've added the global site tag to the header.php of my child theme which also has a google analytics tag, so the tag just looks like this in the header
<!-- global site tag (gtag.js) - google analytics --> <script async src="https://www.googletagmanager.com/gtag/YADDA YADDDA></script> <script> window.datalayer = window.dataLayer || []; ... ... gtag('config', 'ANALYTICS TAG ID'); gtag('config', 'AD TAG ID'); </script>
This all works good, but now I have to add the event snippet on the conversion page which for WooCommerce is the thankyou.php in the WooCommerce/templates/checkout/ folder I believe. Google specifies this to be placed in the header of that specific page. Should I be adding a function to my functions.php - of my child theme - that hooks into thankyou page and the header? or directly place it into the thankyou.php hook file - of child theme - to overwrite it? Then it is also asking to add code to dynamically pass a value to the transaction_id parameter and value and currency parameters. Event snippet below. I'm confused on how to do this and my last few attempts have ended with a site error.
<!-- Event snippet for DB_Purchase conversion page --> <script> gtag('event', 'conversion', { 'send_to': 'AW-5555555/5555555', 'value': 45.0, 'currency': 'USD', 'transaction_id': ''}); </script>
placeholder tag used.
Add this to your child theme function.php at the end, and insert the proper tag. ``
<?php function ds_checkout_analytics($order_id){ $order = new WC_Order( $order_id ); $currency = $order->get_order_currency(); $total = $order->get_total(); $date = $order->order_date; ?> <!-- Paste Tracking Code Under Here --> <!-- End Tracking Code --> <?php } add_action( 'woocommerce_thankyou', 'ds_checkout_analytics' );``