Skip to content

Commit

Permalink
Merge pull request #22023 from Yoast/416-implement-organic-session-ba…
Browse files Browse the repository at this point in the history
…ckend-endpoint-using-the-google-analytics-api

416 implement organic session backend endpoint using the google analytics api
  • Loading branch information
thijsoo authored Feb 18, 2025
2 parents 507449f + 804871a commit c11c566
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function __construct(
*/
public function get_data( Parameters $parameters ): Data_Container {

$top_pages_search_data = $this->site_kit_search_console_adapter->get_data( $parameters );
$top_pages_full_data = $this->top_page_indexable_collector->get_data( $top_pages_search_data );
$top_pages_search_ranking_data = $this->site_kit_search_console_adapter->get_data( $parameters );
$top_pages_full_data = $this->top_page_indexable_collector->get_data( $top_pages_search_ranking_data );

return $top_pages_full_data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function __construct( Site_Kit_Search_Console_Adapter $site_kit_search_co
}

/**
* Method to get search related data from a provider.
* Gets the top queries' data.
*
* @param Parameters $parameters The parameter to get the search data for.
* @param Parameters $parameters The parameter to use for getting the top queries.
*
* @return Data_Container
*/
Expand Down
48 changes: 48 additions & 0 deletions src/dashboard/application/traffic/organic-sessions-repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Application\Traffic;

use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Dashboard_Repository_Interface;
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Container;
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Parameters;
use Yoast\WP\SEO\Dashboard\Infrastructure\Analytics_4\Site_Kit_Analytics_4_Adapter;

/**
* The data provider for organic sessions data.
*/
class Organic_Sessions_Repository implements Dashboard_Repository_Interface {

/**
* The adapter.
*
* @var Site_Kit_Analytics_4_Adapter $site_kit_analytics_4_adapter
*/
private $site_kit_analytics_4_adapter;

/**
* The constructor.
*
* @param Site_Kit_Analytics_4_Adapter $site_kit_analytics_4_adapter The adapter.
*/
public function __construct(
Site_Kit_Analytics_4_Adapter $site_kit_analytics_4_adapter
) {
$this->site_kit_analytics_4_adapter = $site_kit_analytics_4_adapter;
}

/**
* Gets the organic sessions' data.
*
* @param Parameters $parameters The parameter to use for getting the organic sessions' data.
*
* @return Data_Container
*
* @throws Exception When getting the organic sessions' data fails.
*/
public function get_data( Parameters $parameters ): Data_Container {

$organic_sessions_data = $this->site_kit_analytics_4_adapter->get_data( $parameters );

return $organic_sessions_data;
}
}
21 changes: 21 additions & 0 deletions src/dashboard/domain/analytics-4/failed-request-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Domain\Analytics_4;

use Exception;

/**
* Exception for when an Analytics 4 request fails.
*/
class Failed_Request_Exception extends Exception {

/**
* Constructor of the exception.
*
* @param string $error_message The error message of the request.
* @param int $error_status_code The error status code of the request.
*/
public function __construct( $error_message, $error_status_code ) {
parent::__construct( 'The Analytics 4 request failed: ' . $error_message, $error_status_code );
}
}
20 changes: 20 additions & 0 deletions src/dashboard/domain/analytics-4/invalid-request-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Domain\Analytics_4;

use Exception;

/**
* Exception for when an Analytics 4 request is invalid.
*/
class Invalid_Request_Exception extends Exception {

/**
* Constructor of the exception.
*
* @param string $error_message The error message of the request.
*/
public function __construct( $error_message ) {
parent::__construct( 'The Analytics 4 request is invalid: ' . $error_message, 400 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
interface Dashboard_Repository_Interface {

/**
* Method to get search related data from a provider.
* Method to get dashboard related data from a provider.
*
* @param Parameters $parameters The parameter to get the search data for.
* @param Parameters $parameters The parameter to get the dashboard data for.
*
* @return Data_Container
*/
Expand Down
55 changes: 55 additions & 0 deletions src/dashboard/domain/data-provider/parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ abstract class Parameters {
*/
private $limit;

/**
* The compare start date.
*
* @var string $compare_start_date
*/
private $compare_start_date;

/**
* The compare end date.
*
* @var string $compare_end_date
*/
private $compare_end_date;

/**
* Getter for the start date.
*
Expand Down Expand Up @@ -56,6 +70,25 @@ public function get_limit(): int {
return $this->limit;
}

/**
* Getter for the compare start date.
*
* @return string
*/
public function get_compare_start_date(): ?string {
return $this->compare_start_date;
}

/**
* Getter for the compare end date.
* The date format should be Y-M-D.
*
* @return string
*/
public function get_compare_end_date(): ?string {
return $this->compare_end_date;
}

/**
* The start date setter.
*
Expand Down Expand Up @@ -87,4 +120,26 @@ public function set_end_date( string $end_date ): void {
public function set_limit( int $limit ): void {
$this->limit = $limit;
}

/**
* The compare start date setter.
*
* @param string $compare_start_date The compare start date.
*
* @return void
*/
public function set_compare_start_date( string $compare_start_date ): void {
$this->compare_start_date = $compare_start_date;
}

/**
* The compare end date setter.
*
* @param string $compare_end_date The compare end date.
*
* @return void
*/
public function set_compare_end_date( string $compare_end_date ): void {
$this->compare_end_date = $compare_end_date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Interface;

/**
* Domain object that represents a single Search Data record.
* Domain object that represents a single Search Ranking Data record.
*/
class Search_Data implements Data_Interface {
class Search_Ranking_Data implements Data_Interface {

/**
* The amount of clicks a `subject` gets.
Expand Down
22 changes: 11 additions & 11 deletions src/dashboard/domain/search-rankings/top-page-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
class Top_Page_Data implements Data_Interface {

/**
* The search data for the top page.
* The search ranking data for the top page.
*
* @var Search_Data $search_data
* @var Search_Ranking_Data $search_ranking_data
*/
private $search_data;
private $search_ranking_data;

/**
* The SEO score group the top page belongs to.
Expand All @@ -34,18 +34,18 @@ class Top_Page_Data implements Data_Interface {
/**
* The constructor.
*
* @param Search_Data $search_data The search data for the top page.
* @param SEO_Score_Groups_Interface $seo_score_group The SEO score group the top page belongs to.
* @param string $edit_link The edit link of the top page.
* @param Search_Ranking_Data $search_ranking_data The search ranking data for the top page.
* @param SEO_Score_Groups_Interface $seo_score_group The SEO score group the top page belongs to.
* @param string $edit_link The edit link of the top page.
*/
public function __construct(
Search_Data $search_data,
Search_Ranking_Data $search_ranking_data,
SEO_Score_Groups_Interface $seo_score_group,
?string $edit_link = null
) {
$this->search_data = $search_data;
$this->seo_score_group = $seo_score_group;
$this->edit_link = $edit_link;
$this->search_ranking_data = $search_ranking_data;
$this->seo_score_group = $seo_score_group;
$this->edit_link = $edit_link;
}

/**
Expand All @@ -54,7 +54,7 @@ public function __construct(
* @return array<string|float|int|string[]>
*/
public function to_array(): array {
$top_page_data = $this->search_data->to_array();
$top_page_data = $this->search_ranking_data->to_array();
$top_page_data['seoScore'] = $this->seo_score_group->get_name();
$top_page_data['links'] = [];

Expand Down
70 changes: 70 additions & 0 deletions src/dashboard/domain/traffic/comparison-traffic-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Domain\Traffic;

use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Interface;

/**
* Domain object that represents a single Comparison Traffic record.
*/
class Comparison_Traffic_Data implements Data_Interface {

/**
* The current traffic data.
*
* @var Traffic_Data $current_traffic_data
*/
private $current_traffic_data;

/**
* The previous traffic data.
*
* @var Traffic_Data $previous_traffic_data
*/
private $previous_traffic_data;

/**
* The constructor.
*
* @param Traffic_Data $current_traffic_data The current traffic data.
* @param Traffic_Data $previous_traffic_data The previous traffic data.
*/
public function __construct( ?Traffic_Data $current_traffic_data = null, ?Traffic_Data $previous_traffic_data = null ) {
$this->current_traffic_data = $current_traffic_data;
$this->previous_traffic_data = $previous_traffic_data;
}

/**
* Sets the current traffic data.
*
* @param Traffic_Data $current_traffic_data The current traffic data.
*
* @return void
*/
public function set_current_traffic_data( Traffic_Data $current_traffic_data ): void {
$this->current_traffic_data = $current_traffic_data;
}

/**
* Sets the previous traffic data.
*
* @param Traffic_Data $previous_traffic_data The previous traffic data.
*
* @return void
*/
public function set_previous_traffic_data( Traffic_Data $previous_traffic_data ): void {
$this->previous_traffic_data = $previous_traffic_data;
}

/**
* The array representation of this domain object.
*
* @return array<string|float|int|string[]>
*/
public function to_array(): array {
return [
'current' => $this->current_traffic_data->to_array(),
'previous' => $this->previous_traffic_data->to_array(),
];
}
}
48 changes: 48 additions & 0 deletions src/dashboard/domain/traffic/daily-traffic-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Domain\Traffic;

use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Interface;

/**
* Domain object that represents a single Daily Traffic record.
*/
class Daily_Traffic_Data implements Data_Interface {

/**
* The date of the traffic data, in YYYYMMDD format.
*
* @var string $date
*/
private $date;

/**
* The traffic data for the date.
*
* @var Traffic_Data $traffic_data
*/
private $traffic_data;

/**
* The constructor.
*
* @param string $date The date of the traffic data, in YYYYMMDD format.
* @param Traffic_Data $traffic_data The traffic data for the date.
*/
public function __construct( string $date, Traffic_Data $traffic_data ) {
$this->date = $date;
$this->traffic_data = $traffic_data;
}

/**
* The array representation of this domain object.
*
* @return array<string,string,int>
*/
public function to_array(): array {
$result = [];
$result['date'] = $this->date;

return \array_merge( $result, $this->traffic_data->to_array() );
}
}
Loading

0 comments on commit c11c566

Please sign in to comment.