Created
October 31, 2025 11:00
-
-
Save larbous/c827ec9e3b4479331ef6dac40b54896a to your computer and use it in GitHub Desktop.
Capture as UTMs em querystrings para formulários do Elementor Pro
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
| <script> | |
| // Cole este código no Custom Code do Elementor e deixe para rodar no HEAD | |
| // Em seguinda crie os campos ocultos para todas as UTMs: 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content' | |
| // Confire o Request Parameter de cada campo para pegar a propria utm | |
| // We are waiting for the full page to load before loading this feature. This prevents a few bugs. | |
| window.addEventListener('load', function () { | |
| var queryForm = function(settings) { | |
| // Parsing URL | |
| var reset = settings && settings.reset ? settings.reset : false; | |
| var self = window.location.toString(); | |
| var querystring = self.split('?'); | |
| // Checking for parameters in the URL | |
| if (querystring.length > 1) { | |
| var pairs = querystring[1].split('&'); | |
| // Iterating through all key-value pairs | |
| for (var i in pairs) { | |
| var keyval = pairs[i].split('='); | |
| // Condition to only save 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content' in cookies | |
| if (reset || sessionStorage.getItem(keyval[0]) === null) { | |
| if (['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'].includes(keyval[0])) { | |
| const d = new Date(); | |
| // Setting cookie expiration time to 14 days | |
| d.setTime(d.getTime() + (14 * 24 * 60 * 60 * 1000)); | |
| let expires = 'expires=' + d.toUTCString(); | |
| // Setting the cookie | |
| document.cookie = keyval[0] + '=' + keyval[1] + ';' + expires + ';path=/'; | |
| } | |
| } | |
| } | |
| } | |
| // Function to get the value of a cookie by its name | |
| function getCookie(cname) { | |
| let name = cname + '='; | |
| let ca = document.cookie.split(';'); | |
| for(let i = 0; i < ca.length; i++) { | |
| let c = ca[i]; | |
| while (c.charAt(0) == ' ') { | |
| c = c.substring(1); | |
| } | |
| if (c.indexOf(name) == 0) { | |
| return c.substring(name.length, c.length); | |
| } | |
| } | |
| return ''; | |
| } | |
| // Finding all hidden fields and text input fields in the form | |
| var hiddenFields = document.querySelectorAll('input[type=hidden], input[type=text]'); | |
| // Iterating through all found fields | |
| for (var i=0; i < hiddenFields.length; i++) { | |
| var elementor_field_name = hiddenFields[i].name; | |
| var elementor_field_name_clean = elementor_field_name.match(/\[(.*?)\]/); | |
| if(elementor_field_name_clean) { | |
| // Getting the cookie value for the corresponding parameter | |
| var param = getCookie(elementor_field_name_clean[1]); | |
| } | |
| // If a corresponding parameter is found in the cookies, populate the field | |
| if(param) { | |
| document.getElementsByName(hiddenFields[i].name)[0].value = param; | |
| } | |
| } | |
| } | |
| setTimeout(function() { | |
| queryForm(); | |
| }, 3000); | |
| }) | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment