Key metrics (impressions, clicks, CTR, conversions, CPA, ROAS) in Digital Marketing - Time & Space Complexity
When analyzing key digital marketing metrics, it's important to understand how the amount of data affects the time it takes to calculate these numbers.
We want to know how the effort to compute metrics like impressions, clicks, and conversions grows as the data size increases.
Analyze the time complexity of the following code snippet.
// Assume data is a list of ad events
let impressions = 0;
let clicks = 0;
let conversions = 0;
for (let event of data) {
if (event.type === 'impression') impressions++;
else if (event.type === 'click') clicks++;
else if (event.type === 'conversion') conversions++;
}
let ctr = clicks / impressions;
let cpa = totalCost / conversions;
let roas = totalRevenue / totalCost;
This code counts different event types from a list and then calculates key metrics like CTR, CPA, and ROAS.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop through all events in the data list.
- How many times: Once for each event, so as many times as there are events.
As the number of events grows, the time to count impressions, clicks, and conversions grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and counts |
| 100 | 100 checks and counts |
| 1000 | 1000 checks and counts |
Pattern observation: The work grows directly with the number of events; doubling events doubles the work.
Time Complexity: O(n)
This means the time to calculate these metrics grows in a straight line with the number of events.
[X] Wrong: "Calculating metrics like CTR or CPA takes the same time no matter how many events there are."
[OK] Correct: The code must look at each event to count it, so more events mean more work and more time.
Understanding how metric calculations scale with data size helps you explain performance in real marketing tools and dashboards.
What if we stored counts in a database and updated them as events come in instead of scanning all events each time? How would the time complexity change?