Skip to content

STATS-60 - Fix: match the Video stats CSV download to the production export #103287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion client/my-sites/stats/stats-download-csv/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am adding a condition here because getSiteStatsCSVData calles another function that only keeps three columns of data in the export. This is being used in all the other exports hence I did not refactor this to handle this data for now to avoid a larger PR

const isLoading = isRequestingSiteStatsForQuery( state, siteId, statType, query );

return { data, siteSlug, siteId, isLoading };
Expand Down
1 change: 1 addition & 0 deletions client/my-sites/stats/summary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class StatsSummary extends Component {
title = translate( 'Videos' );
path = 'videoplays';
statType = 'statsVideoPlays';
moduleQuery.complete_stats = 1;
summaryView = (
<Fragment key="videopress-stats-module">
{ /* For CSV button to work, video page needs to pass custom data to the button.
Expand Down
42 changes: 42 additions & 0 deletions client/state/stats/lists/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' ],
Expand Down