Skip to content

Improved analytics CSV exports to include the site name#28308

Merged
aileen merged 2 commits into
mainfrom
aileen/onc-1782-analytics-csv-site-name
Jun 2, 2026
Merged

Improved analytics CSV exports to include the site name#28308
aileen merged 2 commits into
mainfrom
aileen/onc-1782-analytics-csv-site-name

Conversation

@aileen
Copy link
Copy Markdown
Member

@aileen aileen commented Jun 2, 2026

closes ONC-1782
Closes #28227

Why

Post analytics CSV exports were downloaded with a generic post-analytics.YYYY-MM-DD.csv filename. That made exports from multiple Ghost sites harder to identify and was inconsistent with content exports, which already include the site name in the filename.

How

This updates the Admin settings export flow to build the analytics CSV filename from the current site title, using Ghost's existing @tryghost/string.slugify helper so the title formatting matches Ghost's established safe filename behavior. The direct Admin API /posts/export/ response now uses the same site-name.ghost.analytics.YYYY-MM-DD.csv filename in Content-Disposition, so browser-triggered and direct downloads stay consistent.

Impact

Users managing exports across multiple publications can tell which site an analytics CSV came from without manually renaming files, and repeated exports are less likely to collide in downloads folders.

Testing

  • Added unit coverage for analytics export filename generation with and without a site title.
  • Updated core serializer and E2E header expectations for the new filename format.
  • Pre-commit lint-staged checks passed for the staged JS/TS/TSX files.
  • git diff --check passed before committing.
  • Focused test execution in this local shell was blocked by native module code-signature errors for Rollup/sqlite3.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 2, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 43ef25ec-d324-47f3-8a7d-e0a0430b7038

📥 Commits

Reviewing files that changed from the base of the PR and between 11d0fe3 and 2ab7fe9.

⛔ Files ignored due to path filters (3)
  • ghost/core/test/e2e-api/admin/__snapshots__/post-analytics-export.test.js.snap is excluded by !**/*.snap
  • ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap is excluded by !**/*.snap
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (10)
  • apps/admin-x-settings/package.json
  • apps/admin-x-settings/src/components/settings/advanced/migration-tools/migration-tools-export.tsx
  • apps/admin-x-settings/src/typings.d.ts
  • apps/admin-x-settings/test/unit/utils/post-analytics-export-filename.test.ts
  • apps/admin/src/vite-env.d.ts
  • ghost/core/core/server/api/endpoints/posts.js
  • ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js
  • ghost/core/test/e2e-api/admin/post-analytics-export.test.js
  • ghost/core/test/e2e-api/admin/posts.test.js
  • ghost/core/test/unit/api/canary/utils/serializers/output/posts-export-csv.test.ts
🚧 Files skipped from review as they are similar to previous changes (10)
  • apps/admin/src/vite-env.d.ts
  • apps/admin-x-settings/package.json
  • apps/admin-x-settings/test/unit/utils/post-analytics-export-filename.test.ts
  • apps/admin-x-settings/src/typings.d.ts
  • ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js
  • ghost/core/test/e2e-api/admin/posts.test.js
  • apps/admin-x-settings/src/components/settings/advanced/migration-tools/migration-tools-export.tsx
  • ghost/core/test/e2e-api/admin/post-analytics-export.test.js
  • ghost/core/core/server/api/endpoints/posts.js
  • ghost/core/test/unit/api/canary/utils/serializers/output/posts-export-csv.test.ts

Walkthrough

Adds site-title-aware filenames for analytics CSV exports. Introduces the @tryghost/string dependency and TypeScript declarations, implements getPostAnalyticsExportFileName in admin UI and Core posts endpoint (using settingsCache + slugify + date), updates the posts export controller to return filename with data, changes the CSV serializer to use models.filename (erroring when missing), and updates unit and e2e tests to expect .ghost.analytics..csv.

Possibly related PRs

  • TryGhost/Ghost#27998: Both PRs modify the “Post analytics” export UI/flow in migration-tools-export.tsx (one changes the generated CSV download filename via a helper, the other adds a loading/disabled state that gates concurrent exports).
  • TryGhost/Ghost#27752: Both PRs modify the posts CSV export path—exportCSV/CSV serializers in ghost/core/.../endpoints/posts.js and ghost/core/.../serializers/output/posts.js—with overlapping export logic changes.

Suggested reviewers

  • EvanHahn
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: improving analytics CSV exports to include the site name in the filename.
Description check ✅ Passed The description is well-detailed and directly related to the changeset, explaining the problem, solution, and testing approach comprehensively.
Linked Issues check ✅ Passed The code changes fully satisfy issue #28227 requirements by implementing site-name inclusion in analytics CSV filenames using slugified site titles in the pattern .ghost.analytics.YYYY-MM-DD.csv across all export flows.
Out of Scope Changes check ✅ Passed All code changes are directly scoped to implementing the analytics CSV filename improvement: dependency addition, filename generation logic, TypeScript declarations, and corresponding test updates.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch aileen/onc-1782-analytics-csv-site-name

Comment @coderabbitai help to get the list of available commands and usage tips.

@aileen aileen changed the title Fixed analytics CSV exports to include the site name Improved analytics CSV exports to include the site name Jun 2, 2026
@aileen aileen force-pushed the aileen/onc-1782-analytics-csv-site-name branch from c5f6ef1 to 36ef301 Compare June 2, 2026 06:30
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js`:
- Line 62: The Content-Disposition header is being set using models.filename
without guarding for missing values; update the code around the
res.setHeader('Content-Disposition', ...) call to check models.filename (or the
variable name used in this scope) and only include filename when it is a
non-empty string, otherwise use a safe default filename (e.g., 'export.csv') or
omit the filename parameter; ensure you reference the existing models.filename
symbol and the res.setHeader call so the header is never set to
filename="undefined".

In `@ghost/core/test/e2e-api/admin/post-analytics-export.test.js`:
- Around line 30-33: The current matcher in matchExportHeaders over-constrains
the filename by requiring a slug prefix; update the stringMatching regex used
for 'content-disposition' so the slug+dot is optional (e.g. allow either
"ghost.analytics.YYYY-MM-DD.csv" or "slug.ghost.analytics.YYYY-MM-DD.csv"). Edit
the regex in the matchExportHeaders constant (the stringMatching(...) entry) to
make the initial [a-z0-9-]+\. portion optional (while keeping the rest of the
pattern and anchors intact) so tests cover the empty-site-title fallback.

In `@ghost/core/test/e2e-api/admin/posts.test.js`:
- Around line 144-145: The test's 'content-disposition' assertion is too strict
and requires a slug prefix though getPostAnalyticsExportFileName() can return
the fallback "ghost.analytics.YYYY-MM-DD.csv" when site title is falsy; update
the matcher used in the tests (the 'content-disposition' stringMatching
assertions) to accept either the slug-prefixed form or the fallback by using a
regex that makes the "<slug>." prefix optional (so both
"<slug>.ghost.analytics.YYYY-MM-DD.csv" and "ghost.analytics.YYYY-MM-DD.csv"
match); apply this change to the occurrences referenced (the current assertion
and the ones at the other noted locations).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e5ef2cf2-5832-46c3-8edc-66bc4cc4fae3

📥 Commits

Reviewing files that changed from the base of the PR and between c99c164 and c5f6ef1.

⛔ Files ignored due to path filters (3)
  • ghost/core/test/e2e-api/admin/__snapshots__/post-analytics-export.test.js.snap is excluded by !**/*.snap
  • ghost/core/test/e2e-api/admin/__snapshots__/posts.test.js.snap is excluded by !**/*.snap
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • apps/admin-x-settings/package.json
  • apps/admin-x-settings/src/components/settings/advanced/migration-tools/migration-tools-export.tsx
  • apps/admin-x-settings/src/typings.d.ts
  • apps/admin-x-settings/test/unit/utils/post-analytics-export-filename.test.ts
  • ghost/core/core/server/api/endpoints/posts.js
  • ghost/core/core/server/api/endpoints/utils/serializers/output/posts.js
  • ghost/core/test/e2e-api/admin/post-analytics-export.test.js
  • ghost/core/test/e2e-api/admin/posts.test.js
  • ghost/core/test/unit/api/canary/utils/serializers/output/posts-export-csv.test.ts

Comment thread ghost/core/test/e2e-api/admin/post-analytics-export.test.js
Comment thread ghost/core/test/e2e-api/admin/posts.test.js Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 2, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.62%. Comparing base (e802e82) to head (2ab7fe9).

Files with missing lines Patch % Lines
...er/api/endpoints/utils/serializers/output/posts.js 37.50% 5 Missing ⚠️
ghost/core/core/server/api/endpoints/posts.js 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #28308      +/-   ##
==========================================
- Coverage   73.62%   73.62%   -0.01%     
==========================================
  Files        1536     1536              
  Lines      130847   130863      +16     
  Branches    15651    15654       +3     
==========================================
+ Hits        96341    96350       +9     
- Misses      33519    33524       +5     
- Partials      987      989       +2     
Flag Coverage Δ
admin-tests 54.18% <ø> (-0.02%) ⬇️
e2e-tests 73.62% <66.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

aileen added 2 commits June 2, 2026 11:48
closes [ONC-1782](https://linear.app/ghost/issue/ONC-1782/oss-issue-analytics-csv-export-should-include-site-name-in-filename)

Analytics downloads used a generic filename, which made exports from multiple sites hard to identify and inconsistent with content exports.
Guarded the CSV serializer against missing filenames, relaxed export header matchers to cover the no-title fallback, and exposed the existing `@tryghost/string` module type to the admin build that imports admin-x-settings source.
@aileen aileen force-pushed the aileen/onc-1782-analytics-csv-site-name branch from 11d0fe3 to 2ab7fe9 Compare June 2, 2026 07:48
@aileen aileen requested a review from sagzy June 2, 2026 08:28
@aileen aileen merged commit 8ba93e3 into main Jun 2, 2026
55 checks passed
@aileen aileen deleted the aileen/onc-1782-analytics-csv-site-name branch June 2, 2026 09:21
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 CSV export should include site name in filename (consistent with JSON export)

2 participants