# Google Ads Conversion Tracking for WooCommerce

> Loads the Google Ads tag and reports a purchase conversion with the order value and transaction ID on the WooCommerce order received page.

- Type: PHP
- Runs: Run everywhere
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Requires: WooCommerce
- Tags: google-ads, woocommerce, conversion, gtag
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-google-ads-woo-conversion/

## Code

```php
$wpide_gads = array(
    'conversion_id'    => 'AW-XXXXXXXXX',     // Your Google Ads tag ID.
    'conversion_label' => 'XXXXXXXXXXXXXXXX', // Conversion label of the purchase action.
);

add_action( 'wp_head', function () use ( $wpide_gads ) {
    if ( 'AW-XXXXXXXXX' === $wpide_gads['conversion_id'] ) {
        return;
    }

    printf(
        "<script async src=\"%s\"></script>\n<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', %s);</script>\n",
        esc_url( 'https://www.googletagmanager.com/gtag/js?id=' . rawurlencode( $wpide_gads['conversion_id'] ) ),
        wp_json_encode( $wpide_gads['conversion_id'] )
    );
}, 5 );

add_action( 'woocommerce_thankyou', function ( $order_id ) use ( $wpide_gads ) {
    $order = wc_get_order( $order_id );

    if ( 'AW-XXXXXXXXX' === $wpide_gads['conversion_id'] || ! $order || $order->get_meta( '_wpide_gads_sent' ) ) {
        return;
    }

    $data = array(
        'send_to'        => $wpide_gads['conversion_id'] . '/' . $wpide_gads['conversion_label'],
        'value'          => round( (float) $order->get_total(), 2 ),
        'currency'       => $order->get_currency(),
        'transaction_id' => (string) $order->get_order_number(),
    );

    echo "<script>if (typeof gtag === 'function') { gtag('event', 'conversion', " . wp_json_encode( $data ) . "); }</script>\n";

    $order->update_meta_data( '_wpide_gads_sent', time() );
    $order->save();
}, 20 );
```

## Notes

Set $conversion_id (AW-...) and $conversion_label from Google Ads > Goals > Conversions. Nothing is output while the placeholders are in place.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Google Ads Conversion Tracking for WooCommerce" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
