Forked from tammalee/wp_gtm_data_layer_gravity_forms.php
Created
February 22, 2021 13:42
-
-
Save lkoudal/c7200f07c788a5b36570211f310f2543 to your computer and use it in GitHub Desktop.
[WP] GTM data layer push for Gravity Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| /** | |
| * 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); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gform_post_submission is deprecated - changed to gform_after_submission