-
Notifications
You must be signed in to change notification settings - Fork 291
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
Add support for GA4 pivot reports #8484
Comments
As discussed here in 8136 there are a number of refactors required to the AudienceTiles and AudienceTile components which are best worked on here:
site-kit-wp/assets/js/modules/analytics-4/components/dashboard/AudienceSegmentation/AudienceTiles.js Lines 211 to 271 in 5dcc66a
Lines 50 to 63 in 5dcc66a
Lines 50 to 63 in 5dcc66a
|
For reference, here is an example pivot report and it's response based on my testing: Report Options
Report Response
I often substitute I also have an Insomnia playground export with some example queries which I can share with anyone who picks this up. |
@techanvil I split this ticket into two, moving work specific to Audience Tiles to #8726 as there is a lot of work involved in both parts of this ticket. |
Thanks @benbowler, that sounds sensible! Regarding the IB, it's a good first take. However with the The commonalities between the two can be shared either via existing helpers or extracting new ones where appropriate. This approach should be cleaner and easier to maintain going forward, and we won't have to maintain a mental model of the differences between the two GA4 endpoints to make sense of a single SK endpoint. I realise the AC was a bit open to interpretation, so I've updated it to be more explicit. Please can you iterate on the IB, taking this into consideration? |
Thanks @techanvil, I did consider this split originally, however I thought it would lead to lots of duplicated code as the report structures are so similar, also our report infra already does not directly map to the GA Data API structure. That's not a reason not to improve things though. I've updated the IB to have distinct pivot selector and REST endpoint and pivot-reports store. I've increased the estimate as there will be lots of additional changes and requirements for additional tests to complete this work. |
Thanks Ben! IB LGTM ✅ |
QA Brief run as part of the code review, and since it's very straightforward moving directly to Approval, since there isn't much to QA. 😄 |
Feature Description
As discussed on Slack, while we are initially implementing the Audience Tiles to use separate, per-audience reports to retrieve the cities and "top content" metrics (one report per audience per metric), we can reduce the number of reports needed by using pivot reports. This will allow us to retrieve metric data for all of the audiences in a single report (i.e., one report per metric).
We should add support for running pivot reports, and refactor the Audience Tiles to use them.
This is not critical to the release and can be done post-launch, hence the P2 priority.
Please also note this comment relating to the
AudienceTile
andAudienceTiles
refactoring: #8484 (comment)Because of the amount of work involved, this ticket has been split, only the GA4 pivot report infrastructure should be worked on in this ticket. Updating the Audience Tiles should be completed in #8726.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
GET /google-site-kit/v1/modules/analytics-4/data/pivot-report
.getPivotReport()
.Implementation Brief
Pivot Report Infrastructure
Create a new class,
SharedReportParsers
, and moveparse_dimensions
,parse_dateranges
andparse_orderby
methods fromincludes/Modules/Analytics_4/Report.php
into this class so that they can be shared byReport
andPivotReport
classes. Update both of these classes to use these methods from the newSharedReportParsers
class.Create a new class,
PivotReport
, inincludes/Modules/Analytics_4/PivotReport.php
, based on the existingincludes/Modules/Analytics_4/Report.php
class with these differences:Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\Pivot
asGoogle_Service_AnalyticsData_Pivot
.parse_pivots
, which takesData_Request
and returnsGoogle_Service_AnalyticsData_Pivot[]
, modelled on the parse_orderby method.array_map
, create a newGoogle_Service_AnalyticsData_Pivot
and usesetFieldNames
,setLimit
andsetOrderBys
to build each pivot request value.Create a new class.
Request
, inincludes/Modules/Analytics_4/PivotReport/Request.php
, based onincludes/Modules/Analytics_4/Report/Request.php
, which extends PivotReport:Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\RunPivotReportRequest
asGoogle_Service_AnalyticsData_RunPivotReportRequest
.create_request
method:$request
should create a newGoogle_Service_AnalyticsData_RunPivotReportRequest
.$request->setKeepEmptyRows( true );
hostName
dimension using$dimensions[] = array('name' => 'hostName');
.This is needed because of the hostname dimension filter we add to all requests here to filter out domains other than the WordPress sites domain.
$pivots = $this->parse_pivots( $data );
and add them to the request using$request->setPivots( $pivots )
.Google_Service_AnalyticsData_RunPivotReportRequest
$request
.SharedCreateRequest
class and extract common logic from theRequest
andPivotRequest
classes'create_request
methods, into this class.REST Endpoint
GET:pivot-report
, modelled onGET:report
, with the following differences:missing_required_param
WP_Error
if thepivots
param is missing from the request.runPivotReport
GA API method.$datapoints
array inget_datapoint_definitions
, settingservice
toanalyticsdata
andsharable
totrue
.Pivot Reports store and getPivotReport selector
Create a new
isValidPivots
util in the fileassets/js/modules/analytics-4/utils/pivots-validation.js
, based onassets/js/modules/analytics-4/utils/report-validation.js
. A valid pivots object must:isPlainObject
fieldNames
prop which must contain and array of strings.orderBys
prop is passed it must validate using the existingisValidOrders
helper.Create a new file
assets/js/modules/analytics-4/datastore/pivot-report.js
, based on the existingassets/js/modules/analytics-4/datastore/report.js
file, which creates a new pivot reports datastore:createFetchStore
with the following keys:baseName
:getPivotReport
controlCallback
returnsAPI.get
to the newpivot-report
endpoint, passing the options, passed tonormalizeReportOptions
.reducerCallback
should update thepivotReports
key within the state object with[ stringifyObject( options ) ]: report,
to save reports to a unique caching key based on their options.argsToParams
should return{ options }
validateParams
should validate params similarly to the existingreports.js
file with these key changes/additions:pivots.length
invariant violation: pivots must contain at least one value to be a valid pivot report requestisValidPivots(pivots)
invariant violation: must validate a given pivots array using theisValidPivots
util created above.! orderby.length
invariant violation: orderbys may not be set at the parent level for pivot reports, if passed this must cause an invariant violation.baseInitialState
should contain a singlepivotReports
key with an empty object by defaultpivotReports: {}
.baseResolvers
should contain a single resolver calledgetPivotReport
, which uses thegetPivotReport
selector to get the report from the store if present.fetchGetPivotReport
passing through the options.baseSelectors
should contain a single selector,getPivotReport
which returns the report for the stringified key of the request options usingpivotReports[ stringifyObject( options ) ]
fetchGetReportStore
with theinitialState
,selectors
, andresolvers
created in the last few steps and export the store and it's core components from this file.Import the new pivot-report store as
pivotReport
inassets/js/modules/analytics-4/datastore/index.js
and include it in the maincombineStores
call to make the store available within the app.Data Mock Updates
getAnalytics4MockResponse
inassets/js/modules/analytics-4/utils/data-mock.js
to generate valid data when given a valid pivot report object.assets/js/modules/analytics-4/utils/data-mock.test.js
Test Coverage
assets/js/modules/analytics-4/datastore/pivot-report.test.js
which tests the new store and thegetPivotsReport
selector.tests/phpunit/integration/Modules/Analytics_4Test.php
adding comprehensive new tests of the new REST endpoint, and the pivot report functions including limits and ordering. Note: when making a pivot report you cannot pass limit or orderbys to the top level, they must be included for each pivot instead.QA Brief
getPivotReport
, you can call the selector in a component or console. There are some example pivot report options we will use in the Audience Tile refactor ticket #8726.eg. pivot report request:
Changelog entry
The text was updated successfully, but these errors were encountered: