1

I have an adwords conversion code which I want to add in my child theme.I want to insert the Total purchase Amount in the "value" attribute in this piece of code so that each time the code is triggered the total amount in cart is added to the conversion.

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

            gtag('config', 'AW-806400000"');
            gtag('event', 'conversion', {
                  'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
                  'value': 1.0, **[Get the Total from cart and use here]**
                  'currency': 'USD',
                  'transaction_id': ''
              });

        </script>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tanvir
  • 1,642
  • 8
  • 32
  • 53
  • How about using one of the many plugins that will do that for you? (like ours https://wordpress.org/plugins/woocommerce-google-adwords-conversion-tracking-tag/) – Aleksandar Jun 06 '18 at 04:34
  • @alev yup I used that also, I just thought adding another plugin will cause an overhead. – Tanvir Jun 07 '18 at 12:39

1 Answers1

4

Update

As Reigel suggested, it should be more appropriated in "Order received" end point (thankyou page). Here we target the Order total instead (as cart object doesn't exist anymore).

So the code should be:

add_action('wp_head','google_tag_manager_checkout_conversion_script' );
function google_tag_manager_checkout_conversion_script() {
    // Only on "Order received" page
    if( ! is_wc_endpoint_url('order-received') ) 
        return; // Exit

    global $wp;

    $order_id  = absint( $wp->query_vars['order-received'] );
    $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';

    if ( empty($order_id) || $order_id == 0 )
        return; // Exit

    $order = wc_get_order( $order_id );

    if ( $order->get_order_key() != $order_key )
        return; // Exit

    // Get Order total amount and Order transaction ID
    $order_total    = (float) $order->get_total();
    $transaction_id = $order->get_transaction_id();

    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'AW-806400000"');
        gtag('event', 'conversion', {
              'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
              'value': <?php echo $order_total; ?>,
              'currency': 'USD',
              'transaction_id': '<?php echo $transaction_id; ?>'
          });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

This should work better as you get the transaction ID this time.


Original answer to the original question that how to get to cart total for this Adwords script…

To display the total cart amount you will use:

<?php echo number_format( WC()->cart->total + WC()->cart->total_tax, 2 ); ?>

Targeting checkout page, you can try the following hooked function, that will add your script in the <head> section with the correct total cart amount:

add_action('wp_head','google_tag_manager_order_received_conversion_script' );
function google_tag_manager_order_received_conversion_script() {
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'AW-806400000"');
        gtag('event', 'conversion', {
              'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
              'value': <?php echo number_format( WC()->cart->total + WC()->cart->total_tax, 2 ); ?>,
              'currency': 'USD',
              'transaction_id': ''
          });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

But it seems strange as there is not yet any transaction ID to set in it…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I think, this code is best to put in the "Thank You" page. After success checkout. – Reigel Gallarde Jun 06 '18 at 01:13
  • @Reigel Yes I know, but the OP asked for cart total… In Thankyou there is no more cart object but order object instead… – LoicTheAztec Jun 06 '18 at 01:27
  • Yeap, but I guess the OP is just confused with the term/word "cart". Oh well, the OP has to clear this up. – Reigel Gallarde Jun 06 '18 at 01:34
  • 1
    @Reigel Finally I have added the "order-received" option… I will see what the OP say… It's up to him. – LoicTheAztec Jun 06 '18 at 01:58
  • TONS OF THANKS, Sorry for late reply.I agree that since its conversion script, its best to use in "Order received" end point. I was also going to say the same bcoz my script is added on every page right now.l ll change the question Title to Order Total. – Tanvir Jun 07 '18 at 12:18