--- title: WooCommerce GA4 purchase tracking without MonsterInsights description: MonsterInsights is heavy and expensive. This small plugin fires GA4 purchase events from WooCommerce order completions, and nothing else. source: https://steem.dev/blog/woocommerce-ga4-purchase-tracking-plugin published: 2026-09-13 updated: 2026-10-31 alternative_to: MonsterInsights site: Steem — AI WordPress plugin generator --- # WooCommerce GA4 purchase tracking without MonsterInsights MonsterInsights is the default answer to Google Analytics on WooCommerce. It works, but it is a heavy plugin with a database footprint, a paid tier behind most of the useful settings, and far more configuration than most stores need. The job at its core is narrow: load the GA4 snippet and tell Google when an order completes. GA4 ecommerce tracking needs a particular event shape. A generic snippet or a copy-pasted gtag tag will not send the purchase event with order data. MonsterInsights hooks the WooCommerce thank-you page and sends the right fields, and a few dozen lines of PHP can do the same. This plugin adds one field to WooCommerce settings for your GA4 Measurement ID, loads gtag.js on every page, and fires a purchase event on the thank-you page with order value, currency, tax, shipping and a line-item array. Nothing else. ## What one build covers - A settings field in WooCommerce > Settings > General for your GA4 Measurement ID - gtag.js injected on every page using the configured Measurement ID - A purchase event on the WooCommerce thank-you page with transaction_id, value, currency, tax, shipping, and a per-line-item array - Line items include product name, SKU or product ID, unit price, and quantity - No admin menu, no dashboard widget, no stylesheet ## What it does not cover - Upper funnel events: view_item, add_to_cart, and begin_checkout require a follow-up prompt - Server-side tracking via the Measurement Protocol; browser ad blockers can still intercept the event - GA4 reports embedded inside the WordPress admin dashboard - Google Tag Manager integration; GTM handles its own snippet injection separately - Automatic deduplication if a caching plugin serves a static thank-you page that bypasses the PHP hook ## The prompt ```text Build a WordPress plugin that adds lightweight GA4 purchase tracking to WooCommerce. Plugin Name: WC GA4 Purchase Tracking. Version: 1.0.0. Description: Fires a GA4 purchase event on WooCommerce order completion. Wrap all code in a function attached to plugins_loaded. Add a settings field using the woocommerce_general_settings filter: prepend an array element with type 'text', id 'wc_ga4_measurement_id', title 'GA4 Measurement ID', desc 'Your GA4 Measurement ID (G-XXXXXXXXXX)', default '', desc_tip true. On wp_head at priority 1, read get_option('wc_ga4_measurement_id') into $mid; if not empty, esc_attr it and echo: a script tag with async src https://www.googletagmanager.com/gtag/js?id={$mid}, then an inline script that declares window.dataLayer=window.dataLayer||[], a function gtag(){dataLayer.push(arguments);}, then calls gtag('js',new Date()) and gtag('config','{$mid}'). On the woocommerce_thankyou action, which receives $order_id, get the order with wc_get_order($order_id); if the order exists and get_option('wc_ga4_measurement_id') is not empty, build a PHP array $items by iterating $order->get_items(): for each WC_Order_Item_Product, get the product with $item->get_product(); push an associative array with keys item_name (esc_js($item->get_name())), item_id (esc_js($product && $product->get_sku() ? $product->get_sku() : (string)$item->get_product_id())), price ($item->get_quantity()>0 ? round((float)$item->get_subtotal()/$item->get_quantity(),2) : 0.0), quantity (intval($item->get_quantity())). Then echo an inline script that pushes to window.dataLayer: an object with event 'purchase' and ecommerce containing transaction_id (esc_js($order->get_order_number())), value (floatval($order->get_total())), currency (esc_attr($order->get_currency())), tax (floatval($order->get_total_tax())), shipping (floatval($order->get_shipping_total())), items (wp_json_encode($items) output raw inside the script). Use proper escaping throughout. No admin menu, no options page beyond the WooCommerce settings field, no stylesheet. ``` ## One event, built from the order object The only event is `purchase`, on the order confirmation page. GA4 receives the transaction ID, order total, currency, tax, shipping and an items array with one entry per line: product name, SKU (or the product ID when there is no SKU), unit price and quantity. The figures come from the WooCommerce order object, so they match your database exactly. The thank-you page normally shows once per order, so the event fires once; a customer who refreshes it fires it again, which is a limit of client-side tracking generally rather than of this plugin. The Measurement ID field sits in WooCommerce > Settings > General. Paste your G-XXXXXXXXXX there and save. There is no options page, admin menu or dashboard widget. ## No funnel, no server side, no reports in WordPress `view_item`, `add_to_cart` and `begin_checkout` are not sent, so GA4's purchase funnel report shows only the final step. A follow-up message can add `add_to_cart` on the `woocommerce_add_to_cart` action, pushing the product name, SKU, price and quantity to the dataLayer. Nothing goes through the Measurement Protocol. The event fires from gtag.js in the browser, so ad blockers that stop Google's scripts drop it for those visitors. Most stores can live with that; if measurement accuracy is critical, server-side tracking is a separate build. There are no GA4 reports in the WordPress dashboard. You read your data in GA4 itself. ## When to keep MonsterInsights Keep it for a client who needs GA4 reports inside the WordPress admin without logging in to Google Analytics. That embedded reporting is its strongest feature and a tracking plugin does not replicate it. Keep it if you already pay for MonsterInsights Pro for another reason: WooCommerce tracking is then a toggle on a cost already spent. And keep it if a non-developer needs to switch on add-to-cart or checkout funnel tracking from checkboxes; that configuration experience is worth paying for. This plugin is for stores where the subscription is the only reason MonsterInsights is installed. To confirm the swap works, place a test order and open Reports > Realtime in GA4. The purchase should appear within a few seconds. ## Questions ### Do I need to set up a GA4 property first? Yes. Create the property in Google Analytics, go to Admin > Data Streams, choose your web stream and copy the Measurement ID, which starts with G-. Paste it into the plugin's settings field. ### Why is the purchase not showing in GA4 Realtime? Usually one of three things: a typo in the Measurement ID, an ad blocker or privacy extension stopping gtag.js, or a caching plugin serving a static copy of the thank-you page that never runs the PHP hook. Test in an incognito window on an uncached URL. ### Can I use this alongside Google Tag Manager? Not directly. GTM handles its own gtag injection, and two gtag configurations produce duplicate events. If you use GTM, set up the purchase event as a dataLayer push inside GTM instead. ### Does it work with High-Performance Order Storage? Yes. `woocommerce_thankyou` passes a plain order ID and the plugin loads the order with `wc_get_order`, which supports both the legacy post-based storage and HPOS. --- Source: https://steem.dev/blog/woocommerce-ga4-purchase-tracking-plugin Build this plugin: https://steem.dev/app