function calculateConversionRate() {
// Use 'var' as requested
var visitorsInput = document.getElementById('totalVisitors').value;
var conversionsInput = document.getElementById('totalConversions').value;
var resultDiv = document.getElementById('conversionResult');
// Basic validation before parsing
if (visitorsInput.trim() === "" || conversionsInput.trim() === "") {
resultDiv.style.display = "block";
resultDiv.innerHTML = 'Please enter values for both fields.';
return;
}
var visitors = parseFloat(visitorsInput);
var conversions = parseFloat(conversionsInput);
// Validate numbers and ensure visitors > 0 to avoid division by zero
if (isNaN(visitors) || isNaN(conversions)) {
resultDiv.style.display = "block";
resultDiv.innerHTML = 'Please enter valid numeric values.';
return;
}
if (visitors <= 0) {
resultDiv.style.display = "block";
resultDiv.innerHTML = 'Total visitors must be greater than zero.';
return;
}
if (conversions < 0) {
resultDiv.style.display = "block";
resultDiv.innerHTML = 'Conversions cannot be negative.';
return;
}
if (conversions > visitors) {
resultDiv.style.display = "block";
// While technically possible in some tracking setups due to attribution lag, usually it indicates an error.
resultDiv.innerHTML = 'Note: Conversions are higher than visitors. Please verify your data.';
// Proceeding with calculation anyway as it might be intentional in rare edge cases.
}
// The Calculation Logic
var rate = (conversions / visitors) * 100;
// Formatting output to 2 decimal places
var roundedRate = rate.toFixed(2);
// Display result
resultDiv.style.display = "block";
resultDiv.innerHTML = 'Your Conversion Rate is: ' + roundedRate + '%';
}
Understanding How to Work Out Conversion Rate
Knowing how to work out your conversion rate is fundamental to measuring the success of any online campaign, landing page, or website. It is a key performance indicator (KPI) that tells you what percentage of your traffic is completing a desired action.
What is a Conversion?
A "conversion" isn't just a sale. It can be any predefined action that brings value to your business. Common examples include:
Completing a purchase (eCommerce).
Filling out a contact form (Lead Generation).
Signing up for a newsletter.
Downloading a whitepaper or software trial.
The Conversion Rate Formula
The math behind calculating your conversion rate is straightforward. You simply take the total number of conversions and divide it by the total number of visitors (or sessions) during the same time period. To get a percentage, you multiply the result by 100.
Conversion Rate = (Total Conversions / Total Visitors) x 100
Why Work Out Your Conversion Rate?
Without knowing this metric, you are flying blind. Working out your conversion rate allows you to:
Evaluate Traffic Quality: High traffic with low conversions might mean you are attracting the wrong audience.
Measure ROI: Understand how effectively your marketing spend turns into tangible results.
Run A/B Tests: Determine if changes to your website design or copy actually improve performance.
Realistic Example Calculation
Let's say you run a monthly report for your landing page. Your analytics software shows that you had 15,500 sessions (visitors) last month. Your CRM shows that from that page, you generated 420 qualified leads (conversions).
To work out the conversion rate:
Divide conversions by visitors: 420 / 15,500 = 0.02709…
Multiply by 100 to get percentage: 0.02709 * 100 = 2.709…
Round to two decimal places: 2.71%
In this scenario, your landing page has a 2.71% conversion rate.