Skip to content

Instantly share code, notes, and snippets.

@tammalee
Last active May 13, 2024 15:04
Show Gist options
  • Save tammalee/68cc55c398be7e6d992fac7aa867f690 to your computer and use it in GitHub Desktop.
Save tammalee/68cc55c398be7e6d992fac7aa867f690 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_post_submission", "gf_gtm_tracking", 10, 2);
@Ybyonas1
Copy link

Hey! Where exactly do I add this code into the backend of my site. I've added it into to the functions.php file underneath themes > divichild > functions.php, however I am still not seeing the inputed data from the form submission. Could really use your help!

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