Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Year over Year" comparison mode #2704

Merged
merged 16 commits into from
Mar 7, 2023
Merged

Implement "Year over Year" comparison mode #2704

merged 16 commits into from
Mar 7, 2023

Conversation

vinibrsl
Copy link
Contributor

Changes

This pull request adds support for multiple comparison modes, changes the comparison checkbox to a combobox, and implements the year over year / last year comparison mode. The feature is still behind a feature flag.

Screenshot 2023-02-23 at 12 56 07

Tests

  • Automated tests have been added

Changelog

  • This PR does not make a user-facing change

Documentation

  • This change does not need a documentation update

Dark mode

  • The UI has been tested both in dark and light mode

@vinibrsl vinibrsl requested a review from a team February 23, 2023 16:03
@bundlemon
Copy link

bundlemon bot commented Feb 23, 2023

BundleMon

Files updated (1)
Status Path Size Limits
static/js/dashboard.js
298.2KB (+195B +0.06%) -
Unchanged files (6)
Status Path Size Limits
static/css/app.css
492.34KB -
static/js/app.js
12.13KB -
static/js/embed.host.js
5.58KB -
static/js/embed.content.js
5.06KB -
tracker/js/plausible.js
733B -
static/js/applyTheme.js
314B -

Total files change +195B +0.02%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history


const COMPARISON_MODES = {
'previous_period': 'Previous period',
'last_year': 'Last year',
Copy link
Contributor Author

@vinibrsl vinibrsl Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love some feedback on what we should call this comparison mode. I named it "Last year", but it's pretty common to call this "Year over year" too. Essentially it compares the current stats with last year stats, e.g. Jan 2021 compared to Jan 2020.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! "Previous period" and "Year over year" are good i think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer 'year over year' vs 'last year'

let comparison = !!q.get('comparison')
let comparison = q.get('comparison')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the comparison query property was a boolean, on or off. Now it supports multiple modes and it's a string, e.g. last_year, previous_period.

@vinibrsl vinibrsl force-pushed the year-over-year branch 3 times, most recently from 808b4af to 16801c0 Compare February 23, 2023 16:31

const COMPARISON_MODES = {
'previous_period': 'Previous period',
'last_year': 'Last year',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer 'year over year' vs 'last year'

comparison_query = Query.shift_back(query, site)
Stats.timeseries(site, comparison_query, [selected_metric])

"last_year" ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this implementation is missing the incomplete period logic that we have in Query.shift_back.

The idea is that comparisons should compare the same number of months/weeks/days/hours. For example, if you're in the middle of the month and looking at 'this month', then we display the full month even though there's only data for the first 14 days. In this case we want the comparison to also only include 14 days from the comparison period.

We've started to add clauses to shift_back to account for this discrepancy. I think some are still missing, and that's what this PR is about.

I think similar special cases need to be added for the year over year comparison mode. When 'this month' only includes 14 days, we need to shift back one year but also make sure we only select 14 days to compare with.

Does that make sense? Happy to hop on a quick call to dive deeper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! If I understand that correctly, we should not show comparisons for any future date in the graph, is that it? Even if comparison has data to show.

Screenshot 2023-03-01 at 12 22 40

We could reimplement what we have for shift_back to match the comparison logic. Or we could truncate the comparison plot to match the same number of items of the main plot. What do you think?

I'm up for pairing on this. We can talk more about it tomorrow in our call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, yes. In your screenshot 'month to date' view shows 30 days and compares with 30 days from the previous year. However, since there has only been 1 day this March it's not a fair comparison. The comparison period should also be just 1 day from the previous March.

This applies for any time period that can show an incomplete period: today, this month and year to date come to mind. We already handle period: month and period: year correctly. We don't currently handle today correctly which is addressed in this PR.

We could reimplement what we have for shift_back to match the comparison logic. Or we could truncate the comparison plot to match the same number of items of the main plot. What do you think?

I think it needs to be in shift_back. If we only truncate the comparison plot then the percentages in top_stats will still be off.

start_date = Date.add(source_query.date_range.first, -365)
end_date = Date.add(source_query.date_range.last, -365)
range = Date.range(start_date, end_date)
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elegant 👌

@vinibrsl vinibrsl changed the title Implement "Last Year" comparison mode Implement "Year over Year" comparison mode Mar 7, 2023
@vinibrsl vinibrsl requested a review from ukutaht March 7, 2023 14:11
case Stats.Comparisons.compare(site, query, comparison_mode) do
{:ok, prev_query} ->
%{visitors: %{value: prev_converted_visitors}, events: %{value: prev_completions}} =
Stats.aggregate(site, prev_query, [:visitors, :events])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a concern for this PR but I think eventually we want to also support the different comparison modes in the API. In that case it would make sense to push the comparison logic down to Stats.Aggregate and Stats.Timeseries so the logic can be shared between the dashboard and the API.

@vinibrsl vinibrsl merged commit 8b0f0ca into master Mar 7, 2023
@vinibrsl vinibrsl deleted the year-over-year branch March 7, 2023 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants