Google Analytics for WordPress plugin not tracking WP e-Commerce transactions?

Wednesday, 11th August 2010

I recently tried out Joost de Valk’s Google Analytics for WordPress (version 4) and was happy to see it supported ecommerce tracking for WP e-Commerce.

I had the latest version of WP e-Commerce installed, version 3.7.6.7, and configured the analytics plugin to track transactions but for some reason my ecommerce purchases were not being tracked by Google Analytics.

I did a bit of digging around and discovered the Analytics plugin was looking for the global variable $cart_log_id from which it could get the transaction details, but this variable did not seem to exist (it did seem to be in pre 3.7.6.x versions of WP e-Commerce though).

I have submitted a patch for this for the forthcoming WP e-Commerce version 3.8 (which is currently in beta and looking great, by the way).

In the meantime, if you’re having this problem here’s how you can fix it…

The Fix

You’ll need to get your hands a little bit dirty, but fortunately you only have to edit one file. In the WP e-Commerce plugin folder open the transaction_results.php file.

Step 1

Change the first line that defines global variables to add the $cart_log_id variable:

2
global $wpdb, $user_ID, $nzshpcrt_gateways, $sessionid, $cart_log_id;

Step 2

At the end of the file just before the closing php tag we’ll query the database to get the $cart_log_id that will be used by the analytics plugin:

46
47
$cart_log_id = $wpdb->get_var( "SELECT `id` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`= " . $sessionid . " LIMIT 1" );
?>

…and hopefully that should fix it.

I haven’t tested this for all versions of WP e-Commerce but it should certainly work on the latest 3.7.6.7 version.

10 Responses to “Google Analytics for WordPress plugin not tracking WP e-Commerce transactions?”

  1. Jeffry Ghazally Says:

    Just noticed that it’s been commited to trunk as well, nice one. :D

  2. eschmidt Says:

    Should there be added fields to the GA script on the /transaction-results page? I’m not seeing anything extra added when I implement this fix.

  3. Ben Says:

    It should add transaction information to the transaction results page after a successful purchase. In addition to the standard GA tracking code it will also output transaction information in the following format (but only on the transaction results page though):

    _gaq.push(['_addTrans',
        '1234',           // order ID - required
        'Acme Clothing',  // affiliation or store name
        '11.99',          // total - required
        '1.29',           // tax
        '5',              // shipping
        'San Jose',       // city
        'California',     // state or province
        'USA'             // country
    ]);
    
    // add item might be called for every item in the shopping cart
    // where your ecommerce engine loops through each item in the cart and
    // prints out _addItem for each
    _gaq.push(['_addItem',
        '1234',           // order ID - required
        'DD44',           // SKU/code - required
        'T-Shirt',        // product name
        'Green Medium',   // category or variation
        '11.99',          // unit price - required
        '1'               // quantity - required
    ]);
    _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
  4. eschmidt Says:

    That doesn’t appear to work for me. I am using WP E-Commerce 3.7.6.7 with WP 3.0.1. I noticed my transaction_results.php file did not include $sessionid with the global variables on the first line. I tried adding that as well, but it still didn’t work.

  5. Ben Says:

    You could try position the tracking code manually at the bottom of the page. In the Google Analytics plugin settings select the option to insert the tracking code manually rather than in the header.

    Then, in your footer.php template file add the tracking code template tag just above the closing body tag. Find out more about using the tracking template tag here.

  6. eschmidt Says:

    That appeared to do the trick. My transaction data is now in the source code anyway. We’ll see in a few hours if it makes it to Google. Thanks for your help.

  7. Jonathon Says:

    Hi I have modified code, placed the tracking code template in the footer and still no results.

    Running 3.7.8 with wp 3.0.4

    Thanks!

  8. Ben Says:

    Have you got a link to your site so I can see what it is outputting (if anything)?

  9. Brett Says:

    Hey – last post was almost six months ago; wonder if the updates to plugin have fixed this or still an issue? hate to go meddling and screw up tracking in current ver (4.1.3). Appreciate a heads up if updates have fixed this. great post – thanks.

  10. Ben Says:

    For more information please follow the WP e-Commerce Issue here…