Adding ecommerce tracking to Easy Digital Downloads

redd angelo 70553

One of the most frustrating aspects of selling products online is the fact that you can’t always tell who you’re selling to or where they’ve come from. As a personal goal this year, I want to improve how I market and promote my products and the first place to start, I think, is by improving my understanding of who’s arriving on my site and what they’re doing when they get there. Step one of that process is to set up ecommerce tracking on Google Analytics.

Download the plugin

I’ve incorporated the code from this article into a plugin which you can download from the plugins directory. Install and activate this on your site and you can start tracking ecommerce data almost immediately. If you prefer just to grab the code to use in your own project or for more information on setting up tracking in Google Analytics, then read on.

The plugin will work with Easy Digital Downloads or with WooCommerce. Through this article I’ll use EDD as the primary example and cover WooCommerce in a separate post – but most of the information here applies equally to both plugins.

Overcoming my fear of Google Analytics

I love charts and graphs as much as the next person – I really do. I find trawling through datasets to see what can be discovered a genuinely enjoyable pastime. But I freely confess to finding Google Analytics monstrous and intimidating. I’m never entirely sure what I’m doing with various filters, segments, and the seemingly constantly changing interface; and there seem to be so many views and levels that it’s just confusing. For this reason, I’ve always been cautious about getting involved with setting up complex tracking. But not any more…

What is ecommerce conversion tracking?

According to the Google Analytics guide:

Ecommerce tracking allows you to measure the number of transactions and revenue that your website generates

Conversion tracking allows you to monitor what happens when a user performs an action on your site such as making a purchase. The site feeds the data back to Google Analytics.

Tracking can also incorporate advertising your site on networks like Google Ads or Facebook but for the purposes of this article, we’re keeping it simple and just concentrating on ecommerce tracking with GA.

What are the benefits of ecommerce tracking?

For me, it seems that the single most important benefit of ecommerce tracking is to help you establish what channels are your most profitable. By this, I mean that you might find that you get most of your traffic from organic search but most of your sales from newsletters; or you might find that certain types of blog posts result in generating more sales than others. Knowing this kind of information means you can decide where best to focus your efforts on improving sales.

What ecommerce data gets tracked?

For each order that gets placed on your site, the plugin will send back the following transaction details:

  • Transaction ID
  • Store name
  • Order total

Then for each item in the order, it will also send the following data:

  • Transaction ID
  • Product name
  • Product SKU
  • Product category
  • Product price
  • Quantity

Setting up ecommerce tracking in Google Analytics

To get things started, we need to brave the beast that is Google Analytics and enable a couple of settings.

Google Analytics goals

First, we need to create a goal in Analytics. Goals can mean several things, but in our case a completed goal is when a user makes a successful purchase. In Easy Digital Downloads when a user successfully makes a purchase, they’re directed to the Purchase Confirmation page. This means that we can set the Purchase Confirmation page as a goal – if a user lands on that page, it means they’ve bought a product and therefore we’ve achieved our goal.

WooCommerce uses endpoints, meaning that the user is directed to a thank you page which will include an extra order-received element within its URLs.

Creating a goal in Google Analytics

To set your Purchase Confirmation page as a goal, log into your Google Analytics account and click on the Admin button. At the moment, that’s down in the bottom left.

From the Administration view, click on Goals in the third column.

Click the red ‘+ New Goal’ button

Give your goal a name – I recommend something sensible like ‘Purchase Confirmation page’.

Under ‘Type’, select ‘Destination’.

Click the blue ‘Continue’ button

Under Goal Details, choose ‘Begins with’ in the ‘Destination’ field. This is where we will enter the URL of our Purchase Confirmation page. Assuming you haven’t altered this since activating Easy Digital Downloads, you just need to enter /checkout/purchase-confirmation in this field. If you are using WooCommerce, you should enter /checkout/order-received/.

Click ‘Save’.

Set up ecommerce tracking for your site

With your goal successfully created, you can enable ecommerce tracking for your site. From the Goals page, click on Ecommerce Settings.

Under Enable Ecommerce, ensure the Status button is toggled to ‘On’. Click ‘Next Step’ then click ‘Submit’.

Adding tracking code to your own site

Adding the necessary code to your site is the easiest bit. If you’re using the plugin, just install and activate it. You can enter your Google Analytics Tracking ID in Settings > Ecommerce Conversion or, if you’re already using a Google Analytics plugin like Monster Insights, the plugin will automatically start working.

If you’re not using the plugin, you can add the following code:

/**
 * Add conversion script for Google Analytics
 * @since 1.0.0
 */
function ect_ga_conversion_script() {
 // Check we're on the purchase confirmation page
 if ( ! edd_is_success_page() ) {
  return;
 }
 global $edd_receipt_args;
 $payment = get_post( $edd_receipt_args['id'] ); // Post data for payment ID
 $meta = edd_get_payment_meta( $payment->ID ); // Meta data about the transaction
 // Get the payment_key
 if ( isset( $_GET[ 'payment_key' ] ) ) {
  $payment_key = $_GET[ 'payment_key' ];
 } else if ( isset( $edd_receipt_args['payment_key'] ) ) {
  $payment_key = $edd_receipt_args['payment_key'];
 } else if ( isset( $meta['key'] ) ) {
  $payment_key = $meta['key'];
 } else {
  // Key not set
  return;
 }
 // Get some transaction data
 $payment_obj = new EDD_Payment( $payment->ID );
 $total = $payment_obj->total;
 if( isset( $meta['cart_details'] ) ) {
  $cart_items = $meta['cart_details'];
 }
 // Monster Insights changes the ga variable name
 if( function_exists( 'MonsterInsights' ) ) {
   $ga = '__gaTracker';
 } else {
   $ga = 'ga';
 }
 /**
  * Now add the script
  * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce
  */ ?>
  <script>
   <?php echo esc_js( $ga ); ?>('require', 'ecommerce');
   <?php echo esc_js( $ga ); ?>('ecommerce:addTransaction', { 'id': '<?php echo esc_attr( $payment_key ); ?>', 'affiliation': '<?php bloginfo( "name" ); ?>', 'revenue': '<?php echo esc_attr( $total ); ?>', });
   <?php // Add item data for each item in the cart
   foreach( $cart_items as $key => $product ) {
    echo esc_js( $ga ) . "('ecommerce:addItem', {
     'id': '" . esc_attr( $product["id"] ) . "',
     'name': '" . esc_attr( $product["name"] ) . "',
     'sku': '" . esc_attr( edd_get_download_sku( $product["id"] ) ) . "',
     'price': '" . esc_attr( $product["item_price"] ) . "',
     'quantity': '" . esc_attr( $product["quantity"] ) . "'
    });\n";
   } ?>
   <?php echo esc_js( $ga ); ?>('ecommerce:send');
  </script>
  <?php
}
add_action( 'wp_footer', 'ect_ga_conversion_script' );

This function:

  • Checks we’re on the Purchase Confirmation page
  • Grabs the payment and other transaction data, including all the items in the cart
  • Checks whether Monster Insights is enabled: for some reason, Monster Insights changes the Google Analytics object variable name
  • Adds the script to the footer. The script passes the transaction data plus data for each item in the cart

How long does it take for data to be recorded?

The data normally takes around 24 hours to hit Google Analytics.

Where do you find the data?

To start viewing the data, head back to Google Analytics. In the left hand menu, click on Conversions then on Goals. For a useful view of which channel is creating the most value for you, click on Acquisition then Overview.

What do you do with the data?

That’s the big question. I’ll be following up with some observations from my own experiences soon.

Three comments

  1. User image

    Hello Gareth,

    Thank you for writing this article and helping to demystify the GA ecommerce set up. I did have a question however. If I am using Woocommerce without any GA plugins installed can I use the code snippet you have provided? I see that it seems to be written for EDD which is not something that I use or am familiar with. Also, if I was able to use this snippet would I just need to add it to the theme functions php file or some other destination. Thank you very much for your time. I will look forward to your response. Have a good day!

    • User image

      Hi James

      If you use the plugin, this will work for EDD or WooCommerce. You won’t need any other plugins, you’ll just need to add your Google Analytics Tracking ID to the settings.

      I have got a follow-up post to this one that looks at WooCommerce – publishing soon.

Leave a Reply

Your email address will not be published. All fields are required.