2

I am trying to install the Google Adwords Conversion tag into Wordpress; specifically Woocommerce. We have tried plugins, but had conflicts with other items so we are now trying to manually implement it.

We have the following 'event' tag placed in our functions.php file:

add_action( 'woocommerce_thankyou',    'conversion_tracking_thank_you_page' );

function conversion_tracking_thank_you_page() {
?>
<!-- Event snippet for Transaction conversion page -->
<script>
 gtag('event', 'conversion', {
  'send_to': 'AW-######/######',
  'value': 1.0,
  'currency': 'USD',
  'transaction_id': '
 });
</script>
<?php
}

It pulls correctly, but, all transactions default to $1. We have this set to revert to $1 if there is no transaction value, however we want to pull the transaction value from Woocommerce. I have searched everywhere, and cannnot find the correct code for the event tag specifically, to pull this value from Woocommerce.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
IVCatalina
  • 348
  • 7
  • 23

1 Answers1

3

Updated

Try the following (where you will have to set the correct 'send_to' replacing 'AW-######/######'):

add_action( 'woocommerce_thankyou', 'conversion_tracking_thank_you_page', 95, 1 );
function conversion_tracking_thank_you_page( $order_id ) {
    if( ! $order_id )
        return;

    // Get the WC_Order instance Object
    $order = wc_get_order( $order_id ); 
    ?>
    <!-- Google Tag: Transaction conversion event -->
    <script>
    gtag('event', 'conversion', {
      'send_to'       : 'AW-######/######',
      'value'         : <?php echo $order->get_total(); ?>,
      'currency'      : '<?php echo $order->get_currency(); ?>',
      'transaction_id': <?php echo $order->get_transaction_id() ? $order->get_transaction_id() : $order_id; ?>
    });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested output… It should works this time.


Similar answers:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! I am testing this to see if it works. Weirdly though, the 'thank you' page misses the information that shows their order number, etc. Any way we can still have this show and let this code do its thing? – IVCatalina Sep 19 '18 at 21:01
  • @IVCatalina There was a missing `'` in the code… try it again.… Now I can't guaranty anything else as it depends of other integrated related JS… Here I am just filling your given question code with the necessary current Order data… You can check that in the generated page source code, so answering just your question. – LoicTheAztec Sep 19 '18 at 21:44
  • No problem; I appreciate that! Am not sure where the ' was missing, but I copied your code again and thank you page is still missing the order number. It shows without this code, but this code seems to remove this information. – IVCatalina Sep 19 '18 at 21:55
  • If it helps, my debug gives me the error: SyntaxError: '' string literal contains an unescaped line break for transaction_id line; which I think is the one you altered to add the '. It doesn't seem to like it though... – IVCatalina Sep 19 '18 at 22:00
  • @IVCatalina Oh yes sorry, I have make another mistake and updated the code… It should work now as expected. – LoicTheAztec Sep 19 '18 at 23:02
  • thank you!! Am testing and will mark your answer as correct, hopefully, shortly. – IVCatalina Sep 20 '18 at 01:21