Load CallTrackerFX for pop up windows using click event

gerard_reyes Posted by: Gerard Reyes Date Posted: January 20, 2025
Back to Case Studies

Issue

The client’s site has multiple pop-up windows with their phone numbers. Since the pop-up is loaded only with a click, the CallTrackerFX cannot swap these numbers.

Solution

Create a custom Javascript to load CallTrackerFX on those pop-up windows upon opening them.

<script>
function callCallTrackerFX() {
     console.log('callCallTrackerFX()');
     var runs = 25;
     var fxINT = setInterval(function() {
         runs--;
         if (runs == 0) {
             console.log('clearing');
             clearInterval(fxINT);
         }
         if (CallTrack.numbersToReplace) {
             CallTrack.numbersToReplace.forEach(function(item) {
                 if (item.number.length) {
                     // The number changes before this is executed so add the changed number in the array too.
                     item.number.push(item.replace);
                     // Makes the array unique
                     item.number = item.number.filter(function(value, index, self) {
                     return self.indexOf(value) === index;
                 });
                 item.number.forEach(function(thenums) {
                     var replace = thenums.replace(/\D/g, ''); // Enable the regex for this ticket.
                     replace = replace.trim(); // Trim because sometimes there are trailing spaces in the number.

                     var formattedReplace = replace.replace(/\D/g, ''); // Remove non-numeric characters
                     formattedReplace = formattedReplace.substring(0, 3) + '-' + formattedReplace.substring(3, 6) + '-' + formattedReplace.substring(6);
                     var selector = 'a[href*="' + replace + '"], a[href*="' + formattedReplace + '"]';

                     document.querySelectorAll(selector).forEach(function(thenum) {
                         thenum.href = 'tel:' + item.tel;
                         thenum.text= thenums;
                     });
                 });
                 }
             });
         }
     }, 500);
}

callCallTrackerFX();

// Get all elements with the class 'elementor-icon'
var elements = document.querySelectorAll('.elementor-icon');

// Loop through each element and add a click event listener
for (var i = 0; i < elements.length; i++) {
     elements[i].addEventListener('click', function() {
     console.log('Element with class "elementor-icon" clicked!');
     callCallTrackerFX();
     });
}
</script>

Achievement

CallTrackerFX is loaded on the pop up window and the numbers are swapped.

Snippet/Screenshot/Link

Task: Link

Code: Screenshot