From e71153dd629f2281752a8c02e709414656787ee5 Mon Sep 17 00:00:00 2001 From: Nikhil Date: Fri, 9 May 2025 17:43:18 +0530 Subject: [PATCH 1/2] Fix: match the Video stats CSV download to the production export --- .../stats/stats-download-csv/index.jsx | 6 +++- client/my-sites/stats/summary/index.jsx | 1 + client/state/stats/lists/utils.js | 35 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/client/my-sites/stats/stats-download-csv/index.jsx b/client/my-sites/stats/stats-download-csv/index.jsx index b3880f90b903..897086a5aae0 100644 --- a/client/my-sites/stats/stats-download-csv/index.jsx +++ b/client/my-sites/stats/stats-download-csv/index.jsx @@ -12,6 +12,7 @@ import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { getSiteSlug } from 'calypso/state/sites/selectors'; import { getSiteStatsCSVData, + getSiteStatsNormalizedData, isRequestingSiteStatsForQuery, } from 'calypso/state/stats/lists/selectors'; import { getSelectedSiteId } from 'calypso/state/ui/selectors'; @@ -125,7 +126,10 @@ const connectComponent = connect( } const { statType, query } = ownProps; - const data = getSiteStatsCSVData( state, siteId, statType, query ); + const data = + statType === 'statsVideoPlays' + ? getSiteStatsNormalizedData( state, siteId, statType, query ) + : getSiteStatsCSVData( state, siteId, statType, query ); const isLoading = isRequestingSiteStatsForQuery( state, siteId, statType, query ); return { data, siteSlug, siteId, isLoading }; diff --git a/client/my-sites/stats/summary/index.jsx b/client/my-sites/stats/summary/index.jsx index 0c0960cc4bba..7f83f8c4cb84 100644 --- a/client/my-sites/stats/summary/index.jsx +++ b/client/my-sites/stats/summary/index.jsx @@ -239,6 +239,7 @@ class StatsSummary extends Component { title = translate( 'Videos' ); path = 'videoplays'; statType = 'statsVideoPlays'; + moduleQuery.complete_stats = 1; summaryView = ( { /* For CSV button to work, video page needs to pass custom data to the button. diff --git a/client/state/stats/lists/utils.js b/client/state/stats/lists/utils.js index 9d91931772a2..a715550903c8 100644 --- a/client/state/stats/lists/utils.js +++ b/client/state/stats/lists/utils.js @@ -458,6 +458,35 @@ export const normalizers = { } ); }, + /** + * Returns a normalized statsVideoPlaysCompleteStats array, ready for use in stats-module + * @param {Object} data Stats data + * @param {Object} query Stats query + * @returns {Array} Parsed data array + */ + statsVideoPlaysCompleteStats: ( data, query = {} ) => { + if ( ! data || ! query.period || ! query.date ) { + return []; + } + + const videoPlaysData = get( data, [ 'days', 'summary', 'data' ], [] ); + const normalizedData = videoPlaysData.map( ( item ) => { + return { + post_id: item.post_id, + label: item.title, + views: item.views, + impressions: item.impressions, + watch_time: item.watch_time, + retention_rate: item.retention_rate, + }; + } ); + + return [ + [ 'post_id', 'title', 'views', 'impressions', 'watch_time', 'retention_rate' ], + ...normalizedData, + ]; + }, + /** * Returns a normalized statsVideoPlays array, ready for use in stats-module * @param {Object} data Stats data @@ -471,6 +500,12 @@ export const normalizers = { return []; } const { startOf } = rangeOfPeriod( query.period, query.date ); + const isCompleteStats = query.complete_stats; + + if ( isCompleteStats ) { + return normalizers.statsVideoPlaysCompleteStats( data, query, siteId, site ); + } + const videoPlaysData = get( data, query.summarize ? [ 'days', 'summary', 'plays' ] : [ 'days', startOf, 'plays' ], From 471f67074ed86fe068b7d7c11caee31a4e880672 Mon Sep 17 00:00:00 2001 From: Nikhil Date: Fri, 9 May 2025 17:52:34 +0530 Subject: [PATCH 2/2] Handle case of API response when summarize parameter is not set --- client/state/stats/lists/utils.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/state/stats/lists/utils.js b/client/state/stats/lists/utils.js index a715550903c8..14563d2a8a7a 100644 --- a/client/state/stats/lists/utils.js +++ b/client/state/stats/lists/utils.js @@ -459,7 +459,8 @@ export const normalizers = { }, /** - * Returns a normalized statsVideoPlaysCompleteStats array, ready for use in stats-module + * Returns a normalized data for the `stats/video-plays` API request with `complete_stats` query param set to `1` + * This returns more enriched data about the video plays, including impressions, watch time, and retention rate. * @param {Object} data Stats data * @param {Object} query Stats query * @returns {Array} Parsed data array @@ -469,7 +470,13 @@ export const normalizers = { return []; } - const videoPlaysData = get( data, [ 'days', 'summary', 'data' ], [] ); + const { startOf } = rangeOfPeriod( query.period, query.date ); + const videoPlaysData = get( + data, + query.summarize ? [ 'days', 'summary', 'data' ] : [ 'days', startOf, 'data' ], + [] + ); + const normalizedData = videoPlaysData.map( ( item ) => { return { post_id: item.post_id,