Created
May 18, 2022 23:12
-
-
Save patrickfreitasdev/9ab3aa123dbe25aa646070eff6b45f3f to your computer and use it in GitHub Desktop.
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
| <?php | |
| add_action( 'wp_footer', function(){ | |
| ?> | |
| <script> | |
| function getParam(p) { | |
| var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search); | |
| return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); | |
| } | |
| function getExpiryRecord(value) { | |
| var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds | |
| var expiryDate = new Date().getTime() + expiryPeriod; | |
| return { | |
| value: value, | |
| expiryDate: expiryDate, | |
| }; | |
| } | |
| function addGclid() { | |
| var gclidParam = getParam('gclid'); | |
| var gclidFormFields = ['gclid_field', 'foobar', 'hidden-1']; // all possible gclid form field ids here // Added hidden-1 Forminator field | |
| var gclidRecord = null; | |
| var currGclidFormField; | |
| var gclsrcParam = getParam('gclsrc'); | |
| var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1; | |
| gclidFormFields.forEach(function (field) { | |
| if (document.getElementById(field)) { | |
| currGclidFormField = document.getElementById(field); | |
| } | |
| }); | |
| if (gclidParam && isGclsrcValid) { | |
| gclidRecord = getExpiryRecord(gclidParam); | |
| localStorage.setItem('gclid', JSON.stringify(gclidRecord)); | |
| } | |
| var gclid = gclidRecord || JSON.parse(localStorage.getItem('gclid')); | |
| var isGclidValid = gclid && new Date().getTime() < gclid.expiryDate; | |
| if (currGclidFormField && isGclidValid) { | |
| currGclidFormField.value = gclid.value; | |
| } | |
| } | |
| window.addEventListener('load', addGclid); | |
| (function($){ | |
| $(function(){ | |
| $(document).on('after.load.forminator', function( e, form_id ){ | |
| $(function() { | |
| // Necessary when Form is loading from Ajax | |
| window.addEventListener('load', addGclid); | |
| }); | |
| }); | |
| }); | |
| })(window.jQuery) | |
| </script> | |
| <?php | |
| }, 999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment