Skip to content
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

Fix incorrect figures in All Traffic widget pie charts #10244

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,41 +166,20 @@ export default function UserDimensionsPieChart( props ) {
isTooltipOpen,
] );

const absOthers = {
current: report?.totals?.[ 0 ]?.metricValues?.[ 0 ]?.value,
previous: report?.totals?.[ 1 ]?.metricValues?.[ 0 ]?.value,
};

( report?.rows || [] ).forEach( ( { dimensionValues, metricValues } ) => {
const dateRangeDimension = dimensionValues[ 1 ].value;

if ( dateRangeDimension === 'date_range_0' ) {
absOthers.current -= metricValues[ 0 ].value;
} else if ( dateRangeDimension === 'date_range_1' ) {
absOthers.previous -= metricValues[ 0 ].value;
}
} );

const dataMap = extractAnalyticsDataForPieChart( report, {
keyColumnIndex: 0,
maxSlices: 5,
withOthers: true,
tooltipCallback: ( row, previousDateRangeRow, rowData ) => {
let difference =
previousDateRangeRow?.metricValues?.[ 0 ].value > 0
? ( row?.metricValues?.[ 0 ].value * 100 ) /
previousDateRangeRow?.metricValues?.[ 0 ].value -
const difference =
previousDateRangeRow.metricValues?.[ 0 ].value > 0
? ( row.metricValues?.[ 0 ].value * 100 ) /
previousDateRangeRow.metricValues?.[ 0 ].value -
100
: 100;

if ( row === null && absOthers.previous > 0 ) {
difference =
( absOthers.current * 100 ) / absOthers.previous - 100;
}
const svgArrow = getChartDifferenceArrow( difference );
const absValue = row
? row.metricValues[ 0 ].value
: absOthers.current;
const absValue = row.metricValues[ 0 ].value;
const statInfo = sprintf(
/* translators: 1: numeric value of users, 2: up or down arrow , 3: different change in percentage, %%: percent symbol */
_x(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function DashboardAllTrafficWidgetGA4( props ) {
desc: true,
},
],
limit: 6,
};

const graphArgs = {
Expand Down
56 changes: 46 additions & 10 deletions assets/js/modules/analytics-4/utils/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function extractAnalyticsDataForPieChart( report, options = {} ) {
tooltipCallback,
} = options;

const { rows = [], totals = [] } = report || {};
const { rows = [] } = report || {};

const withTooltips = typeof tooltipCallback === 'function';
const columns = [ 'Source', 'Percent' ];
Expand All @@ -59,17 +59,30 @@ export function extractAnalyticsDataForPieChart( report, options = {} ) {
} );
}

const totalUsers =
totals?.[ 0 ]?.metricValues?.[ keyColumnIndex ]?.value || 0;
const dataMap = [ columns ];

const currentDateRangeRows = rows.filter(
( { dimensionValues } ) => dimensionValues[ 1 ].value === 'date_range_0'
);

const totalCurrent = currentDateRangeRows.reduce(
( sum, row ) => sum + parseInt( row.metricValues[ 0 ].value, 10 ),
0
);

const previousDateRangeRows = rows.filter(
( { dimensionValues } ) => dimensionValues[ 1 ].value === 'date_range_1'
);

const totalPrevious = previousDateRangeRows.reduce(
( sum, row ) => sum + parseInt( row.metricValues[ 0 ].value, 10 ),
0
);

let hasOthers = withOthers;
let rowsNumber = currentDateRangeRows.length;
let others = 1;
let othersCurrent = totalCurrent;
let othersPrevious = totalPrevious;
if ( maxSlices > 0 ) {
hasOthers = withOthers && currentDateRangeRows.length > maxSlices;
rowsNumber = Math.min(
Expand All @@ -83,10 +96,20 @@ export function extractAnalyticsDataForPieChart( report, options = {} ) {

for ( let i = 0; i < rowsNumber; i++ ) {
const row = currentDateRangeRows[ i ];
const users = row.metricValues[ keyColumnIndex ].value;
const percent = totalUsers > 0 ? users / totalUsers : 0;
const usersCurrent = row.metricValues[ keyColumnIndex ].value;

others -= percent;
const previousRow = previousDateRangeRows.find(
( { dimensionValues } ) =>
dimensionValues[ 0 ].value === row.dimensionValues[ 0 ].value
);
const usersPrevious = previousRow
? previousRow.metricValues[ keyColumnIndex ].value
: 0;

othersCurrent -= usersCurrent;
othersPrevious -= usersPrevious;

const percent = totalCurrent > 0 ? usersCurrent / totalCurrent : 0;

const rowData = [ row.dimensionValues[ 0 ].value, percent ];
if ( withTooltips ) {
Expand All @@ -105,10 +128,23 @@ export function extractAnalyticsDataForPieChart( report, options = {} ) {
dataMap.push( rowData );
}

if ( hasOthers && others > 0 ) {
const rowData = [ __( 'Others', 'google-site-kit' ), others ];
if ( hasOthers && othersCurrent > 0 ) {
const rowData = [
__( 'Others', 'google-site-kit' ),
othersCurrent / totalCurrent,
];
if ( withTooltips ) {
rowData.push( tooltipCallback( null, null, rowData ) );
rowData.push(
tooltipCallback(
{
metricValues: [ { value: othersCurrent } ],
},
{
metricValues: [ { value: othersPrevious } ],
},
rowData
)
);
}

dataMap.push( rowData );
Expand Down