Support

Home Forums Event Espresso Premium Only Load Thank You page conversion code on first load

Only Load Thank You page conversion code on first load

Posted: October 22, 2019 at 3:34 pm

Viewing 10 reply threads


Daniel

October 22, 2019 at 3:34 pm

Hello!

I am customizing the Thank You page with Google Conversion code.

I was able to use the method described here to place JS code on the page:
https://eventespresso.com/topic/ee4-google-pixel-code-on-thank-you-page/

My problem is that the Thank You page can be reloaded multiple times and the conversion code runs every time. So, I need a way to only load/run this JS conversion code on the first load of the Thank You page for a transaction.

I set up a custom question (admin only – conversion-tracked) to track this status and I was able to use these techniques to get the answer to the question:

https://eventespresso.com/topic/i-need-to-get-custom-questions-answers-from-the-registration-answers-object/
https://gist.github.com/joshfeck/63fe625950c9a71e19be
https://eventespresso.com/topic/get-wp_user-id-from-a-transaction/

This is all great!

However, I cannot find an example of setting an answer value on a custom question using code.

My idea was to create a page that is called (by ajax) by the generated JS on the Thank You page and code triggered by this page load would set the custom question answer for the transaction/registration to “tracked.” That would prevent the conversion code from loading the subsequent page loads of the Thank You page.

However, I can’t seem to figure out how to set an answer to a custom question by code.

Can you assist?

Either with a pointer to a code sample or an answer that says “you are doing it all wrong” and point me to the EE feature that handles this already!

Thank you!


Daniel

October 22, 2019 at 3:50 pm

Here is the code that gets the custom question answer…
// from: https://eventespresso.com/topic/i-need-to-get-custom-questions-answers-from-the-registration-answers-object/
// and: https://gist.github.com/joshfeck/63fe625950c9a71e19be
// more code from: https://eventespresso.com/topic/get-wp_user-id-from-a-transaction/


// store and retrieve the custom question answered with this purchase
// this enables the conversion tracking code to only load on one page
function get_conversion_tracked ($transaction) {
	$conversion_tracked = 0;
	$question_id = 18; // replace with custom question's ID
	// get the attendee to access the custom question
	// that tracks the conversion tracking status (thank you page already loaded)
	$reg = $transaction->get_first_related( 'Registration' );
	if ( $reg instanceof EE_Registration ) {
		$event_id = $reg->event()->ID();
		$contact = $reg->attendee();
		if ( $contact instanceof EE_Attendee ) {
			$att_id = $contact->get( 'ATT_ID' );
		}

		if( $contact instanceof EE_Attendee ) {
			$answer_value = EEM_Answer::instance()->get_var( 
				array(
					array(
						'Registration.ATT_ID' => $att_id,
						'Registration.EVT_ID' => $event_id,
						'QST_ID' => $question_id 
					),
					'order_by' => array(
						'ANS_ID' => 'DESC'
					),
					'limit' => 1
				),
				'ANS_value' 
			);
		}
	}
	return $answer_value;	
}


Josh

  • Support Staff

October 22, 2019 at 4:02 pm

Hi,

I can advise to check this other topic:

https://eventespresso.com/topic/push-conversion-event-to-datalayer/#post-295823

The replies in the topic include a solution for pushing the conversion code only on the first page load.


Daniel

October 22, 2019 at 10:14 pm

Thanks Josh!

I looked at that thread and it is very informative; also helpful to know that someone else is working on the same issue. Maybe we can come up with some kind of standard way to do this.

The cookie is a good “first line of defense.” It is straightforward and it will work to prevent conversion code running on page reloads in the same browser, but the thank you page is accessible with no cookie – you can copy the URL and load it up in a different browser and it will run the conversion code again. I know it’s not likely, but I want to prevent it altogether.

I am looking for a server-side conversion tracking solution and the only piece missing from an EE centric version of that right now is my understanding of “updates while querying EE models.”

I wonder if you can point me to a code example of updating a custom question answer starting from data that’s available in the thank you page context?

Thanks!


Tony

  • Support Staff

October 23, 2019 at 4:10 am

Hi there,

Either with a pointer to a code sample or an answer that says “you are doing it all wrong” and point me to the EE feature that handles this already!

I’m glad you added this…

Whilst I have no problem giving you details on how to save the answer to a custom question assigned to a registration, what I don’t want to do is lead you down the ‘wrong’ path for something when there is a simple solution to what you need (although its managed differently which may be a deal-breaker for you, I’m not sure with the above).

Do you need a registration object here?

The thank-you page loads all of the details for the group, yet the method you are using would (eventually) pull ‘a’ registration from the transaction and assign an answer to it. That’s likely going to be the ‘primary registration’ for the group (which we have a method specifically for if needed) but I assume your tracking is for the whole transaction (even if it’s just one registration) not a specific registration in the group?

The hooks on the thank you page are passed an EE_Transaction object, and with our models, you can pull pretty much any information you need from that.

However, what I would do here is simply add some ‘extra meta’ (What is extra meta?) to the transaction itself and then check for it before displaying the tracking data.

Which simplifies your code to something like:

if( ! $transaction->get_extra_meta('conversion-tracked', true) ) {
    // Output tracking code here

    $transaction->update_extra_meta('conversion-tracked', true);
}

On initial page load, the value will not be set and the above conditional will return true so your tracking code outputs and then the transaction’s extra meta value is ‘updated’. Refreshing the page, revisiting the page etc will now evaluate to true so the tracking code will no longer load.

If you want to use wp_footer to output your tracking code, that is not an EE hook and it won’t have the transaction object passed to it. In that case either hook into wp_footer in the above with your own hook to output the code, or just hook directly into wp_footer, run an is_page() check to confirm it’s the thank you page and then use something like what we have in the docs:

https://github.com/eventespresso/event-espresso-core/blob/master/docs/T–Tutorials/customizing-the-ee-thank-you-page.md#user-content-getting-the-transaction-data

To pull the transaction object using the url (check you have an EE_Transaction object before trying to use it) and then output your code.

The ‘downside’ to this is you won’t be populating the custom question you added and you won’t have a visual indication that the above is set on a transaction without some more custom code to load something in the admin so it depends if thats needed or just a workaround.


Daniel

October 23, 2019 at 7:32 am

This looks like exactly what I need. Much simpler! Thank you.

For my current task (protect the Thank You page conversion code from running more than once) I do not need any admin-visible UI so this will work.

I had no idea about the meta feature of the EE models.

There is one drawback of this method (which is acceptable for the code simplicity it offers). Using the extra_meta method, you have to assume that the JS loads, runs, and completes. The meta info makes a nice place to mark the successful emission of the JS code to the page, but it doesn’t guarantee that that JS code runs. The only way to make sure that JS code runs would be to write JS code that calls back to the site and updates it. That’s what I was initially aiming at, but I’d rather keep the code and process simpler.

JS-run-in-browser-guarantee is not a requirement for this current work; I just wanted to make a note of the tradeoff here.

Cheers!


Daniel

October 23, 2019 at 11:43 am

One more thing…

I have not been able to figure out how to get the Quantity of a line item from the transaction.

I see in the Model Relations table that they are directly related, but I can’t get the right syntax to pull out each quantity for each line item.

Can you point me to a code example that will output the quantity purchased per registration? I tried $registration->count() but that isn’t the count that I need (it counts registrations, not items in each).

Thanks!


Tony

  • Support Staff

October 23, 2019 at 12:15 pm

Hi Daniel,

Can you define ‘items’ here?

1 ticket == 1 registration.

$registration->count() is the ‘reg count’, so the count number for that specific registration in a group. So a single ticket in a ‘group’ would always return 1 for count() on the registration. Select 2 tickets and count would return 1 then 2 for each respective registration, with a group_size() value of 2.

‘Line items’ build out the transaction, but any object type could have line items so I’m not sure what you need.

I could try and point you in the right direction but I’m not really sure what it is you are after and need more details.


Daniel

October 23, 2019 at 12:25 pm

Thanks for the quick reply!

This is for an art school.
People buy classes and materials.
I’ve decided for a few reasons to handle the materials as “tickets” just to keep things organized (this was suggested in another thread early on in my research for this project).
I need to send the number of materials purchased to the Google Analytics code to track how many of each material was purchased. People can buy several canvases for instance.

I see the data is there in items_purchased() below as LIN_quantity. But I can’t seem to get that data using this code (they all output “1” which is weird):

<?php $items = $transaction->items_purchased(); 
foreach ($items as $item) {
//echo '<pre>'.print_r(get_class_methods($item)).'</pre>';
echo '<pre>'.print_r(($item)).'</pre>';
echo '<pre>'.print_r($item->unit_price).'</pre>';
echo '<pre>'.print_r($item->total).'</pre>';
echo '<pre>'.print_r($item->quantity).'</pre>';
echo '<pre>'.print_r($item->name).'</pre>';
}
?>

The Data in the $item where I see the correct Quantity (this one has 2).


[_fields:protected] => Array
 (
[LIN_ID] => 1077
[LIN_code] => c640f5b290ebb0032fe858bce02e55bf
[TXN_ID] => 117
[LIN_name] => Acrylic Painting No-Fuss Art Kit
[LIN_desc] => (For Acrylic Painting Class Materials)
[LIN_unit_price] => 75
[LIN_percent] => 0
[LIN_is_taxable] => 1
[LIN_order] => 0
[LIN_total] => 150
[LIN_quantity] => 2
[LIN_parent] => 1076
[LIN_type] => line-item
[OBJ_ID] => 83
[OBJ_type] => Ticket
[LIN_timestamp] => EventEspresso\core\domain\entities\DbSafeDateTime Object
...SNIP


Tony

  • Support Staff

October 24, 2019 at 4:45 am

echo '<pre>'.print_r(($item)).'</pre>';
echo '<pre>'.print_r($item->unit_price).'</pre>';
echo '<pre>'.print_r($item->total).'</pre>';
echo '<pre>'.print_r($item->quantity).'</pre>';
echo '<pre>'.print_r($item->name).'</pre>';

None of those properties exist on a EE_Line_Item object, they exist as methods, but not properties. So for the above to work it needs to be:

echo '<pre>'.print_r(($item)).'</pre>';
echo '<pre>'.print_r($item->unit_price()).'</pre>';
echo '<pre>'.print_r($item->total()).'</pre>';
echo '<pre>'.print_r($item->quantity()).'</pre>';
echo '<pre>'.print_r($item->name()).'</pre>';

The reason you are getting ‘1’ is actually because null returned and print_r is being echo’d, easiest way to explain is to link you to: https://stackoverflow.com/a/3771126

If you switch your print_r‘s to var_dump you’ll see you get null.

$item->quantity() appears to be what you need, although you probably want to make sure you have an EE_Line_Item object before using it as above.

Also, print_r and var_dump are all well and good by my recommendation is to use Kint. Wrap whatever you have in d(); and you’ll get a much nicer output to view properties, methods etc.

So in your example swap this:

echo '<pre>'.print_r(($item)).'</pre>';

For just:

d($item);

Obviously, after you’ve installed the above plugin.

You’ll then get something like this – https://monosnap.com/file/oyeq7PL4QuVAGQiUBQAiFJBlBXFKxg

Clicking the available methods lets you see all of the methods available on that object and you can dig deeper into each line if needed. A word of caution though, don’t click the + symbols, they expand EVERY child section which quickly mounts up on say model_relations when you expanding objects within objects. Click the line itself or the name just to open that single line then work through the levels from there.


Daniel

October 28, 2019 at 2:32 pm

Thank you!

I got it working now.

Dan

Viewing 10 reply threads

The support post ‘Only Load Thank You page conversion code on first load’ 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