Support

Home Forums Event Espresso Premium Update on Jan 16 (4.9.76.p/4.9.77.p) broke my transaction tracking

Update on Jan 16 (4.9.76.p/4.9.77.p) broke my transaction tracking

Posted: April 9, 2019 at 4:40 pm

Viewing 10 reply threads


Praxis

April 9, 2019 at 4:40 pm

Hi there,
We just learned last week that Google analytics quit tracking our transactions when I upgraded our site to EE core 4.9.76.p/4.9.77.p (both on the same date in January). We are currently using the latest version of EE core (4.9.79).
The developer who wrote our transaction tracking script checked it out and “everything seems to be fine”. He doesn’t have time to troubleshoot any further.

Can you guys help me figure out how to fix our tracking code or perhaps point to which changes in that update are likely affecting us?

We’re using this GA plugin: https://wordpress.org/plugins/google-analytics-dashboard-for-wp/ Version 5.3.7

Our custom code:

// Google Ecommerce tracking -------------------------------------------------------------------*/
add_action( 'AHEE__thank_you_page_transaction_details_template__after_transaction_table_row', 'ga_ecommerce_tracking', 10, 1 );

/**
 * Google Analytics e-commerce tracking.
 *
 * @param object $transaction
 *
 * @return void
 */
function ga_ecommerce_tracking( $transaction ) {

	if( $transaction ) {
		
		$transaction = EEM_Transaction::instance()->get_one_by_ID( $transaction->ID() );
		
		if( method_exists( $transaction, 'line_items' ) ) {

			$line_items = $transaction->line_items();
	
			if( ! empty( $line_items ) ) {
				
				?>
				<script type="text/javascript">
				ga('ecommerce:addTransaction', {
					'id': '<?php echo $transaction->ID(); ?>',
					'affiliation': 'Events',
					'revenue': '<?php echo $transaction->total(); ?>',
					'shipping': '0',
					'tax': '<?php echo $transaction->tax_total(); ?>'
				});
	
				<?php foreach( $line_items as $line_item ): ?>
				
					<?php if( $line_item->type() == 'line-item' ): ?>
	
						<?php
						$object = $line_item->get_object();
						$parent = $line_item->parent();
						$category = $object->name();
						
						$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
					    $query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $parent->OBJ_ID();
						$categories = EEM_Term::instance()->get_all( $query_params );
						
						if( ! empty( $categories ) ) {
							foreach( $categories as $category ) {
								$category = $category->name();
								break;
							}
						}
						?>
		
						<?php if( ! empty( $object ) && ! empty( $parent ) ): ?>
						
							ga('ecommerce:addItem', {
								'id': '<?php echo $transaction->ID(); ?>',
								'name': '<?php echo addslashes( $parent->name() ); ?>',
								'sku': '<?php echo $object->ID(); ?>',
								'category': '<?php echo addslashes( $category ); ?>',
								'price': '<?php echo $line_item->unit_price(); ?>',
								'quantity': '<?php echo $line_item->quantity(); ?>'
							});
						
						<?php endif; ?>
					
					<?php endif; ?>
				
				<?php endforeach; ?>
	
				ga('ecommerce:send');
				ga('ecommerce:clear');
				</script>
				<?php
				
			}
		
		}
	
	}

}


Praxis

April 9, 2019 at 4:44 pm

I’ll just add that analytics is otherwise working fine. The transactions tracking is the only part that’s broken.


Tony

  • Support Staff

April 9, 2019 at 5:51 pm

Hi there,

We’ll need to investigate this a little more, but it looks like the hook you are using isn’t loading on the page anymore and that’s not expected.

If you switch this:

add_action( 'AHEE__thank_you_page_transaction_details_template__after_transaction_table_row', 'ga_ecommerce_tracking', 10, 1 );

To be this:

add_action( 'AHEE__thank_you_page_overview_template__bottom', 'ga_ecommerce_tracking', 10, 1 );

Does your tracking then work as expected?


Praxis

April 9, 2019 at 5:59 pm

thanks tony! i’ll try this out tonight and report back.


Praxis

April 10, 2019 at 5:12 pm

Thanks again, Tony. That did the trick!

I have one other question about the code above:
It includes an attempt to send the post ID of the event to GA as the Product SKU. It looks like it’s logical, but it doesn’t work – The sku that appears in GA for each product is a totally different number. I can’t guess where the sku is coming from, actually.

Would you have an idea of how to set the sku from the event post id? Or even better, how to get a sku from the contents of a custom field?


Praxis

April 10, 2019 at 5:17 pm

I should probably add that i’m using MER v2.0.17.rc.000.


Tony

  • Support Staff

April 15, 2019 at 9:37 am

To answer your question, the reason the SKU is not the event ID is due to the fact that the code loops over all of the line items for a transaction and uses those line_items to pass data to GA. However, at no point does the code check for an event object, to confirm if you’re actually working with an event and line items usually contain much more than an event object which is why your getting ID’s that you don’t recognise.

What data are you trying to send to GA?

I can’t go through an review all of your code for you, but I can point out some of the odd things I’ve noticed:

if( $transaction ) {
    $transaction = EEM_Transaction::instance()->get_one_by_ID( $transaction->ID() );

The hooks you are using are passed the transaction object, so your using the transaction object, to pull the ID of the transaction object, to pull the transaction object.

It’s easier, and less expensive, to just confirm you have an EE_Transaction object to begin with:

if(! $transaction instanceof EE_Transaction ) {
    return;
}

because if you don’t then you’ve got bigger problems. If its anything other than an EE_Transaction object, the ID isn’t going to be correct anyway.

$object = $line_item->get_object();
$parent = $line_item->parent();
$category = $object->name();
						
$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
$query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $parent->OBJ_ID();
$categories = EEM_Term::instance()->get_all( $query_params );

I don’t think $parent is what you expect it to be, it’s not an Event and at that point is likely an EE_Line_Item object.

What details are you trying to send over to GA? It may be better to give you an example of how to pull what you need.


Praxis

April 23, 2019 at 3:39 pm

I see what you mean, it does look kinda messy. It was written by a programmer I found on Codeable. Apparently they don’t have many programmers who know EE very well.

I’m trying to send transaction id, product sku, product name (event name), product category (event category), price, and quantity. All of these items appear to be reporting accurately except for the SKU, i believe.


Tony

  • Support Staff

April 24, 2019 at 1:53 am

What is the product sku in terms of EE? The event ID?


Praxis

April 25, 2019 at 10:58 pm

yes, the event/post id as the product sku was the original aim. Even better would be to send the contents of a custom field in the event as the product sku.


Tony

  • Support Staff

May 1, 2019 at 8:33 am

Ok, so if you just want to use the event ID for the SKU then changing this:

'sku': '<?php echo $object->ID(); ?>',

To this:

'sku': '<?php echo $parent->OBJ_ID(); ?>',

Should work.

However, if you want to use a custom value from the event that’s also possible.

Add this just after $parent = $line_item->parent();

$sku = null;
if ( $parent->OBJ_type() == 'Event' ) {
    $sku = get_post_meta( $parent->OBJ_ID(), {custom_field_key}, true );
}

if ( empty($sku) ) {
   $sku = $parent->OBJ_ID();
}

Which says if the object type linked to the parent line item is an Event, pull a custom value using the object’s ID, if nothing is returned (or the object isn’t an event) just use the object ID for sku.

You’ll need to change {custom_field_key} to you custom field’s key.

Then change the sky code to:

'sku': '<?php echo $sku; ?>',

Viewing 10 reply threads

The support post ‘Update on Jan 16 (4.9.76.p/4.9.77.p) broke my transaction tracking’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso