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

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.

???? rehau
 

Ben Huson