gistmax. Back to DiscoverStudio
Technology

Calculate conversion rates without dividing by zero

By Gistmax Tutorials · 2026-11-04

Data analytics: Calculate conversion rates without dividing by zero. Workflow: identify, diagnose, fix, verify.
Data analytics: Calculate conversion rates without dividing by zero. Workflow: identify, diagnose, fix, verify.

A campaign with no visits produces an exception, infinity or a misleading zero-percent conversion rate. Another campaign appears to exceed 100 percent.

Why this happens

A zero denominator does not support a rate estimate. A rate above 100 percent may indicate that the numerator counts events while the denominator counts unique sessions, or that the periods differ.

Step-by-step solution

1. Define the numerator and denominator in the same unit and time window. For a session conversion rate, count sessions with at least one conversion rather than every repeated event.

2. Return a missing value when the denominator is zero, and display Not enough data. Distinguish that from zero conversions among real visits.

3. Reconcile the input counts before rounding the final percentage. Investigate impossible values rather than clipping them to 100.

Worked example

def conversion_rate(converted_sessions, sessions):
    if not 0 <= converted_sessions <= sessions:
        raise ValueError('Counts must describe the same session population')
    return None if sessions == 0 else 100 * converted_sessions / sessions
assert conversion_rate(0, 0) is None
assert conversion_rate(8, 200) == 4

Verify the fix

Zero visits should show no estimate, while eight converted sessions out of 200 should show 4 percent. Check that weekly totals use summed counts, not an unweighted average of daily percentages.

Prevent a repeat

Name metrics with their unit, such as converted_sessions, and retain the denominator in exports so users can assess small samples.

Reference

Official documentation or standards guidance

An original Gistmax troubleshooting guide. Scenarios and sample values are illustrative; configuration depends on your software version and environment. Cover diagram: Gistmax Tutorials.

Join the conversation →