Skip to content

Instantly share code, notes, and snippets.

@johnnyopao
Last active December 15, 2022 11:41
Show Gist options
  • Save johnnyopao/03b44199338feb1745be to your computer and use it in GitHub Desktop.
Save johnnyopao/03b44199338feb1745be to your computer and use it in GitHub Desktop.
<script type="text/javascript">
function yourSubmitFunction(e, $) {
e.preventDefault();
//CUSTOM CODE HERE
// If your code is asynchronous, call this final line in your callback instead
lp.jQuery('.lp-pom-form form').submit();
}
lp.jQuery(function($) {
$('.lp-pom-form .lp-pom-button').unbind('click tap touchstart').bind('click.formSubmit', function(e) {
if ( $('.lp-pom-form form').valid() ) yourSubmitFunction(e, $);
});
$('form').unbind('keypress').bind('keypress.formSubmit', function(e) {
if(e.which === 13 && e.target.nodeName.toLowerCase() !== 'textarea' && $('.lp-pom-form form').valid() )
yourSubmitFunction(e, $);
});
});
</script>
@honzafelt
Copy link

Hey @johnnyopao,

I was wondering whether I could use this script for tracking Unbounce form submissions via dataLayer in GTM. The good folks from Unbounce customer support have referred you to me as the measurement implementation guru. As a non-developer (damn it, Jim, I'm an analyst, not a coder...), I envision the situation thusly.

  1. a visitor submits a form
  2. when the form gets validated, javascript like this fires: dataLayer.push({'event' : 'form-sent', 'formType' : 'lp-01'});​
  3. GTM reads the payload and fires a tag that sends the conversion into Google Analytics / AdWords / FB / RTB system.

My main challenge is to get the dataLayer.push event to fire after the successful submission.

Any ideas?

Thank you so very much,

Honza

@johnnyopao
Copy link
Author

Sorry for the super late reply here @honzafelt. Github doesn't send me notifications for my gist scripts.

I'm sure you already have the answer to this or an alternative, but i'll add my response just incase someone else finds this useful.

This script will only allow you to send events after validation which is also before the data gets submitted. This script cannot hook into after submission. For that i'd suggest sending the data event when they land on a unique confirmation page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment