The client has a select form where the value is not equal to the Display Value.
Example:
The Actual Value is useless because it does not provide any meaning to the client.
I created a custom script in Google Tag Manager that listens to changes in the select input. Then, upon selecting a value, I look at the element using the value and pass the matched item’s Display Value to a hidden form. This hidden form that has the Display Value will now be the one to be tracked, and the user will see it.
<script>// Mutation observer for tracking change events on hidden fieldsvar trackChange = function(element) {MutationObserver = window.MutationObserver || window.WebKitMutationObserver;var observer = new MutationObserver(function(mutations, observer) {if (mutations[0].attributeName == "value") {element.dispatchEvent(new Event("change"));}});observer.observe(element, {attributes: true});}setTimeout(function() {// Assuming you have references to the label elementsvar labelElements = document.querySelectorAll('label.css-bh3upc');var forIndustry = '';var forTrailerModel = '';labelElements.forEach(function(label) {var labelText = label.textContent.trim();if (labelText === 'Industry') {var forAttribute = label.getAttribute('for');forIndustry = forAttribute;}if (labelText === 'Type of Trailer') {var forAttribute = label.getAttribute('for');forTrailerModel = forAttribute;}});console.log('Labels found: ' + forIndustry + ', ' + forTrailerModel);// Get the input elementvar inputElements = document.querySelectorAll('input[name="' + forIndustry + '"], input[name="' + forTrailerModel + '"]');inputElements.forEach(function(inputElement) {// Fire change event on hidden fieldtrackChange(inputElement);// Add an event listener to the input element for value changeinputElement.addEventListener('change', function() {// Get element name dinamically based on the labelvar label = inputElement.closest("div[class*='-container']").parentElement.querySelector('label');label = label.innerText.replace(/ /g, "-");// Create a hidden input elementvar hiddenInput = document.createElement('input');var hiddenInputName = "fx-" + label;hiddenInput.type = "hidden";hiddenInput.name = hiddenInputName;// Find the div that appears before the input element with a class ending in "-singleValue"var div = inputElement.closest("div[class*='-container']").querySelector('div[class*="-singleValue"]');// Set the value of the hidden input to the inner HTML of the divif (div) {hiddenInput.value = div.innerText;}// Append the hidden input to the form that contains the input elementvar form = inputElement.closest('form');if (form) {var elementToRemove = form.querySelector('input[name="' + hiddenInputName + '"]');if (elementToRemove) {elementToRemove.remove();console.log('Removed hidden element: ' + hiddenInputName);}form.appendChild(hiddenInput);console.log('Added hidden element: ' + hiddenInputName + ' with value: ' + hiddenInput.value);}});});}, 3000);</script>
The lead was captured with the correct value for the select input.
Link: Ticket