Integrate PostHog with WordPress and WooCommerce for detailed user behavior tracking, product analytics, experimentation, and more.
This (unofficial) PostHog plugin offers visitor and user tracking for WordPress and WooCommerce websites, leveraging PostHog’s powerful analytics platform.
Features
- Easy Setup: Configure with just your PostHog project ID
- WooCommerce Integration: Track ecommerce events including:
- Product views
- Add-to-cart actions
- Cart updates
- Checkout process
- Completed orders
- User Tracking: Capture and identify logged-in user data
- Link Click Tracking: Monitor clicks on links (affiliate links, etc.)
- PostHog Experiment Support (experimental): Run A/B tests and feature flag experiments using Gutenberg blocks
- Custom Initialization: Advanced options for custom PostHog configurations
Why Use PostHog?
PostHog is an all-in-one platform that helps you build better products. It includes all the tools you need to deploy changes, observe their effect, gather feedback, and iterate from there — and it’s completely free to get started.
Some of the features PostHog offers include…
- Product and web analytics
- Revenue tracking and surveys
- Session recording and heatmaps
- LLM observability and AI assistant
- Feature flags and experimentation
- Error tracking and issue assignment
- Data pipelines and integrations
You can create a free account with PostHog at posthog.com
WooCommerce Features
With WooCommerce enabled, the plugin automatically tracks:
- Product view events
- Add to cart actions
- Cart views and updates
- Checkout progress and completion
- Purchase events with detailed product information
Gutenberg Experiment Support
Create and manage PostHog experiments directly in the Gutenberg editor. Test different content variations using PostHog feature flags without any coding.
Link Click Tracking
Monitor link clicks with customizable link prefixes, supporting both direct links and encoded/obfuscated links.
Configuration
Basic Configuration
- Project ID: Your PostHog project ID (required)
- API Host: Select US or EU PostHog instance, or enter a custom URL
- Tracking Mode: Choose between tracking all visitors or only identified users
Advanced Options
- Debug Mode: Enable console logging for troubleshooting
- Gutenberg Experiments: Enable PostHog feature flag experiments in the Gutenberg editor
- Link Click Tracking: Track clicks on links with customizable prefixes
- Custom Mode: Override default initialization with custom PostHog script
FAQ
Where do I find my PostHog Project ID?
Log in to your PostHog account, navigate to Project Settings, and copy the Project API Key (starts with ‘phc_’).
Does this plugin work without WooCommerce?
Yes, the plugin works with standard WordPress sites for basic analytics. WooCommerce-specific tracking is only activated when WooCommerce is installed and active.
Can I use this with other PostHog instances besides the official ones?
Yes, you can use a custom API host URL for reverse proxy PostHog instances.
How do I set up an experiment?
- Enable Gutenberg experiments in the plugin settings
- Edit a post or page with the Gutenberg editor
- Select any block and use the PostHog Experiment panel in the block settings sidebar
- Enter your feature flag name and variant
- Save your post/page
Which PostHog snippet does the plugin use?
The plugin ships PostHog’s current HTML loader snippet, which loads the latest posthog-js v1 from PostHog’s assets CDN (or from your custom instance or reverse proxy). Nothing is bundled.
How do I use PostHog without cookies?
Set Persistence to localStorage to avoid PostHog cookies, or use Cookieless Mode always to store nothing in the browser. Cookieless mode must also be enabled in your PostHog project settings.
How do I connect a consent banner?
Enable Require Consent (or set Cookieless Mode to on_reject) and have your consent tool call posthog.opt_in_capturing() when the visitor accepts and posthog.opt_out_capturing() when they decline. Do not combine Require Consent with Cookieless Mode on_reject.
Can I change the distinct ID used for logged-in users?
Yes. The infnet_posthog_distinct_id filter receives the WordPress user ID and the user object. Use a site-specific value when several sites share one PostHog project.
Which WooCommerce events are sent, and when?
Names in brackets are used with the PostHog ecommerce spec setting.
view_product (Product Viewed): a product page is shown
view_product_list (Product List Viewed): a shop, category or tag archive is shown. Lists the products on that page
add_to_cart (Product Added): once per add, with the added quantity. Covers AJAX buttons, product forms, add to cart links and the Product Collection, Add to Cart and Mini-Cart blocks
remove_from_cart (Product Removed): once per removed cart line
cart_updated: once per cart change, with the new cart totals and cart_action (add, remove, quantity or mixed). A quantity change also has product_id, quantity and previous_quantity
cart_view (Cart Viewed): once per view of the cart page
begin_checkout (Checkout Started): once per cart when the checkout page is shown. Reloading the page does not repeat it
order_completed (Order Completed): once per order, when the shopper sees the order received page
checkout_progress, checkout_field_interaction, payment_method_selected: classic checkout form only
Which orders send order_completed?
Orders placed through the classic checkout, the Checkout block, express payment buttons or the order pay page, at the moment the shopper reaches the order received page. Each order is sent once, also when that page is reloaded or opened again.
Not sent: failed, cancelled and refunded orders (a failed payment is sent when the retry succeeds), orders created in wp-admin, subscription renewals, order received links older than 7 days, and orders whose shopper never returns to the order received page (for example when the tab is closed on the payment provider’s site).
The infnet_posthog_track_order_completed filter (arguments: the decision, the order) changes which orders are sent. infnet_posthog_purchase_max_age changes the 7 days.
What is the infnet_posthog_ev_ cookie?
Cart changes happen in background requests that do not load a page. After such a request the plugin sets a first-party cookie named infnet_posthog_ev_ plus a random token. The page then fetches the cart events once and sends them to PostHog. The cookie holds no personal data, is removed as soon as the events are fetched (normally within a second) and expires after 30 minutes. It is only set when a PostHog Project ID is configured and the cart changed. The events are only sent if the visitor has not opted out of PostHog.
Is this plugin GDPR compliant?
This plugin integrates with PostHog, which has GDPR compliance features. The plugin itself is designed to work with PostHog’s privacy controls. However, you are responsible for ensuring your specific implementation meets GDPR requirements, including proper consent management.
ChangeLog
1.6.1
- WooCommerce events now work on block themes and with the Cart and Checkout blocks: add to cart from Product Collection, Add to Cart and Mini-Cart blocks, quantity changes and removals in the Cart block, checkout started on the Checkout block
- Every WooCommerce event now has one source (the server) and is sent once per action. Fixes: add to cart was sent twice for add to cart links without AJAX, cart view twice on the classic cart page, and add to cart was lost when the add was followed by a redirect
- New remove_from_cart event (Product Removed with the ecommerce spec names). Undoing a removal sends add_to_cart
- cart_updated is sent once per cart change (add, remove, quantity) with the new cart totals and a cart_action property. Before, it was sent on every page load and with outdated totals after a change. Applying or removing a coupon alone no longer sends it
- A quantity change sends cart_updated with cart_action “quantity”, product_id, quantity and previous_quantity
- Fix: order_completed is sent once per order. Reloading the order received page, opening it in another tab or going back no longer sends it again
- order_completed is no longer sent for failed, cancelled or refunded orders (a failed payment is sent when the retry succeeds), for orders created in wp-admin, for subscription renewals, or for order received links older than 7 days. New filters: infnet_posthog_track_order_completed, infnet_posthog_purchase_max_age
- begin_checkout is sent once per cart. Reloading the checkout page or opening it in a second tab does not repeat it
- Performance: shop and category pages no longer load every product of the store for view_product_list. The event lists the products shown on the page
- Fix: checkout_progress step 1 was also sent on the order received page, and payment_method_selected had no method
- Cart events are delivered with a short-lived first-party cookie (infnet_posthog_ev_…) that is set after a cart change, see the FAQ
- Declares compatibility with WooCommerce High-Performance Order Storage and the Cart and Checkout blocks
- Removed the purchase and server-side checkout_progress hooks, which never sent an event
- Fix: an order with a deleted product no longer causes an error on the order received page
1.6.0
- Update the PostHog loader snippet to the version in the current PostHog docs (loads posthog-js from the PostHog assets CDN, guards against loading twice)
- Fix: “Disable auto-capture” now works. posthog-js has no disable_autocapture option; the plugin now sends autocapture: false
- “Disable cookie” is sent as disable_persistence, its current posthog-js name. The behavior is unchanged; the label now says what it does
- Fix: in “Always” tracking mode, anonymous visitors were reset to a new PostHog ID on every page. PostHog is now reset once after a WordPress logout instead
- Fix: on classic (non-block) themes, product view, product list, cart view, checkout started and order completed events were captured too late and never sent
- Fix: WooCommerce events no longer overwrite the PostHog $browser property with the raw user agent
- New PostHog SDK options, all off by default: configuration defaults snapshot, persistence, cookieless mode, opt out by default, ui_host, exception autocapture, heatmaps, disable surveys, mask all text in session recordings
- New “Never” tracking mode (person_profiles: never); tracking mode values are validated on save
- New optional WooCommerce event names from the PostHog ecommerce spec, with spec properties. Legacy names stay the default
- New infnet_posthog_distinct_id filter
- Removed the checkout_initiated hook, which ran after its data was printed and never sent an event
- Fix: clicking an AJAX add to cart button on shop pages also followed the link, adding the product twice and sending add_to_cart twice
- Fix: the feature flag ready script for experiments ran before PostHog was initialized (“posthog is not defined”)
1.5.5
- Fix: Link Prefixes saved with blank lines or Windows line endings no longer track every link click or stop link tracking; the list is cleaned on save and when loaded
- Link tracking script ignores empty prefixes
1.5.4
- Tested with WordPress 7.2 and PHP 8.5; checked for PHP 8.6 deprecations
- Use wp.editor.PluginSidebar for the experiment sidebar (wp.editPost.PluginSidebar is deprecated) and declare all editor script dependencies
- Opt the experiment sidebar controls into the current @wordpress/components sizing and margin styles
- Do not load the experiment editor script in the widgets and Customizer editors (avoids the core wp-editor notice)
- Replace deprecated jQuery shorthand calls in the admin and WooCommerce scripts
- Keep line breaks when saving link prefixes, so each prefix on its own line is tracked
- Add a plugin version constant
1.5.3
- Fix custom block attribute registration
1.5.2
- Refactor custom initialization settings
- Add boolean value conversion helper
1.5.1
- Use form for script customization to avoid arbitrary script injection
1.5.0
- Add nonce verification for AJAX
- Proper escaping
- json_encode to wp_json_encode
- Rename classes, functions, prefixes
- Refactor all scripts as dedicated .js files and enqueue them
1.4.11
- Set default tracking mode to identified_only
- Properly debug-gated all console logging
1.4.10
- Fixed JavaScript syntax error in WooCommerce integration
1.4.9
- Fixed jQuery selector in toggleSettings
1.4.8
- Improved compatibility with WooCommerce
- Fixed issue with direct add-to-cart link tracking
- Enhanced checkout process tracking
1.4.7
- Added support for custom PostHog instance
- Improved WooCommerce checkout tracking
- Fixed JavaScript errors
1.4.6
- Added link click tracking features
- Improved PostHog feature flag experiment functionality
- Fixed compatibility issues with WordPress 6.7
1.4.5
- Added custom initialization options
- Improved error handling and debugging
- Fixed issue with duplicate events
1.4.0
- Added support for Gutenberg experiments
- Enhanced WooCommerce integration
- Improved user identification
1.3.0
- Added WooCommerce checkout tracking
- Improved cart event handling
- Fixed issues with event batching
1.2.0
- Added product view tracking
- Enhanced add-to-cart event data
- Fixed compatibility issues with other plugins
1.1.0
- Added basic WooCommerce support
- Improved user tracking
1.0.0
- Initial release with core PostHog integration