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

Create GA4 DashboardAllTrafficWidget. #6514

Merged
merged 50 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a6f5835
Add initial DashboardAllTrafficWidgetGA4 widget, still using UA data.
techanvil Jan 31, 2023
46b7fac
Add initial DashboardAllTrafficWidgetGA4 stories, still using UA data.
techanvil Jan 31, 2023
2837424
Add initial GA4 DimensionTabs and UserDimensionsPieChart (still UA da…
techanvil Jan 31, 2023
53fd40a
Add GA4 isGatheringData and hasZeroData selectors, and isZeroData fun…
techanvil Jan 31, 2023
5a3cf8c
Add initial GA4 TotalUserCount and UserCountGraph (still UA data).
techanvil Jan 31, 2023
0e5a5f9
Fix GA4 mock data utility.
techanvil Jan 31, 2023
2a189b4
Update to use GA4 report options and selectors.
techanvil Jan 31, 2023
53fe19d
Add GA4 extractAnalyticsDataForPieChart function (still UA data).
techanvil Jan 31, 2023
32afa85
Update DashboardAllTrafficWidgetGA4 to use GA4 report data.
techanvil Jan 31, 2023
18cdc86
Prevent getAnalytics4MockResponse from mutating passed options.
techanvil Feb 1, 2023
36623fa
Update DashboardAllTrafficWidgetGA4 stories to use GA4 data.
techanvil Feb 1, 2023
2cf72f3
Update DashboardAllTrafficWidgetGA4 stories.
techanvil Feb 1, 2023
223aafa
Rename story file.
techanvil Feb 1, 2023
3a9b81d
Add Entity Dashboard stories for DashboardAllTrafficWidgetGA4.
techanvil Feb 1, 2023
5eebd0c
Improve date range handling in getAnalytics4MockResponse.
techanvil Feb 1, 2023
69cced1
Rename story files.
techanvil Feb 2, 2023
92578fd
Specify "Main Dashboard" rather than just "Dashboard".
techanvil Feb 2, 2023
146308b
Fix getAnalytics4MockResponse for multiple date ranges.
techanvil Feb 2, 2023
64974b5
Fix report args in DashboardAllTrafficWidgetGA4 story.
techanvil Feb 2, 2023
ed3b9ab
Add tests for GA4 isZeroReport.
techanvil Feb 2, 2023
f4ea986
Add tests for isSingleSliceGA4.
techanvil Feb 2, 2023
a6a0b07
Add tests for GA4 isGatheringData and hasZeroData.
techanvil Feb 2, 2023
ca3414d
Add TODO comments.
techanvil Feb 2, 2023
7fbce1a
Fix typo.
techanvil Feb 2, 2023
8492da8
Fix DashboardAllTrafficWidgetGA4 Loading VRT scenarios.
techanvil Feb 2, 2023
2f56fb8
Fix VRT animation rules.
techanvil Feb 2, 2023
7bf4e11
Add VRT reference images for DashboardAllTrafficWidgetGA4.
techanvil Feb 2, 2023
afa014c
Restore UA All Traffic widget on Dashboard.
techanvil Feb 2, 2023
d44b5a1
Add comment.
techanvil Feb 2, 2023
c5ca3a4
Fix typo.
techanvil Feb 2, 2023
16ae1bb
Fix copyright year in file headers.
techanvil Feb 2, 2023
d8ef488
Merge branch 'develop' into enhancement/6216-ga4-all-traffic-widget.
techanvil Feb 8, 2023
bebd5a3
Update orderby to use new shape.
techanvil Feb 8, 2023
0b3363b
Order line chart by date.
techanvil Feb 8, 2023
09d4dba
Fix merge error.
techanvil Feb 8, 2023
d974e42
Avoid creating a new `util` directory where there is `utils`.
techanvil Feb 8, 2023
87b7c0d
Rename file.
techanvil Feb 8, 2023
dcc30d0
Move isSingleSliceGA4 to isSingleSlice in GA4 module.
techanvil Feb 8, 2023
611fc5a
Fix "one row of data" stories.
techanvil Feb 8, 2023
12deb97
Merge branch 'develop' into enhancement/6216-ga4-all-traffic-widget.
techanvil Feb 15, 2023
b81f9e4
Fix DashboardAllTrafficWidgetGA4 Data Unavailable and Zero Data stories.
techanvil Feb 15, 2023
af96125
Fix rendering of UserCountGraph chart ticks for empty reports.
techanvil Feb 15, 2023
e46af58
Update VRT reference images.
techanvil Feb 15, 2023
bda3284
Only add zero data rows for the zero data case, and not for gathering…
techanvil Feb 15, 2023
9ae3bd1
Remove commented out lines.
techanvil Feb 15, 2023
729d642
Update VRT reference images.
techanvil Feb 15, 2023
208e9f3
Avoid abbreviated variable name.
techanvil Feb 22, 2023
87e2e64
Use tab dimensionName rather than index for key and in select value.
techanvil Feb 22, 2023
e19ffcf
Clarify comment.
techanvil Feb 22, 2023
ff3f09b
Restore use of index in option value.
techanvil Feb 22, 2023
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
19 changes: 18 additions & 1 deletion .storybook/preview-head-vrt.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
#root *,
Copy link
Collaborator Author

@techanvil techanvil Feb 9, 2023

Choose a reason for hiding this comment

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

Please remember to review the notes in Relevant technical choices in the PR description.

#root *::before,
#root *::after {
animation-duration: 0ms !important;
transition-duration: 0ms !important;
}

/* We only want to override animation-duration if _not_ pausing the animations via the .googlesitekit-vrt-animation-paused class. */
#root:not( :has( .googlesitekit-vrt-animation-paused ) ) *,
#root:not( :has( .googlesitekit-vrt-animation-paused ) ) *::before,
#root:not( :has( .googlesitekit-vrt-animation-paused ) ) *::after {
animation-duration: 0ms !important;
}

/**
* While the rules above set animation-duration to 0ms for VRT builds, there can be times where this is not appropriate.
* For example if there is a constant animation in a story, it results in the animation running constantly at top speed and the VRT screengrab becomes somewhat undeterministic.
Expand All @@ -18,4 +24,15 @@
animation: none !important;
transition: none !important;
}

/**
* An alternative scenario is that the VRT scenario does not render correctly unless the animation is active but paused.
* In this case, we can apply the following .googlesitekit-vrt-animation-paused class to a story wrapper element to pause animations for a given story.
* Please note, this only applies to animations and not transitions which don't have an equivalent paused state.
*/
#root .googlesitekit-vrt-animation-paused *,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This class has been added to allow the Loading VRT scenarios to display properly...

#root .googlesitekit-vrt-animation-paused *::before,
#root .googlesitekit-vrt-animation-paused *::after {
animation-play-state: paused !important;
}
</style>
4 changes: 2 additions & 2 deletions .storybook/utils/zeroReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export function replaceValuesInAnalyticsReportWithZeroData( report ) {
* @return {Object} Empty Analytics 4 report object.
*/
export function replaceValuesInAnalytics4ReportWithZeroData( report ) {
// eslint-disable-next-line no-unused-vars -- Ignore `rows` since we're omitting it from the returned report object.
const { rows, ...reportWithoutRows } = report;
// eslint-disable-next-line no-unused-vars -- Ignore `rows` and `rowCount` since we're omitting them from the returned report object.
const { rows, rowCount, ...reportWithoutRows } = report;

return {
...reportWithoutRows,
Expand Down
148 changes: 148 additions & 0 deletions assets/js/modules/analytics-4/utils/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Analytics utility functions.
*
* Site Kit by Google, Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Extracts data required for a pie chart from the Analytics 4 report information.
*
* @since n.e.x.t
*
* @param {Array} report The report data.
* @param {Object} [options] Optional. Data extraction options.
* @param {number} [options.keyColumnIndex] Optional. The number of a column to extract metrics data from.
* @param {number} [options.maxSlices] Optional. Limit the number of slices to display.
* @param {boolean} [options.withOthers] Optional. Whether to add "Others" record to the data map. Only relevant
* if `maxSlices` is passed. If passed, the final slice will be the
* "Others" slice, i.e. the number of actual row slices will be
* `maxSlices - 1`.
* @param {Function} [options.tooltipCallback] Optional. A callback function for tooltip column values.
* @return {Array} Extracted data.
*/
export function extractAnalyticsDataForPieChart( report, options = {} ) {
const {
keyColumnIndex = 0,
maxSlices,
withOthers = false,
tooltipCallback,
} = options;

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

const withTooltips = typeof tooltipCallback === 'function';
const columns = [ 'Source', 'Percent' ];
if ( withTooltips ) {
columns.push( {
type: 'string',
role: 'tooltip',
p: {
html: true,
},
} );
}

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

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

let hasOthers = withOthers;
let rowsNumber = currentDateRangeRows.length;
let others = 1;
if ( maxSlices > 0 ) {
hasOthers = withOthers && currentDateRangeRows.length > maxSlices;
rowsNumber = Math.min(
currentDateRangeRows.length,
hasOthers ? maxSlices - 1 : maxSlices
);
} else {
hasOthers = false;
rowsNumber = currentDateRangeRows.length;
}

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

others -= percent;

const rowData = [ row.dimensionValues[ 0 ].value, percent ];
if ( withTooltips ) {
const previousDateRangeRow = rows.find(
( { dimensionValues } ) =>
dimensionValues[ 1 ].value === 'date_range_1' &&
dimensionValues[ 0 ].value ===
row.dimensionValues[ 0 ].value
);

rowData.push(
tooltipCallback( row, previousDateRangeRow, rowData )
);
}

dataMap.push( rowData );
}

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

dataMap.push( rowData );
}

return dataMap;
}

/**
* Checks if there is a single row of data or one row of the provided GA4 report is contributing 100% of the total for a given dimension.
*
* Note that chart reports will be in the multi-date range format.
*
* @since n.e.x.t
*
* @param {Object} report The report data object.
* @return {(boolean|undefined)} Returns undefined if report is undefined, true/false for the above conditions.
*/
export const isSingleSlice = ( report ) => {
if ( report === undefined ) {
return undefined;
}

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

if (
currentDateRangeRows?.length === 1 ||
currentDateRangeRows?.[ 0 ]?.metricValues?.[ 0 ]?.value ===
report?.totals?.[ 0 ]?.metricValues?.[ 0 ]?.value
) {
return true;
}

return false;
};
Loading