Skip to content

Instantly share code, notes, and snippets.

@amistry007
Last active December 8, 2018 07:17
Show Gist options
  • Save amistry007/a4cdc3af229aa77c3e960f0c083829e1 to your computer and use it in GitHub Desktop.
Save amistry007/a4cdc3af229aa77c3e960f0c083829e1 to your computer and use it in GitHub Desktop.
<?php
/**
* Code to trigger on every page.
*/
function onPageLoad() {
?>
<!-- Facebook Pixel Code -->
<!-- PageView -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '<< ADD YOUR FB PIXEL ID HERE >>');
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=<< ADD YOUR FB PIXEL ID HERE >>&ev=PageView&noscript=1"/>
</noscript>
<!-- End Facebook Pixel Code -->
<?php
}
add_action('wp_head', 'onPageLoad');
/**
* Code to trigger on event detail page.
*/
function onEventPageLoad() {
if (get_post_type() === 'espresso_events') {
$event = EEH_Event_View::get_event();
#echo '<pre>'; print_r($event); echo '</pre>';
if(! $event instanceof EE_Event ) {
return;
}
$price = 0;
foreach ($event->tickets() as $ticket) {
#echo '<pre> ** TicketID='; print_r($ticket->ID()); echo ', Available='; print_r($ticket->available()); echo ', Price='; print_r($ticket->price()); echo ' ** </pre>';
#echo '<pre>'; print_r($ticket); echo '</pre>';
if ($ticket->is_remaining() && !$ticket->is_expired()) {
$price = $ticket->price();
#echo '<pre>Price='; print_r($price); echo '</pre>';
break;
}
}
?>
<!-- Facebook Pixel Code -->
<script>
fbq('track', 'ViewContent', {
value: <?php echo $price ?>,
currency: 'GBP',
content_name: '<?php echo $event->name(); ?>',
content_type: 'product', // Required for Dynamic Product Ads
content_ids: '<?php echo $event->ID(); ?>' //Required for Dynamic Product Ads -> Event Espresso Post ID
});
</script>
<!-- End Facebook Pixel Code -->
<?php
}
}
add_action('wp_head', 'onEventPageLoad');
/**
* Code to trigger on event registration page.
*/
function onEventInitiateCheckout(){
if (is_page('registration-checkout')) {
$checkout = EE_Registry::instance()->SSN->checkout();
if ( $checkout instanceof EE_Checkout ) {
$transaction = $checkout->transaction;
if ( $transaction instanceof EE_Transaction ) {
#echo '<pre>** TRANSACTION **</pre>';
#echo '<pre>'; print_r($transaction); echo '</pre>';
#echo '<pre> TRANSACTION TOTAL='; print_r($transaction->total()); echo '</pre>';
foreach ( $transaction->registrations() as $registration ) {
if ( $registration instanceof EE_Registration ) {
$event = $registration->event();
if ( $event instanceof EE_Event ) {
$event_id = $event->ID();
$event_slug = $event->get(EVT_slug);
#echo '<pre> EventId='; print_r($event_id); echo '</pre>';
}
}
}
}
}
?>
<!-- Facebook Pixel Code -->
<script>
fbq('track', 'InitiateCheckout', {
value: <?php echo $transaction->total() ?>,
currency: 'GBP',
content_name: '<?php echo $event_slug; ?>',
content_category: 'Checkout',
content_type: 'product', // Required for Dynamic Product Ads
content_ids: '<?php echo $event_id; ?>', //Required for Dynamic Product Ads -> dynamic espresso event id
num_ids: <?php echo count($transaction->registrations()); ?> //Number of espresso tickets bought
});
</script>
<!-- End Facebook Pixel Code -->
<?php
}
}
add_action('wp_head', 'onEventInitiateCheckout');
/**
* Code to trigger on checkout thank you page.
*/
function onEventCheckout() {
$pagename = get_query_var('pagename');
#echo "page name = "; echo $pagename;
if ($pagename == 'thank-you') {
$transaction = EE_Registry::instance()->load_model('Transaction')->get_transaction_from_reg_url_link();
if (!$transaction instanceof EE_Transaction) {
exit();
}
$registrations = $transaction->registrations();
foreach ($registrations as $key => $registration) {
if ($_REQUEST['e_reg_url_link'] == $registration->reg_url_link()) {
// DEBUG
#echo '<pre>'; print_r($registration); echo '</pre>';
$event = $registration->event();
if ( $event instanceof EE_Event ) {
$event_id = $event->ID();
$event_slug = $event->get(EVT_slug);
#echo '<pre> EventId='; print_r($event_id); echo '</pre>';
}
//FB Tracking Code
?>
<script>
fbq('track', 'Purchase', {
value: <?php echo $registration->price_paid( ); ?>,
currency: 'GBP',
content_name: '<?php echo $event_slug; ?>', // Dynamic event name
content_type: 'product', // Required for Dynamic Product Ads
content_ids: '<?php echo $event_id; ?>', // Required for Dynamic Product Ads -> dynamic espresso event id
num_ids: <?php echo count($transaction->registrations()); ?> //Number of espresso tickets bought
});
</script>
<?php
}
}
}
}
add_action( 'wp_head', 'onEventCheckout' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment