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..14563d2a8a7a 100644 --- a/client/state/stats/lists/utils.js +++ b/client/state/stats/lists/utils.js @@ -458,6 +458,42 @@ export const normalizers = { } ); }, + /** + * 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 + */ + statsVideoPlaysCompleteStats: ( data, query = {} ) => { + if ( ! data || ! query.period || ! query.date ) { + return []; + } + + 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, + 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 +507,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' ],