Skip to content

Premium Analytics: add CSV downloads to dashboard widgets#50456

Draft
layoutd wants to merge 2 commits into
trunkfrom
add/direct-report-download
Draft

Premium Analytics: add CSV downloads to dashboard widgets#50456
layoutd wants to merge 2 commits into
trunkfrom
add/direct-report-download

Conversation

@layoutd

@layoutd layoutd commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Related to WOOA7S-1671.

Proposed changes

  • Extend the existing DownloadCsvButton with two modes behind the current CSV export proof-of-concept flag.
  • For reports supported by the ported WooCommerce Analytics exporter, request delivery_method: download through the existing CSV export endpoint. The proof of concept wires this mode into Orders Over Time.
  • For reports without server-side export support, serialize the rows already loaded in the browser. Top pages by views continues to use this client-side mode from Premium Analytics: client-side CSV export for the Top pages by views widget (POC) #50344.
  • Keep one consistent button and limit this proof of concept to those two widgets.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No. It downloads existing report data for the authorized user and does not add tracking or collect new data.

Testing instructions

  • Build Premium Analytics with pnpm jetpack build packages/premium-analytics --deps.
  • Enable the proof of concept locally:
add_filter( 'jetpack_premium_analytics_client_side_csv_exports_enabled', '__return_true' );
  • Load the Analytics dashboard on a connected site with WooCommerce report data.
  • Confirm Orders Over Time shows Download CSV. Click it and confirm the downloaded CSV contains the complete Orders Over Time report for the selected period.
  • Confirm Top pages by views shows the same Download CSV action. Click it and confirm the CSV contains the rows currently loaded by the widget.
  • Change the dashboard date range and confirm both downloads use the selected period.
  • Disable the proof-of-concept filter and confirm both actions are hidden.
  • From projects/packages/premium-analytics, run pnpm test --runInBand and pnpm typecheck.

@layoutd layoutd self-assigned this Jul 13, 2026
@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 13, 2026
@layoutd layoutd requested a review from Copilot July 13, 2026 16:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a synchronous “direct download” CSV export path to Premium Analytics so users can download a freshly generated CSV for a single report from the dashboard (without waiting for scheduled exports), supporting both WooCommerce Analytics reports and Jetpack Stats Top Posts.

Changes:

  • Add a new REST route for direct CSV downloads, plus bootstrapping to register Analytics + Stats sources and enforce source-scoped capabilities.
  • Extend the export pipeline to support source-specific proxy routing (prefix/version), per-report param mapping, optional comparison filtering behavior, and optional post-fetch data filtering.
  • Add a reusable DownloadReportButton and wire it into the Orders Over Time and Top Posts widgets (Top Posts button appears in the content export area only when data is settled).

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
projects/packages/premium-analytics/widgets/top-posts/style.module.css Adds layout styles for a content export action area.
projects/packages/premium-analytics/widgets/top-posts/render.tsx Injects a settled-data-gated DownloadReportButton into the widget content export area.
projects/packages/premium-analytics/widgets/top-posts/tests/top-posts.test.tsx Adds UI tests for download button placement and loading/refetch gating.
projects/packages/premium-analytics/widgets/orders-over-time/render.tsx Adds DownloadReportButton to Orders Over Time widget.
projects/packages/premium-analytics/tests/php/Reports/Export/ReportDataFetcher_Test.php Adds coverage for controller param mapping, ranked comparison matching, and endpoint selection logic.
projects/packages/premium-analytics/tests/php/Reports/Export/fixtures/class-recording-report-data-fetcher.php Test double to record proxy endpoint/params for assertions.
projects/packages/premium-analytics/tests/php/Reports/Export/fixtures/class-fake-generator.php Extends generator test double to simulate streamability checks.
projects/packages/premium-analytics/tests/php/Reports/Export/fixtures/class-fake-fetcher.php Records passed params for controller tests.
projects/packages/premium-analytics/tests/php/Reports/Export/Exports/TopPostsExportController_Test.php New unit tests for the Stats Top Posts export controller.
projects/packages/premium-analytics/tests/php/Reports/Export/DirectCSVExportController_Test.php New unit tests for the direct-download REST controller.
projects/packages/premium-analytics/src/Reports/Export/Logging/class-error-log-logger.php Adds a WooCommerce-free logger implementation for the export pipeline.
projects/packages/premium-analytics/src/Reports/Export/Exports/class-top-posts-export-controller.php Adds a Stats Top Posts export controller (param mapping, matching, filtering, CSV formatting).
projects/packages/premium-analytics/src/Reports/Export/Exports/class-orders-over-time-controller.php Adds direct-export option allowlist and conditional endpoint selection for product-related filters.
projects/packages/premium-analytics/src/Reports/Export/class-report-data-fetcher.php Adds source-configurable proxy routing, optional controller hooks (param mapping/filtering), and Stats summarydata normalization.
projects/packages/premium-analytics/src/Reports/Export/class-report-csv-generator.php Adds can_stream_file() preflight check for streaming responses.
projects/packages/premium-analytics/src/Reports/Export/class-export.php Centralizes Analytics controller allowlist via get_report_controllers().
projects/packages/premium-analytics/src/Reports/Export/class-direct-export.php Bootstraps direct export for Analytics + Stats sources and registers controllers.
projects/packages/premium-analytics/src/Reports/Export/class-direct-csv-export-controller.php Adds REST endpoint for synchronous CSV downloads, including validation and streaming response handling.
projects/packages/premium-analytics/src/class-analytics.php Wires direct export bootstrap into package initialization.
projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts Exports DownloadReportButton from the toolkit package.
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/index.ts Re-exports DownloadReportButton from the components barrel.
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/download-report-button/index.ts Adds component entrypoint export.
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/download-report-button/download-report-button.tsx Implements reusable Download CSV button that calls the direct export API and reports errors to WidgetRoot.
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/download-report-button/download-report-button.module.scss Styles default action-row wrapper and button sizing.
projects/packages/premium-analytics/packages/data/src/index.ts Exposes new direct-download API helpers and types.
projects/packages/premium-analytics/packages/data/src/api/report-export-fetch/report-export-fetch.ts Adds direct download fetch path, content-disposition filename parsing, and blob saving.
projects/packages/premium-analytics/packages/data/src/api/report-export-fetch/index.ts Updates exports/types for the new direct-download functions.
projects/packages/premium-analytics/packages/data/src/api/index.ts Re-exports the expanded report export API surface.
projects/packages/premium-analytics/packages/data/src/api/tests/report-export-fetch.test.ts Adds unit tests for direct-download request bodies, filename parsing, and fallback filenames.
projects/packages/premium-analytics/changelog/add-pa-direct-report-download Adds changelog entry for direct CSV download support.

@jp-launch-control

jp-launch-control Bot commented Jul 13, 2026

Copy link
Copy Markdown

Code Coverage Summary

This PR did not change code coverage!

That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷

Full summary · PHP report

@layoutd layoutd force-pushed the add/direct-report-download branch from 4ce891f to be08017 Compare July 14, 2026 14:48
@layoutd layoutd changed the title Premium Analytics: Add direct CSV report downloads Premium Analytics: add CSV downloads to dashboard widgets Jul 14, 2026
@layoutd

layoutd commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Reworked this draft for simplicity. It now uses the existing CSV export endpoint for supported WooCommerce Analytics reports and the already-merged client-side generator otherwise, scoped to Orders Over Time and Top pages by views.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Comment on lines +153 to +156
const utf8Match = header.match( /filename\*=UTF-8''([^;]+)/i );
if ( utf8Match?.[ 1 ] ) {
return decodeURIComponent( utf8Match[ 1 ].replace( /(^"|"$)/g, '' ) );
}
Comment on lines +171 to +174

link.href = url;
link.download = filename;
link.style.display = 'none';
}

/**
* Request a browser download from the existing WooCommerce Analytics CSV endpoint.
Comment on lines 317 to +321
// Only expose the export once the query has settled on data for the current
// params. Stats queries keep the previous period's rows as placeholder data
// while a refetch is in flight, so exporting mid-fetch (or after an error)
// could hand the user stale rows under the new-period `csvFilename`.
const canExport =
areClientSideCsvExportsEnabled() && rows.length > 0 && ! isFetching && ! isError;
const canExport = rows.length > 0 && ! isFetching && ! isError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Premium Analytics [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants