Support

Home Forums Event Espresso Premium Push Conversion Event to DataLayer

Push Conversion Event to DataLayer

Posted: July 8, 2019 at 5:13 pm


cmccreery

July 8, 2019 at 5:13 pm

Hi there I’m working to push some conversion data into the datalayer that I think would be very helpful to all those that use Google Tag Manager so they can fire events for conversions to various tracking platforms (LinkedIn Insights, Google Analytics, Facebook Pixel.

Here is the datalayer code I want to fire:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event' : 'eventRegistration',
'value' : '120'
});

Could you possibly show me the PHP I could use in the functions.php to handle this?

Thanks,


Josh

  • Support Staff

July 8, 2019 at 7:43 pm

Hi,

May I ask what’s the value “120” coming from? I want to make sure I point you in the right direction in case that’s a value coming from the transaction or event that’s being registered for. Also, is there any other context for this code you want to add?


cmccreery

July 9, 2019 at 1:18 am

Yes the 120 would be the dollar value (without the $) from the total cost of the registration.

There are lots of others that would be very useful (an array of the event ids they registered for, event name, the transaction id, registration code, number of tickets purchased, event categories)

There are probably more that would be very useful for those that want to implement E-commerce or Enhanced E-commerce reporting for Google Analytics as well but I haven’t thought through that as of yet.

Also pushing other events into the data layer for when users move through the checkout steps would be very useful as well.

Thanks,
Chris


Josh

  • Support Staff

July 9, 2019 at 11:11 am

Hi,

I’ll assume this is something you want to add to the thank-you page, if that’s true, then the following PHP could be started with:

add_action(
	'AHEE__thank_you_page_overview_template__top',
	'example_code_function'
);
function example_code_function($transaction) {
    if ( $transaction instanceof EE_Transaction ) {
		?>
    	<script type="text/javascript">
    	jQuery(document).ready(function($) {
    		window.dataLayer = window.dataLayer || [];
			window.dataLayer.push({
				'event' : 'eventRegistration',
				'value' : '<?php echo $transaction->get('TXN_total'); ?>'
			});
    	});
    	</script>
		<?php	
	}
}

Anything else transaction, event, and registration-related you’d like to add can be grabbed from the $transaction object by using the model system which is documented here:

https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System


cmccreery

July 9, 2019 at 12:21 pm

Would this fire every time someone reloaded the Thank You page though?


Josh

  • Support Staff

July 9, 2019 at 2:09 pm

It would as is. You could make it a one time thing by setting a token on the session after the push, then check for that token e.g.

function example_code_function($transaction) {
	if (!isset($_SESSION['gtm-token'])){ 
    if ( $transaction instanceof EE_Transaction ) {
		?>
    	<script type="text/javascript">
    	jQuery(document).ready(function($) {
    		window.dataLayer = window.dataLayer || [];
			window.dataLayer.push({
				'event' : 'eventRegistration',
				'value' : '<?php echo $transaction->get('TXN_total'); ?>'
			});
    	});
    	</script>
		<?php	
	}
	}
	if(! isset($_SESSION['gtm-token'])) {
  		$_SESSION['gtm-token'] = rand(11111, 99999);
	} 
}


cmccreery

July 10, 2019 at 6:58 am

Awesome! Thanks Josh. I will implement that code and see how it goes.

The support post ‘Push Conversion Event to DataLayer’ 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