# GA4 begin_checkout Event for WooCommerce

> Sends a GA4 "begin_checkout" event with the cart value and items when the customer opens the checkout page.

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

## Code

```php
add_action( 'wp_footer', function () {
    if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_wc_endpoint_url() || ! WC()->cart || WC()->cart->is_empty() ) {
        return;
    }

    $items = array();

    foreach ( WC()->cart->get_cart() as $line ) {
        $product = $line['data'];
        $items[] = array(
            'item_id'   => $product->get_sku() ? $product->get_sku() : (string) $product->get_id(),
            'item_name' => $product->get_name(),
            'price'     => round( (float) wc_get_price_to_display( $product ), 2 ),
            'quantity'  => (int) $line['quantity'],
        );
    }

    $data = array(
        'currency' => get_woocommerce_currency(),
        'value'    => round( (float) WC()->cart->get_cart_contents_total() + (float) WC()->cart->get_cart_contents_tax(), 2 ),
        'coupon'   => implode( ',', WC()->cart->get_applied_coupons() ),
        'items'    => $items,
    );

    echo "<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('event', 'begin_checkout', " . wp_json_encode( $data ) . ");</script>\n";
} );
```

## Notes

Needs the GA4 tag (gtag.js) on the page.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "GA4 begin_checkout Event 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/
