Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/admin-x-settings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@tanstack/react-query": "catalog:",
"@tryghost/color-utils": "catalog:",
"@tryghost/i18n": "workspace:*",
"@tryghost/string": "catalog:",
"@tryghost/kg-unsplash-selector": "0.4.1",
"@tryghost/limit-service": "catalog:",
"@tryghost/nql": "catalog:",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import React from 'react';
import {Button} from '@tryghost/admin-x-design-system';
import {downloadAllContent} from '@tryghost/admin-x-framework/api/db';
import {getSettingValues} from '@tryghost/admin-x-framework/api/settings';
import {slugify} from '@tryghost/string';
import {useGlobalData} from '../../../providers/global-data-provider';
import {useHandleError} from '@tryghost/admin-x-framework/hooks';
import {usePostsExports} from '@tryghost/admin-x-framework/api/posts';

export const getPostAnalyticsExportFileName = (siteTitle?: string | null) => {
const titlePrefix = siteTitle ? `${slugify(siteTitle)}.` : '';
const today = new Date().toISOString().split('T')[0];

return `${titlePrefix}ghost.analytics.${today}.csv`;
};

const MigrationToolsExport: React.FC = () => {
const [isExportingPosts, setIsExportingPosts] = React.useState(false);
const {settings} = useGlobalData();
const [siteTitle] = getSettingValues<string>(settings, ['title']);
const {refetch: postsData} = usePostsExports({
searchParams: {
limit: '1000'
Expand All @@ -29,7 +41,7 @@ const MigrationToolsExport: React.FC = () => {
const a = document.createElement('a');
a.setAttribute('hidden', '');
a.setAttribute('href', url);
a.setAttribute('download', `post-analytics.${new Date().toISOString().split('T')[0]}.csv`);
a.setAttribute('download', getPostAnalyticsExportFileName(siteTitle));
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Expand Down
3 changes: 3 additions & 0 deletions apps/admin-x-settings/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
declare module '@tryghost/limit-service'
declare module '@tryghost/nql'
declare module '@tryghost/string' {
export function slugify(string: string, options?: {requiredChangesOnly?: boolean}): string;
}

declare module '*.svg' {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {afterEach, describe, expect, it, vi} from 'vitest';
import {getPostAnalyticsExportFileName} from '@src/components/settings/advanced/migration-tools/migration-tools-export';

describe('getPostAnalyticsExportFileName', function () {
afterEach(function () {
vi.useRealTimers();
});

it('includes the slugified site title', function () {
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-06-02T12:00:00.000Z'));

expect(getPostAnalyticsExportFileName('My Publication')).toBe('my-publication.ghost.analytics.2026-06-02.csv');
});

it('falls back to ghost when the site title is missing', function () {
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-06-02T12:00:00.000Z'));

expect(getPostAnalyticsExportFileName(null)).toBe('ghost.analytics.2026-06-02.csv');
});
});
3 changes: 3 additions & 0 deletions apps/admin/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

declare module '@tryghost/limit-service'
declare module '@tryghost/nql'
declare module '@tryghost/string' {
export function slugify(string: string, options?: {requiredChangesOnly?: boolean}): string;
}
declare module '@tryghost/koenig-lexical'
16 changes: 13 additions & 3 deletions ghost/core/core/server/api/endpoints/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const urlUtils = require('../../../shared/url-utils');
const models = require('../../models');
const settingsCache = require('../../../shared/settings-cache');
const {slugify} = require('@tryghost/string');
const getPostServiceInstance = require('../../services/posts/posts-service-instance');
const allowedIncludes = [
'tags',
Expand All @@ -22,6 +24,14 @@ const unsafeAttrs = ['status', 'authors', 'visibility'];

const postsService = getPostServiceInstance();

function getPostAnalyticsExportFileName() {
const datetime = (new Date()).toJSON().substring(0, 10);
const title = settingsCache.get('title');
const titlePrefix = title ? `${slugify(title)}.` : '';

return `${titlePrefix}ghost.analytics.${datetime}.csv`;
}

/**
* @param {string} event
*/
Expand Down Expand Up @@ -91,8 +101,7 @@ const controller = {
disposition: {
type: 'csv',
value() {
const datetime = (new Date()).toJSON().substring(0, 10);
return `post-analytics.${datetime}.csv`;
return getPostAnalyticsExportFileName();
}
},
cacheInvalidate: false
Expand All @@ -107,7 +116,8 @@ const controller = {
validation: {},
async query(frame) {
return {
data: await postsService.export(frame)
data: await postsService.export(frame),
filename: getPostAnalyticsExportFileName()
};
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mappers = require('./mappers');
const tiersService = require('../../../../../services/tiers');
const {pipeline} = require('stream');
const {createCSVTransform} = require('./posts-csv-transform');
const {InternalServerError} = require('@tryghost/errors');

module.exports = {
async all(models, apiConfig, frame) {
Expand Down Expand Up @@ -56,11 +57,16 @@ module.exports = {

exportCSV(models, apiConfig, frame) {
frame.response = function streamResponse(req, res, next) {
if (!models.filename) {
return next(new InternalServerError({
message: 'Missing CSV export filename'
}));
}

const csvTransform = createCSVTransform();

const todayIsoDate = (new Date()).toJSON().substring(0, 10);
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
res.setHeader('Content-Disposition', `Attachment; filename="post-analytics.${todayIsoDate}.csv"`);
res.setHeader('Content-Disposition', `Attachment; filename="${models.filename}"`);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const cacheControl = res.getHeader('Cache-Control');
const cacheControlDirectives = cacheControl ? String(cacheControl).split(',').map(value => value.trim().toLowerCase()) : [];
if (!cacheControlDirectives.includes('no-transform')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Post Analytics Export Default settings (all features enabled) Exports C
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -37,7 +37,7 @@ exports[`Post Analytics Export Settings variations Hides clicks column when emai
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -70,7 +70,7 @@ exports[`Post Analytics Export Settings variations Hides email columns when memb
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -103,7 +103,7 @@ exports[`Post Analytics Export Settings variations Hides feedback columns when n
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -136,7 +136,7 @@ exports[`Post Analytics Export Settings variations Hides opens column when email
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -169,7 +169,7 @@ exports[`Post Analytics Export Settings variations Hides paid_conversions when p
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -202,7 +202,7 @@ exports[`Post Analytics Export Settings variations Hides signups and paid_conver
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ exports[`Posts API Export Can export 1: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down Expand Up @@ -1865,7 +1865,7 @@ exports[`Posts API Export Can export with filter 1: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand All @@ -1885,7 +1885,7 @@ exports[`Posts API Export Can export with limit 1: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand All @@ -1904,7 +1904,7 @@ exports[`Posts API Export Can export with order 1: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-transform",
"content-disposition": StringMatching /\\^Attachment; filename="post-analytics\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\.csv"\\$/,
"content-disposition": StringMatching /\\^Attachment; filename="\\(\\?:\\[a-z0-9-\\]\\+\\\\\\.\\)\\?ghost\\\\\\.analytics\\\\\\.\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}\\\\\\.csv"\\$/,
"content-type": "text/csv; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"vary": "Accept-Version, Origin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const csvReplacements = [

const matchExportHeaders = {
'content-version': anyContentVersion,
'content-disposition': stringMatching(/^Attachment; filename="post-analytics.\d{4}-\d{2}-\d{2}.csv"$/)
'content-disposition': stringMatching(/^Attachment; filename="(?:[a-z0-9-]+\.)?ghost\.analytics\.\d{4}-\d{2}-\d{2}\.csv"$/)
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

describe('Post Analytics Export', function () {
Expand Down
8 changes: 4 additions & 4 deletions ghost/core/test/e2e-api/admin/posts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Posts API', function () {
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
'content-disposition': stringMatching(/^Attachment; filename="post-analytics.\d{4}-\d{2}-\d{2}.csv"$/)
'content-disposition': stringMatching(/^Attachment; filename="(?:[a-z0-9-]+\.)?ghost\.analytics\.\d{4}-\d{2}-\d{2}\.csv"$/)
});

// body snapshot doesn't work with text/csv
Expand All @@ -158,7 +158,7 @@ describe('Posts API', function () {
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
'content-disposition': stringMatching(/^Attachment; filename="post-analytics.\d{4}-\d{2}-\d{2}.csv"$/)
'content-disposition': stringMatching(/^Attachment; filename="(?:[a-z0-9-]+\.)?ghost\.analytics\.\d{4}-\d{2}-\d{2}\.csv"$/)
});

// body snapshot doesn't work with text/csv
Expand All @@ -175,7 +175,7 @@ describe('Posts API', function () {
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
'content-disposition': stringMatching(/^Attachment; filename="post-analytics.\d{4}-\d{2}-\d{2}.csv"$/)
'content-disposition': stringMatching(/^Attachment; filename="(?:[a-z0-9-]+\.)?ghost\.analytics\.\d{4}-\d{2}-\d{2}\.csv"$/)
});

// body snapshot doesn't work with text/csv
Expand All @@ -192,7 +192,7 @@ describe('Posts API', function () {
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
'content-disposition': stringMatching(/^Attachment; filename="post-analytics.\d{4}-\d{2}-\d{2}.csv"$/)
'content-disposition': stringMatching(/^Attachment; filename="(?:[a-z0-9-]+\.)?ghost\.analytics\.\d{4}-\d{2}-\d{2}\.csv"$/)
});

// body snapshot doesn't work with text/csv
Expand Down
Loading
Loading