Events manager plugin is very good plugin for events management in WordPress powered websites. I wanted to track event booking through Google analytic after successful payment. I used events manager pro plugin for payment and try to find out the way to add Google tracking after payment but there is not any option in the plugin because the plugin does not return any information regarding booking ie. booking id, price etc. I googled out to add Google eCommerce Tracking Code for Events Manager Plugin but did not find any solution. After all of these, I reviewed plugin code made some tweaks to resolve this. The Patch is stated here to benefit all of you as follows:
Google eCommerce Tracking Code for Events Manager Plugin
Step 1: Add below code in theme’s functions.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php //Adding filter for adding booking id and amount for google tracking of event add_filter('em_action_booking_add', 'em_action_booking_add_custom', 10, 2); function em_action_booking_add_custom($return, $EM_Booking) { global $post, $EM_Event, $EM_Object, $wpdb; $eventID = $EM_Event->output("#_EVENTID"); $sql = "SELECT * FROM ". EM_BOOKINGS_TABLE . " WHERE event_id=".$eventID." order by booking_id DESC limit 1"; $data = $wpdb->get_row($sql); $booking_ID = ''; $booking_PRICE = ''; if(isset($data) && $data->booking_id > 0 ) { $booking_ID=$data->booking_id; $booking_PRICE=$data->booking_price; } $booking['booking_ID']=$booking_ID; $booking['booking_PRICE']=$booking_PRICE; return $booking; } ?> |
Step 1: Add javascript code below in theme’s header.php file to bind event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <script type="text/javascript"> //Adding code to let google add ecommerce transaction For Events jQuery(document).ready( function($){ jQuery(document).bind('em_booking_success', function(e, response ) { if(response.booking_ID!='' && response.booking_PRICE!='') { _gaq.push(['_addTrans', response.booking_ID, // transaction ID - required 'The Castle Donation', // affiliation or store name response.booking_PRICE, // total - required '0', // tax '0', // shipping '', // city '', // state or province '' // country ]); _gaq.push(['_trackTrans']); } }); }); </script> |
And it’s done! Alternatively you can add script using ‘wp_enqueue_script‘ (the WordPress way) to add Google eCommerce Tracking Code for Events Manager Plugin.