Skip to content

Batch report requests using batchRunReports #11553

@benbowler

Description

@benbowler

Feature Description

  • Improve report request time using the batchRunReports API

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • A new route GET:batch-report is implemented which uses batchRunReports to batch several requests together

Implementation Brief

  • Extract/adapt the batchRunReports logic from this PoC
  • Update includes/Modules/Analytics_4.php
    • Include GET:batch-report to the list in get_datapoint_definitions
      • Use analyticsdata for service and true for shareable
    • In create_data_request method:
      • Add new case for GET:batch-report - it should expect $data['requests'] as an array
      • requests should be limited to max 5 items in the array, if more than 5 throw error
      • Instantiate Analytics_4_Report_Request - into say $report
      • Iterate over $data['requests'] and for each request (say $data_request):
        • Instantiate Data_Request for the report ('GET', 'modules', $this->slug, 'report') and pass current request data as last parameter ($data_request)
        • Create a report request, eq $report->create_request(), using $data_request and $this->is_shared_data_request( $data_request ) as parameters
        • Batch the returned request to an array
      • Instantiate new Google_Service_AnalyticsData\BatchRunReportsRequest() and push the batched requests array using setRequests method ($batch_request->setRequests( $batch_requests ))
      • Invoke batchRunReports via $this->get_analyticsdata_service()->properties->batchRunReports and pass property ID and $batch_request as parameters

Test Coverage

  • Update tests/phpunit/integration/Modules/Analytics_4Test.php to cover new batch request route

QA Brief

QA:Eng

  • Connect GA4 module, and edit Analytics_4.php - within register method, on the bottom for example, paste this:
$api_configs = array(
			array(
				'metrics'    => array( 'sessions' ),
				'dimensions' => array( 'sessionDefaultChannelGroup', 'date' ),
				'startDate'  => gmdate( 'Y-m-d', strtotime( '-28 days' ) ),
				'endDate'    => gmdate( 'Y-m-d' ),
				'orderBys'   => array(
					array(
						'metric' => array( 'metricName' => 'sessions' ),
						'desc'   => true,
					),
				),
				'limit'      => 3,
			),
			array(
				'metrics'         => array( 'activeUsers' ),
				'dimensions'      => array( 'date', 'newVsReturning' ),
				'startDate'       => gmdate( 'Y-m-d', strtotime( '-28 days' ) ),
				'endDate'         => gmdate( 'Y-m-d' ),
				'dimensionFilter' => array(
					'filter' => array(
						'fieldName'    => 'newVsReturning',
						'stringFilter' => array(
							'matchType' => 'EXACT',
							'value'     => 'returning',
						),
					),
				),
				'orderBys'        => array(
					array(
						'dimension' => array( 'dimensionName' => 'date' ),
						'desc'      => false,
					),
				),
			),
		);

		$a = $this->get_data( 'batch-report', array(
			'requests' => $api_configs,
		) );
		var_dump( $a );
  • Verify that request worked and response is output on the dashboard

Changelog entry

  • N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0High priorityQA: EngRequires specialized QA by an engineerTeam SIssues for Squad 1Type: EnhancementImprovement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions