Improving Analytics Comparisons for Leap Years

It’s something many of us may not think about every day, but when leap year pops up every so often, the extra day it brings with it can present plenty of challenges and quandaries for us, especially those of us who work in and around computer science.

That was the case recently when the unit tests in WooCommerce’s date package revealed that the getLastPeriod function was not taking leap year into account when returning values. This function helps power a number of important features and components in WooCommerce Admin, such as data stores, report filters, and charts. The fix for the underlying function included logic that would account for leap days, and in doing so, it presented an important question: how should WooCommerce Admin represent those extra days when charting comparisons?

There are a few different ways to approach this problem, which, while it may seem like an edge case, is something that happens regularly and affects two out of every four years in the context of charting comparisons. There were some initial ideas in the Pull Request proposing fixes to the Analytics charts, and a few additional approaches materialized as the team who maintains WooCommerce Admin discussed options internally.

One option would be to simply omit leap days from comparisons. This solution seems nice and neat on the surface, but it ultimately results in charts that contain incomplete data. An equally convenient way of dealing with the extra data is to simply combine it with the previous day’s data. This can be problematic, however, because it artificially inflates data for certain days and obfuscates what is actually happening in a store. Another possible approach is to include leap day data in comparison charts but to represent those same days in common years with zeroed out values and a note of some kind indicating that the year did not include that particular day.

This third approach is what the WooCommerce Admin team eventually chose as a solution. The work is being broken down into two separate issues to be tackled separately. The primary issue is for a fix that implements the zeroed out days on the x-axis for common years in charted comparisons that include leap days. This fix will ensure that charts include data that is complete and accurate. There’s a supplemental enhancement issue that proposes adding the note explaining the zeroed out days in comparisons where they appear. Seeing charts that contain nonexistent days along the axes could be confusing at first glance, so this note should provide some helpful context around those dubious looking dates.

We’re eager to hear your input on the chosen approach. Is this a challenge you’ve navigated in your own extension? How did you handle it? You can follow along with the work and join the conversation by hopping into the issues linked above, dropping us a line in the comments below, or by joining us for Developer Office Hours in the WooCommerce Community Slack.

Leave a Reply

Your email address will not be published. Required fields are marked *