Posted: November 11, 2018 at 4:55 pm
I am using Event Espresso plugin on my wordpress site to host in person events. We need to integrate facebook standard events into the site for facebook advertising that we will be doing. We need help in pulling data dynamically from Event Espresso and populating facebook standard event pixel data. We do not want to use facebook plugins or site specific plugin. I have searched your support forums and read everything related to facebook pixel but still have not found my issue. I need help with the following EVENT ESSPRESSO SUPPORT TODO in each function as we need to populate the Pixel data dynamically. Please can you tell me the custom code I need to write to achieve all of the below. 1. For Each Event Page: // EVENT ESSPRESSO SUPPORT TODO: needs to only fire on an each event espresso event page. is there some code to check if it is a event page? function add_fb_lead_event_to_header(){ $page_post_name = get_queried_object()->post_name; if(empty($page_post_name)) { // EVENT ESSPRESSO SUPPORT TODO: Need more elegant solution then this, can you suggest? if(in_array($page_post_name, $pages)) { } 2. Initiate Checkout Page: function add_fb_initiate_checkout_event_to_header(){ if(is_page(‘registration-checkout’)) { add_action(‘wp_head’, ‘add_fb_initiate_checkout_event_to_header’); 3. Thank You Page: function add_fb_purchase_event_to_header(){ $pagename = get_query_var(‘pagename’); if($pagename == ‘thank-you’){ EE_Registry::instance()->load_model( ‘Transaction’ ); if ( ! $transaction instanceof EE_Transaction ) { $registrations = $transaction->registrations(); // FOR DEVELOPER: This maybe incorrect as if nultiple tickets are we getting correct total price? if ( $_REQUEST[‘e_reg_url_link’] == $registration->reg_url_link() ) { // DEBUG '; print_r($registration); echo ' ‘; //FB Tracking Code </script> <?php add_action( ‘wp_head’, ‘add_fb_purchase_event_to_header’ ); Please help! |
|
Hi Ash, What you have in your example here, I’m afraid, isn’t going to work as is or even after a few fill in the blanks. I’ll try help get you pointed in the right direction for making this happen: 1) First, you can use this conditional to make sure you’re on an event page:
2) If you’re in the middle of inline JavaScript, you can grab a PHP value if need be e.g. if you’re on the event page, the event ID is also the post ID: 3) Getting the event ID from later checkout pages is tricky but possible. Here’s a gist that shows how to get the event ID from the active transaction: Getting other information about the order (such as pricing, quantity of tickets, and so on) is also possible via the model system: |
|
Thanks Josh! With what you provided I am nearly 90% there. Couple more questions…please could you help? Also, to note, I am happy to share my full code once this is done as it will really help anyone using Facebook Ads or Google Ads to help in conversion tracking. 1) How do I get the ticket price from the event page? What model is available on that page? Can you send me some sample code? We could have multiple tickets available so we want to get the active ticket. 2) How can I get the number of tickets bought from the registration page? I used following code to help me get the the price and event id from the transaction, but not sure where to look to get the number of tickets bought. Appreciate your help. Ash |
|
I can share an example of how EE4 core gets tickets and their prices when it’s the event page (used to output the structured event data in the source of the page, read by search engines like Google)
To clarify your statement about getting the active ticket, do you mean there are other tickets for the event, but they’re either no longer on sale and/or they haven’t gone on sale yet? If that’s the case, then you use the is_on_sale() method when you loop through the tickets. Here’s a link to its source:
Where you’ve got the transaction object, you can do this: ` |
|
Ok great point 2 works. In regards to point one…how do I get a handle to the event object on the event page…is their a similar call to something like this but to get the event?
|
|
It depends on the context of the code. We have a helper method that attempts to pull in an EE_Event object in whichever way it can (based on the current post or an ID passed to the function), like so:
You can remove
I’d recommend taking a read of the documentation on our model system: https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System We also have helpers within |
|
Here is my code that seems to be working for anyone else interested. Support team let me know if you spot any glaring mistakes here.
?> add_action('wp_head', 'onPageLoad'); /** $post_type = get_post_type(); if ($post_type == 'espresso_events') { $event_name = get_queried_object()->post_name; $event = EEH_Event_View::get_event( $post->ID ); '; print_r($event); echo ' '; $price = 0; ** TicketID='; print_r($ticket->ID()); echo ', Available='; print_r($ticket->available()); echo ', Price='; print_r($ticket->price()); echo ' ** '; '; print_r($ticket); echo ' '; Price='; print_r($price); echo ' '; } /** if (is_page('registration-checkout')) { $checkout = EE_Registry::instance()->SSN->checkout(); if ( $checkout instanceof EE_Checkout ) { $transaction = $checkout->transaction; ** TRANSACTION ** '; '; print_r($transaction); echo ' '; $transaction_total = $transaction->get('TXN_total'); TRANSACTION TOTAL='; print_r($transaction_total); echo ' '; if ( $transaction instanceof EE_Transaction ) { EventId='; print_r($event_id); echo ' '; ?> <!-- Facebook Pixel Code --> add_action('wp_head', 'onEventInitiateCheckout'); /** $pagename = get_query_var('pagename'); if ($pagename == 'thank-you') { $transaction = EE_Registry::instance()->load_model('Transaction')->get_transaction_from_reg_url_link(); if (!$transaction instanceof EE_Transaction) { $registrations = $transaction->registrations(); foreach ($registrations as $key => $registration) { if ($_REQUEST['e_reg_url_link'] == $registration->reg_url_link()) { // DEBUG '; print_r($registration); echo ' '; $event = $registration->event(); EventId='; print_r($event_id); echo ' '; //FB Tracking Code <?php add_action( 'wp_head', 'onEventCheckout' ); |
|
Sorry for the bad formatting maybe Josh/Tony can make a gist to make the code more easily readable. |
|
<script src=”https://gist.github.com/amistry007/a4cdc3af229aa77c3e960f0c083829e1.js”></script> |
|
https://gist.github.com/amistry007/a4cdc3af229aa77c3e960f0c083829e1 |
|
Hi Ash, Firstly, thank you for sharing your code, I’m sure some other users will find it useful. There are a few places that could be improved to prevent errors and depending on the intention of the code could be incorrect under some use cases. If you rename your gist filename to have the extension Can be condensed into just: That’s a lot of hoop jumping for the event name and then you use the global post anyway on the second line, try:
(More on the event name below) If there is more than one ticket on the event, You have the EE_Event object, so rather than creating a variable to hold details from the event object, use the object directly for the name, like so:
Same with the ID: You’re using Meaning you don’t need a This is probably the most important so far: What if you have Multi Event Registrations enabled and add tickets from say 3 different events? That loop is going to set Are you only wanting this output once? For the primary registrant? |
|
Hi Tony, Thank you for taking the time to review the code…really appreciate it. Updated code can be found here
POINT 1: You said…”If there is more than one ticket on the event, $price is always only going to be the price of whichever ticket is last in the loop, is that expected?” I would expect the price to be the first ticket that is has spaces remaining and that is not expired, due to the POINT 2: You said…”What if you have Multi Event Registrations enabled and add tickets from say 3 different events? That loop is going to set $event_id and $event_slug to the ‘last’ event, which isn’t where all the registrations are from, is that expected?” We only have new single events every month so I believe this would still work for us. Am I mistaken here? POINT 3: You said…”Are you only wanting this output once? For the primary registrant?” Yes we only want it for the primary registrant as I believe if the ticket price is $20 and the primary registrant bought 2 tickets, the total price paid would still be reflected as $40 for the primary registrant. For the pixel we just need how many tickets sold and total price paid. Am I missing something here? Ash |
|
No, that’s correct. But what if you have more than 1 ticket available at a time on the event and the user does not select the ‘first’ ticket? Won’t that setup tracking for an incorrect price? If you only have a single ticket available at any one time it won’t matter but as your sharing the code I thought it worth bringing up.
Yes, if you only ever have 1 single event active at a time, it will work for you. If you ever have more than one and a user adds tickets from both events you’ll likely see unexpected results. If you only ever want to track this on a single event, you don’t need this: It’s basically a loop pulling the same details X amount of times (X being the number of tickets for the event). This isn’t the end of the world its just a little inefficient as you could just the primary registration to track the event. So the above could be swapped out for something like:
Grabs the event object for the primary registrant and if you don’t actually have an event you can’t output anything so returns before trying to use it. Then further down again: https://gist.github.com/amistry007/a4cdc3af229aa77c3e960f0c083829e1#file-add_fb_events_to_event_espresso-php-L114-L117 Just use the event object directly (you already know its an EE_Event object because of the above.
(A transaction should always have a registration assigned to it, and a registration should always be assigned to an event, so if you have an EE_Event from an EE_Registration from an EE_Transaction (say that 3 times fast….) you know everything is working as it should).
Again, no, that’s correct, however, you’re basically pulling in more info than you need. You can again use the primary_registrant method above on the transaction to pull just the primary registrant rather than pulling them all and then checking for the primary registrant based on the reg_url_link. Then swap out the variable calls to use I’ll leave that code to you as I’m sure you can work it out from the above 🙂 Edit – Actually, why are you using the registration to pull the price paid? The transaction has the total amount paid, would it not be better to use that value directly?
—- Just thought of one more change you might want to make. This Whilst nothing is actually wrong with them, if you run this code on a non-english site, will not work, or if someone has changed the page slugs for those slugs it wont work. EE stores those pages within the config, so you can use: Registration checkout = Thank You = — I’m sorry if it seems like I’ve ripped your code apart here, the original code works the above is just some bulletproofing. |
|
One final thought, I can see your using Take a look a Kint Debugger, wrap whatever you want to debug in |
|
The support post ‘Facebook Pixel – Populate Dynamically Fields From Event Espresso Event’ 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.