Posted: April 9, 2019 at 4:40 pm
Hi there, 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 } } } } |
|
I’ll just add that analytics is otherwise working fine. The transactions tracking is the only part that’s broken. |
|
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:
To be this:
Does your tracking then work as expected? |
|
thanks tony! i’ll try this out tonight and report back. |
|
Thanks again, Tony. That did the trick! I have one other question about the code above: 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? |
|
I should probably add that i’m using MER v2.0.17.rc.000. |
|
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:
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:
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.
I don’t think 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. |
|
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. |
|
What is the product sku in terms of EE? The event ID? |
|
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. |
|
Ok, so if you just want to use the event ID for the SKU then changing this:
To this:
Should work. However, if you want to use a custom value from the event that’s also possible. Add this just after
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 Then change the sky code to:
|
|
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.