Yoast\WP\SEO\Dashboard\Infrastructure\Analytics_4

Site_Kit_Analytics_4_Adapter::parse_daily_response()privateYoast 1.0

Parses a response for a Site Kit API request that requests daily data for Analytics 4.

Метод класса: Site_Kit_Analytics_4_Adapter{}

Хуков нет.

Возвращает

Data_Container. The parsed response.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->parse_daily_response( $response ): Data_Container;
$response(RunReportResponse) (обязательный)
The response to parse.

Код Site_Kit_Analytics_4_Adapter::parse_daily_response() Yoast 25.1

private function parse_daily_response( RunReportResponse $response ): Data_Container {
	if ( ! $this->is_daily_request( $response ) ) {
		throw new Invalid_Request_Exception( 'Unexpected parameters for the request' );
	}

	$data_container = new Data_Container();

	foreach ( $response->getRows() as $daily_traffic ) {
		$traffic_data = new Traffic_Data();

		foreach ( $response->getMetricHeaders() as $key => $metric ) {

			// As per https://developers.google.com/analytics/devguides/reporting/data/v1/basics#read_the_response,
			// the order of the columns is consistent in the request, header, and rows.
			// So we can use the key of the header to get the correct metric value from the row.
			$metric_value = $daily_traffic->getMetricValues()[ $key ]->getValue();

			if ( $metric->getName() === 'sessions' ) {
				$traffic_data->set_sessions( (int) $metric_value );
			}
			elseif ( $metric->getName() === 'totalUsers' ) {
				$traffic_data->set_total_users( (int) $metric_value );
			}
		}

		// Since we're here, we know that the first dimension is date, so we know that dimensionValues[0]->value is a date.
		$data_container->add_data( new Daily_Traffic_Data( $daily_traffic->getDimensionValues()[0]->getValue(), $traffic_data ) );
	}

	return $data_container;
}