Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lkoudal/c7200f07c788a5b36570211f310f2543 to your computer and use it in GitHub Desktop.

Select an option

Save lkoudal/c7200f07c788a5b36570211f310f2543 to your computer and use it in GitHub Desktop.
[WP] GTM data layer push for Gravity Forms
<?
/**
* GTM data layer push for gravity forms contact form
*/
/**
* Pushes a submission variables to the GTM dataLayer
* Also pushes the event label for use in GTM tracking
* @param Array $entry the data submitted with the form
* @param Array $form Form data
* @return null
*/
function gf_gtm_tracking($entry, $form) {
//FORM IDS:
// 4 = contact, 5 = venue inquiry
// array of form IDs we care about tracking
$form_ids = array(4, 5);
//if the form ID isn't one of the ones we have specified
//return without doing anything
if(!in_array($form['id'], $form_ids)) return;
switch ($form['id']) {
case '4': ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// var eventLabel = $('#event_label').val();
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event' : 'formSubmissionSuccess',
'formId' : 'bookThisVenueForm'
});
});
</script>
<?php
break;
case '5': ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// var eventLabel = $('#event_label').val();
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event' : 'formSubmissionSuccess',
'formId' : 'contactForm'
});
});
</script>
<?php
break;
default:
# code...
break;
} //end switch
} //end function
add_action("gform_after_submission", "gf_gtm_tracking", 10, 2);
@lkoudal
Copy link
Author

lkoudal commented Feb 22, 2021

gform_post_submission is deprecated - changed to gform_after_submission

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