Ditch the Plugin: Track WooCommerce Sales in Google Analytics Like a Pro (Without the Extra Baggage!)
Want to know exactly how your WooCommerce store is performing? You’re likely already using Google Analytics to track website traffic, but connecting that data to your actual *sales* can feel like a puzzle. Luckily, you don’t need another plugin bloating your site. You can track WooCommerce sales in Google Analytics manually, and this guide will show you how, step-by-step.
Why go plugin-free?
- Less Bloat: Plugins add code. More code often equals slower website loading times, which hurts your user experience and SEO.
- More Control: You understand the code, meaning you can customize it to fit your specific needs. No more being limited by a plugin’s features.
- Cost-Effective: Why pay for a premium plugin when you can achieve the same (or better!) results with a little DIY?
- Product Views: How many times a specific product page is viewed.
- Add to Carts: How often users add products to their shopping cart.
- Checkout Steps: Where users drop off during the checkout process (e.g., shipping information, payment details).
- Purchases: The actual completed sales, including revenue, product information, and transaction ID.
Imagine this: You run a small online store selling handmade jewelry. You notice a spike in website traffic from Pinterest, but you’re not sure if that traffic is actually *buying* anything. By setting up proper WooCommerce sales tracking in Google Analytics, you can definitively see if those Pinterest visitors are converting into paying customers. You can then double down on your Pinterest marketing strategies!
Understanding the Basics: Explore this article on How To Center Woocommerce Shortcode On Page Enhanced Ecommerce
To track sales effectively, we’ll leverage Google Analytics’ Enhanced Ecommerce feature. This powerful tool lets you track:
Step-by-Step Guide to Manual WooCommerce Sales Tracking
We’ll be modifying your WooCommerce template files to inject the necessary data into Google Analytics. Always back up your theme before making any changes! A child theme is highly recommended so theme updates don’t overwrite your changes.
#### 1. Enable Enhanced Ecommerce in Google Analytics
First, you need to enable Enhanced Ecommerce in your Google Analytics account:
1. Go to your Google Analytics account.
2. Select the correct property.
3. Click on Admin.
4. Navigate to Ecommerce Settings under the “View” column.
5. Turn on Enable Ecommerce and Enable Enhanced Ecommerce Reporting.
6. Click Save.
#### 2. Add Code to Your `thankyou.php` Template
The `thankyou.php` template is displayed after a successful order is placed. This is where we’ll fire the “purchase” event to Google Analytics.
Locate the `thankyou.php` file: It’s typically located in `wp-content/themes/[your-theme]/woocommerce/checkout/thankyou.php`. If it’s not there, copy it from `wp-content/plugins/woocommerce/templates/checkout/thankyou.php` to your child theme.
Edit the `thankyou.php` file: Open the file in Discover insights on How To Connect Paypal Account To Woocommerce a text editor. We’ll add a PHP code snippet to output the necessary JavaScript for Google Analytics.
<?php
defined( ‘ABSPATH’ ) || exit;
do_action( ‘woocommerce_before_thankyou’, $order->get_id() );
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( $order ) :
do_action( ‘woocommerce_before_thankyou_message’, $order->get_id() );
?>
<?php
echo apply_filters( ‘woocommerce_thankyou_order_received_text’, esc_html( ‘Thank you. Your order has been received.’ ), null ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
-
get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
-
get_date_created() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
-
get_billing_email(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
-
get_formatted_order_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
-
get_payment_method_title() ); ?>
get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
get_payment_method_title() ) : ?>
get_id() ); ?>
<?php
// Add this code block just before the “endif” statement
?>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
‘ecommerce’: {
‘purchase’: {
‘actionField’: {
‘id’: ‘get_order_number(); ?>’, // Transaction ID.
‘affiliation’: ”, // Your store name.
‘revenue’: ‘get_total(); ?>’, // Total transaction value (incl. tax and shipping)
‘tax’:’get_total_tax(); ?>’,
‘shipping’: ‘get_shipping_total(); ?>’ // Total shipping.
},
‘products’: [
<?php
$items = $order->get_items();
$item_count = count( $items );
$i = 0;
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
echo ‘{‘;
echo ‘”id”: “‘ . $product->get_sku() . ‘”,’; // Product SKU
echo ‘”name”: “‘ . $product->get_name() . ‘”,’; // Product Name
echo ‘”category”: “‘ . strip_tags( wc_get_product_category_list( $product_id ) ) . ‘”,’; // Product Category
echo ‘”quantity”: ‘ . $item->get_quantity() . ‘,’; // Quantity
echo ‘”price”: ‘ . $product->get_price(); // Product Price
echo ‘}’;
if ( ++$i < $item_count ) {
echo ‘,’; // Add comma if not the last product
}
}
?>
]
}
}
});
<?php
else : ?>
get_id() ); ?>
Explanation of the Code:
- `window.dataLayer = window.dataLayer || [];`: This ensures that the `dataLayer` array exists, which is how Google Analytics receives data.
- `dataLayer.push({ ‘ecommerce’: { ‘purchase’: { … } } });`: This pushes a new object into the `dataLayer` array, formatted according to the Enhanced Ecommerce schema.
- `actionField`: Contains information about the overall transaction:
- `id`: The order number.
- `affiliation`: Your store’s name.
- `revenue`: The total revenue of the order.
- `tax`: The tax amount.
- `shipping`: The shipping cost.
- `products`: An array containing information about each product in the order:
- `id`: The product SKU (ideally).
- `name`: The product name.
- `category`: The product category.
- `quantity`: The quantity purchased.
- `price`: The product price.
Important Considerations:
- SKUs: Make sure your products have SKUs assigned in WooCommerce. SKUs are the best way to uniquely identify products. If you don’t use SKUs, you can replace `$product->get_sku()` with `$product->get_id()` (product ID), but SKUs are preferred.
- Currency: This code assumes your store’s currency is set up correctly in WooCommerce. Google Analytics relies on the currency settings in WooCommerce.
- Error Handling: Consider adding error handling to the code to prevent it from breaking if, for example, a product doesn’t have a category assigned.
#### 3. Set Up Google Tag Manager (Recommended)
While you *can* directly add the Google Analytics tracking code to your website’s header or footer, using Google Tag Manager (GTM) provides much more flexibility and control.
Why use GTM?
- Centralized Management: Manage all your tracking codes (Google Analytics, Facebook Pixel, etc.) in one place.
- Easier Updates: Make changes to your tracking setup without directly editing your website’s code.
- Version Control: GTM keeps track of changes, allowing you to revert to previous configurations if needed.
Steps to Set Up GTM:
1. Create a GTM Account: Go to [tagmanager.google.com](https://tagmanager.google.com) and create an account (if you don’t already have one).
2. Install the GTM Snippet: GTM will provide you with two snippets of code. Place the first snippet in the “ section of your website and the second snippet immediately after the opening “ tag. Many themes have options to add code to these locations. Alternatively, you can use a plugin like “Insert Headers and Footers” to easily add the snippets.
3. Create a Google Analytics Tag:
- In GTM, click Add a New Tag.
- Choose the Google Analytics: Universal Analytics tag type.
- Set the Track Type to Event. Even though it’s a purchase, we are using Enhanced Ecommerce so it’s still considered an event.
- Configure the Event Tracking Parameters:
- Category: `ecommerce`
- Action: `purchase`
- Enable Enable Enhanced Ecommerce Settings and set the Use Data Layer option to `true`.
- Select your Google Analytics Settings Variable (or create a new one if you haven’t already).
- Click on Triggering in your tag configuration.
- Create a new trigger with the following settings:
- Trigger Type: `Custom Event`
- Event name: `ecommerce`
- This trigger fires on: `All Custom Events`
4. Create a Trigger:
5. Publish Your Container: Click the Submit button in the top right corner to publish your changes.
#### 4. Testing Your Implementation
After implementing the code, it’s crucial to test that everything is working correctly.
Methods for Testing:
- Google Analytics Real-Time Reports: Place a test order on your website. Go to Google Analytics and check the Real-Time reports (Conversions > Ecommerce) to see if the purchase is being tracked.
- Google Analytics Debugger (Chrome Extension): Install the “Google Analytics Debugger” Chrome extension. This tool provides detailed information about the data being sent to Google Analytics, helping you identify any errors.
- Google Tag Manager Preview Mode: In GTM, click the Preview button. This will enable a debugging console on your website, showing you which tags are firing and the data being sent to the data layer.
Example Real-Life Scenario:
Let’s say you sell t-shirts online. You notice that a lot of people are adding your “Retro Gamer” t-shirt to their cart, but not completing the purchase. By tracking checkout steps with Enhanced Ecommerce, you discover that many users are abandoning their carts at the shipping information stage. You investigate and find that your shipping costs are too high for certain regions. You adjust your shipping rates, and you see a significant increase in completed sales of the “Retro Gamer” Discover insights on How To Turn The Woocommerce’S Sale-Flash Off t-shirt!
Beyond the Basics
Once you have Learn more about How To Create Product In Woocommerce basic sales tracking set up, you can explore more advanced features:
- Checkout Behavior Analysis: Track the steps in your checkout process to identify drop-off points and optimize your funnel. This requires adding code to track each step (e.g., cart page, shipping information page, payment page). This is more complex and might benefit from using a plugin at this point if your comfort level is low.
- Product Performance Analysis: Analyze which products are selling well, which ones are not, and identify opportunities to improve your product offerings.
- Marketing Attribution: Understand which marketing channels are driving the most sales (e.g., organic search, social media, email marketing).
Conclusion
Tracking WooCommerce sales in Google Analytics without a plugin might seem daunting at first, but it gives you more control and reduces website bloat. By following this guide, you can gain valuable insights into your store’s performance and make data-driven decisions to grow your business. Remember to always back up your files and test your implementation thoroughly. Happy tracking!