Skip to content

feat: snap analytics query dates to month/week boundaries#44

Merged
jdwit merged 2 commits into
mainfrom
feat/analytics-date-alignment
Jun 18, 2026
Merged

feat: snap analytics query dates to month/week boundaries#44
jdwit merged 2 commits into
mainfrom
feat/analytics-date-alignment

Conversation

@jdwit

@jdwit jdwit commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Problem

The YouTube Analytics API requires aligned start/end dates for the month dimension: both ends must be the first day of a month, and the API rejects any other day rather than snapping. The query command built dates from --days/-s/-e with no dimension-aware alignment, so -d month --days 365 or any non-first-of-month bound produced an HTTP 400.

Fix

Add _align_date_range(dimension_names, start, end) in commands/analytics.py and call it after the query dates are built:

  • month: snap both ends DOWN to the first of their month (inclusive range; degenerate same-month range stays valid).
  • week: snap both ends back to the preceding Sunday. YouTube Analytics weeks begin on Sunday. Note: the current modern Analytics API exposes only day and month as time-period dimensions (per https://developers.google.com/youtube/analytics/dimensions), so week is handled defensively for forward-compat.
  • day and all other dimensions are returned unchanged.

Tests

New TestAlignDateRange unit tests cover: month start snap, month end snap, --days-derived non-aligned dates, already-aligned unchanged, same-month range, day untouched, no-dimension untouched, week snap to Sunday, and Sunday unchanged. Full suite: 292 passed; ruff clean.

Closes #39

The YouTube Analytics API rejects unaligned start/end dates for the
month dimension (both ends must be the first of a month) and does not
snap on its own, so -d month --days N or any mid-month bound returned
HTTP 400. Add _align_date_range to snap query dates to the required
boundaries before the request: month snaps both ends down to the first
of their month; week snaps both ends back to the preceding Sunday.
day and other dimensions are unchanged.
@codecov-commenter

codecov-commenter commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.90%. Comparing base (8fa621a) to head (aa11bf1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #44      +/-   ##
==========================================
+ Coverage   83.81%   83.90%   +0.08%     
==========================================
  Files          15       15              
  Lines        2447     2460      +13     
  Branches      354      356       +2     
==========================================
+ Hits         2051     2064      +13     
  Misses        264      264              
  Partials      132      132              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jdwit jdwit merged commit 941f1b6 into main Jun 18, 2026
8 checks passed
@jdwit jdwit deleted the feat/analytics-date-alignment branch June 18, 2026 08:52
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.

analytics query: month/week dimensions reject unaligned date ranges

2 participants