Posted: 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: 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: 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! |
|
Here is the code that gets the custom question answer…
|
|
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. |
|
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! |
|
Hi there,
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 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:
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 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. |
|
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! |
|
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 Thanks! |
|
Hi Daniel, Can you define ‘items’ here? 1 ticket == 1 registration.
‘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. |
|
Thanks for the quick reply! This is for an art school. 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):
The Data in the $item where I see the correct Quantity (this one has 2).
|
|
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:
The reason you are getting ‘1’ is actually because If you switch your
— Also, print_r and var_dump are all well and good by my recommendation is to use Kint. Wrap whatever you have in So in your example swap this:
For just:
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. |
|
Thank you! I got it working now. Dan |
|
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.