CallTrackerFX – Phone number not swapping on load/button

Posted by: Nico Cagalitan Date Posted: April 13, 2026
Back to Case Studies

Issue

The phone numbers are not swapping on a CTA button that is configured differently, and when a phone number is loaded after a button click, or engagement.

Solution

Able to create a custom code that fixes the phone load issue where CTFX can’t see the phones after it finished running.

Achievement

Able to swap/replace the phone numbers on a CTA button and after load of the DOM

Snippet/Screenshot/Link

Ticket: https://app.webfx.com/todos/268761772
Code:
var mcfxReady = false;
var replacementTimeout = null;

// Try to initialize mcfx with retries
function initMcfx() {
try {
mcfx(function(t) {
mcfxReady = true;
// Initial replacement
if (t.modules && t.modules.calltracking && typeof t.modules.calltracking.replaceNumbers === ‘function’) {
t.modules.calltracking.replaceNumbers(document.body, true);
}
});
} catch(err) {
setTimeout(initMcfx, 1000);
}
}

initMcfx();

// Debounced replacement function to avoid excessive calls
function triggerPhoneReplacement() {
if (!mcfxReady) return;

// Clear any pending replacement
if (replacementTimeout) {
clearTimeout(replacementTimeout);
}

// Debounce to avoid replacing too frequently
replacementTimeout = setTimeout(function() {
mcfx(function(t) {
if (t.modules && t.modules.calltracking && typeof t.modules.calltracking.replaceNumbers === ‘function’) {
t.modules.calltracking.replaceNumbers(document.body, true);
}
});
}, 100);
}

// Watch for DOM changes that might affect phone numbers
var observer = new MutationObserver(function(mutations) {
if (!mcfxReady) return;

var shouldReplace = false;

mutations.forEach(function(mutation) {
// Check if nodes were added or removed
if (mutation.type === ‘childList’ && (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0)) {
shouldReplace = true;
}

// Check if text content changed
if (mutation.type === ‘characterData’) {
shouldReplace = true;
}

// Check if href attribute changed (for tel: links)
if (mutation.type === ‘attributes’ && mutation.attributeName === ‘href’) {
shouldReplace = true;
}

// Check if class changed (might affect visibility of phone numbers)
if (mutation.type === ‘attributes’ && mutation.attributeName === ‘class’) {
var target = mutation.target;
if (target.querySelector && (target.querySelector(‘a[href*=”tel:”]’) || target.textContent.match(/\d{3}[-.\s]?\d{3}[-.\s]?\d{4}/))) {
shouldReplace = true;
}
}
});

if (shouldReplace) {
triggerPhoneReplacement();
}
});

// Observe with comprehensive options
observer.observe(document.body, {
childList: true, // Watch for nodes being added/removed
subtree: true, // Watch all descendants
characterData: true, // Watch for text content changes
attributes: true, // Watch for attribute changes
attributeFilter: [‘href’, ‘class’] // Specifically watch href and class changes
});