Advertisement
prabapro

WPform dataLayer Push

Mar 26th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. // DataLayer push for WP form submission - Added by Praba - Please do not remove
  3. document.addEventListener("DOMContentLoaded", function() {
  4.   var elementsArray = document.querySelectorAll('[id^="wpforms-form-"]');
  5.   elementsArray.forEach(function(elem) {
  6.     elem.addEventListener("submit", function(e) {
  7.       window.dataLayer = window.dataLayer || [];
  8.       window.dataLayer.push({
  9.         event: "wpFormSubmit",
  10.         wpFormElement: event.target
  11.       });
  12.     });
  13.   });
  14. });
  15. </script>
  16.  
  17.  
  18. //DLV to get form elements
  19. {{dlv - wpFormElement}}
  20.  
  21. //CJS to get Form ID
  22. function() {
  23.   var wpFormId = {{dlv - wpFormElement}}.id;
  24.   return wpFormId ? wpFormId : undefined;
  25. }
  26.  
  27. //CJS to Access Form Action
  28. function() {
  29.   var formAction = {{wpFormElement}}.action;
  30.   return formAction ? formAction : undefined;
  31. }
  32.  
  33. //CJS to get form values
  34. function() {
  35.   var field = {{wpFormElement}}.elements.item(0) //First element of form
  36.   return field ? field.value : undefined;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement