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

Enhancement/6623 ga4 widgets real data #6790

Merged
merged 31 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f18e950
Add the new parse_report_response method.
eugene-manuilov Mar 28, 2023
e349669
Add the parse_reporting_dimensions method.
eugene-manuilov Mar 28, 2023
0bed654
Add the parse_reporting_dateranges method.
eugene-manuilov Mar 28, 2023
797578c
Implement the parse_report_response method.
eugene-manuilov Mar 28, 2023
4c06396
Update rows count.
eugene-manuilov Mar 28, 2023
6ca7774
Merge remote-tracking branch 'origin/develop' into enhancement/6623-g…
eugene-manuilov Mar 29, 2023
680a723
Update the parse_report_response method to set default values to all …
eugene-manuilov Mar 29, 2023
2a2d97e
Fix formatting issues.
eugene-manuilov Mar 29, 2023
97e835a
Add tests for the new method.
eugene-manuilov Mar 29, 2023
e130cc9
Apply suggestions from code review
eugene-manuilov Mar 30, 2023
eb5e884
Fix typos.
eugene-manuilov Mar 30, 2023
4aa8fec
Addressed code review feedback.
eugene-manuilov Mar 30, 2023
182b2fe
Moved the report related functionality to its own class.
eugene-manuilov Mar 30, 2023
3b5a4c8
Fix formatting issues.
eugene-manuilov Mar 30, 2023
d71278a
Update the test_parse_response method to use expected dates array.
eugene-manuilov Mar 30, 2023
fd91b5a
Fixed ordering issues.
eugene-manuilov Mar 31, 2023
b458c4f
Merge remote-tracking branch 'origin/develop' into enhancement/6623-g…
eugene-manuilov Mar 31, 2023
1dc3c19
Refactor the Report class into Response and Request ones.
eugene-manuilov Mar 31, 2023
84cb586
Add the Row_Trait trait.
eugene-manuilov Mar 31, 2023
73384fa
Update the default value for all metrics to be '0'.
eugene-manuilov Mar 31, 2023
43f6d6e
Fix formatting issues.
eugene-manuilov Mar 31, 2023
ebe895f
Merge remote-tracking branch 'origin/develop' into enhancement/6623-g…
eugene-manuilov Apr 3, 2023
78387da
Fix date ranges issue and add more tests.
eugene-manuilov Apr 3, 2023
fb98422
Address code review feedback.
eugene-manuilov Apr 3, 2023
53f76e1
Fix typo.
eugene-manuilov Apr 3, 2023
1557f86
Add $existing_rows to use clause.
techanvil Apr 3, 2023
c2ec3cf
Fix typo in comment.
techanvil Apr 3, 2023
9ae6e2b
Update tests to check initial data as well.
eugene-manuilov Apr 4, 2023
da96ecf
Fix formatting issues.
eugene-manuilov Apr 4, 2023
054afa9
Merge remote-tracking branch 'origin/develop' into enhancement/6623-g…
eugene-manuilov Apr 4, 2023
ac2339a
Simplify test and make expectations more verbose.
techanvil Apr 4, 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
98 changes: 0 additions & 98 deletions includes/Core/Modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception;
use Google\Site_Kit\Core\REST_API\Data_Request;
use Google\Site_Kit\Core\Storage\Transients;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit_Dependencies\Google\Service as Google_Service;
use Google\Site_Kit_Dependencies\Google_Service_Exception;
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
use Google\Site_Kit_Dependencies\TrueBV\Punycode;
use WP_Error;

/**
Expand Down Expand Up @@ -433,37 +431,6 @@ private function validate_base_scopes( OAuth_Client $oauth_client ) {
}
}

/**
* Parses a date range string into a start date and an end date.
*
* @since 1.0.0
*
* @param string $range Date range string. Either 'last-7-days', 'last-14-days', 'last-90-days', or
* 'last-28-days' (default).
* @param string $multiplier Optional. How many times the date range to get. This value can be specified if the
* range should be request multiple times back. Default 1.
* @param int $offset Days the range should be offset by. Default 1. Used by Search Console where
* data is delayed by two days.
* @param bool $previous Whether to select the previous period. Default false.
*
* @return array List with two elements, the first with the start date and the second with the end date, both as
* 'Y-m-d'.
*/
protected function parse_date_range( $range, $multiplier = 1, $offset = 1, $previous = false ) {
preg_match( '*-(\d+)-*', $range, $matches );
$number_of_days = $multiplier * ( isset( $matches[1] ) ? $matches[1] : 28 );

// Calculate the end date. For previous period requests, offset period by the number of days in the request.
$end_date_offset = $previous ? $offset + $number_of_days : $offset;
$date_end = gmdate( 'Y-m-d', strtotime( $end_date_offset . ' days ago' ) );

// Set the start date.
$start_date_offset = $end_date_offset + $number_of_days - 1;
$date_start = gmdate( 'Y-m-d', strtotime( $start_date_offset . ' days ago' ) );

return array( $date_start, $date_end );
}

/**
* Gets the output for a specific frontend hook.
*
Expand All @@ -488,71 +455,6 @@ final protected function get_frontend_hook_output( $hook ) {
return $output;
}

/**
* Permutes site URL to cover all different variants of it (not considering the path).
*
* @since 1.0.0
*
* @param string $site_url Site URL to get permutations for.
* @return array List of permutations.
*/
final protected function permute_site_url( $site_url ) {
$hostname = URL::parse( $site_url, PHP_URL_HOST );
$path = URL::parse( $site_url, PHP_URL_PATH );

return array_reduce(
$this->permute_site_hosts( $hostname ),
function ( $urls, $host ) use ( $path ) {
$host_with_path = $host . $path;
array_push( $urls, "https://$host_with_path", "http://$host_with_path" );
return $urls;
},
array()
);
}

/**
* Generates common variations of the given hostname.
*
* Returns a list of hostnames that includes:
* - (if IDN) in Punycode encoding
* - (if IDN) in Unicode encoding
* - with and without www. subdomain (including IDNs)
*
* @since 1.38.0
*
* @param string $hostname Hostname to generate variations of.
* @return string[] Hostname variations.
*/
protected function permute_site_hosts( $hostname ) {
$punycode = new Punycode();
// See \Requests_IDNAEncoder::is_ascii.
$is_ascii = preg_match( '/(?:[^\x00-\x7F])/', $hostname ) !== 1;
$is_www = 0 === strpos( $hostname, 'www.' );
// Normalize hostname without www.
$hostname = $is_www ? substr( $hostname, strlen( 'www.' ) ) : $hostname;
$hosts = array( $hostname, "www.$hostname" );

try {
// An ASCII hostname can only be non-IDN or punycode-encoded.
if ( $is_ascii ) {
// If the hostname is in punycode encoding, add the decoded version to the list of hosts.
if ( 0 === strpos( $hostname, Punycode::PREFIX ) || false !== strpos( $hostname, '.' . Punycode::PREFIX ) ) {
$host_decoded = $punycode->decode( $hostname );
array_push( $hosts, $host_decoded, "www.$host_decoded" );
}
} else {
// If it's not ASCII, then add the punycode encoded version.
$host_encoded = $punycode->encode( $hostname );
array_push( $hosts, $host_encoded, "www.$host_encoded" );
}
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Do nothing.
}

return $hosts;
}

/**
* Gets the Google client the module uses.
*
Expand Down
51 changes: 51 additions & 0 deletions includes/Core/Util/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Class Google\Site_Kit\Core\Util\URL
*
* @package Google\Site_Kit\Core\Util
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Core\Util;

/**
* Class for custom date parsing methods.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Date {

/**
* Parses a date range string into a start date and an end date.
*
* @since n.e.x.t
*
* @param string $range Date range string. Either 'last-7-days', 'last-14-days', 'last-90-days', or
* 'last-28-days' (default).
* @param string $multiplier Optional. How many times the date range to get. This value can be specified if the
* range should be request multiple times back. Default 1.
* @param int $offset Days the range should be offset by. Default 1. Used by Search Console where
* data is delayed by two days.
* @param bool $previous Whether to select the previous period. Default false.
* @return array List with two elements, the first with the start date and the second with the end date, both as 'Y-m-d'.
*/
public static function parse_date_range( $range, $multiplier = 1, $offset = 1, $previous = false ) {
preg_match( '*-(\d+)-*', $range, $matches );
$number_of_days = $multiplier * ( isset( $matches[1] ) ? $matches[1] : 28 );

// Calculate the end date. For previous period requests, offset period by the number of days in the request.
$end_date_offset = $previous ? $offset + $number_of_days : $offset;
$date_end = gmdate( 'Y-m-d', strtotime( $end_date_offset . ' days ago' ) );

// Set the start date.
$start_date_offset = $end_date_offset + $number_of_days - 1;
$date_start = gmdate( 'Y-m-d', strtotime( $start_date_offset . ' days ago' ) );

return array( $date_start, $date_end );
}

}
68 changes: 68 additions & 0 deletions includes/Core/Util/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Google\Site_Kit\Core\Util;

use Google\Site_Kit_Dependencies\TrueBV\Punycode;

/**
* Class for custom URL parsing methods.
*
Expand Down Expand Up @@ -95,4 +97,70 @@ function ( $matches ) {

return $parts;
}

/**
* Permutes site URL to cover all different variants of it (not considering the path).
*
* @since n.e.x.t
*
* @param string $site_url Site URL to get permutations for.
* @return array List of permutations.
*/
public static function permute_site_url( $site_url ) {
$hostname = self::parse( $site_url, PHP_URL_HOST );
$path = self::parse( $site_url, PHP_URL_PATH );

return array_reduce(
self::permute_site_hosts( $hostname ),
function ( $urls, $host ) use ( $path ) {
$host_with_path = $host . $path;
array_push( $urls, "https://$host_with_path", "http://$host_with_path" );
return $urls;
},
array()
);
}

/**
* Generates common variations of the given hostname.
*
* Returns a list of hostnames that includes:
* - (if IDN) in Punycode encoding
* - (if IDN) in Unicode encoding
* - with and without www. subdomain (including IDNs)
*
* @since n.e.x.t
*
* @param string $hostname Hostname to generate variations of.
* @return string[] Hostname variations.
*/
public static function permute_site_hosts( $hostname ) {
$punycode = new Punycode();
// See \Requests_IDNAEncoder::is_ascii.
$is_ascii = preg_match( '/(?:[^\x00-\x7F])/', $hostname ) !== 1;
$is_www = 0 === strpos( $hostname, 'www.' );
// Normalize hostname without www.
$hostname = $is_www ? substr( $hostname, strlen( 'www.' ) ) : $hostname;
$hosts = array( $hostname, "www.$hostname" );

try {
// An ASCII hostname can only be non-IDN or punycode-encoded.
if ( $is_ascii ) {
// If the hostname is in punycode encoding, add the decoded version to the list of hosts.
if ( 0 === strpos( $hostname, Punycode::PREFIX ) || false !== strpos( $hostname, '.' . Punycode::PREFIX ) ) {
$host_decoded = $punycode->decode( $hostname );
array_push( $hosts, $host_decoded, "www.$host_decoded" );
}
} else {
// If it's not ASCII, then add the punycode encoded version.
$host_encoded = $punycode->encode( $hostname );
array_push( $hosts, $host_encoded, "www.$host_encoded" );
}
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Do nothing.
}

return $hosts;
}

}
9 changes: 5 additions & 4 deletions includes/Modules/AdSense.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
use Google\Site_Kit\Core\REST_API\Data_Request;
use Google\Site_Kit\Core\Tags\Guards\Tag_Environment_Type_Guard;
use Google\Site_Kit\Core\Tags\Guards\Tag_Verify_Guard;
use Google\Site_Kit\Core\Util\Date;
use Google\Site_Kit\Core\Util\Debug_Data;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Core\Util\Sort;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit\Modules\AdSense\AMP_Tag;
use Google\Site_Kit\Modules\AdSense\Settings;
use Google\Site_Kit\Modules\AdSense\Tag_Guard;
Expand All @@ -45,8 +48,6 @@
use Google\Site_Kit_Dependencies\Google\Service\Adsense\Alert as Google_Service_Adsense_Alert;
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
use Exception;
use Google\Site_Kit\Core\Util\Sort;
use Google\Site_Kit\Core\Util\URL;
use WP_Error;

/**
Expand Down Expand Up @@ -528,7 +529,7 @@ private function date_range_to_dates( $date_range ) {
case 'last-14-days':
case 'last-28-days':
case 'last-90-days':
return $this->parse_date_range( $date_range );
return Date::parse_date_range( $date_range );
}

return new WP_Error( 'invalid_date_range', __( 'Invalid date range.', 'google-site-kit' ) );
Expand Down Expand Up @@ -611,7 +612,7 @@ protected function create_adsense_earning_data_request( array $args = array() )
function ( $hostname ) {
return 'DOMAIN_NAME==' . $hostname;
},
$this->permute_site_hosts( $site_hostname )
URL::permute_site_hosts( $site_hostname )
)
);

Expand Down
23 changes: 12 additions & 11 deletions includes/Modules/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Modules\Module_With_Data_Available_State;
use Google\Site_Kit\Core\Modules\Module_With_Data_Available_State_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Deactivation;
use Google\Site_Kit\Core\Modules\Module_With_Debug_Fields;
use Google\Site_Kit\Core\Modules\Module_With_Scopes;
Expand All @@ -22,6 +24,7 @@
use Google\Site_Kit\Core\Modules\Module_With_Assets_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Owner;
use Google\Site_Kit\Core\Modules\Module_With_Owner_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Service_Entity;
use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception;
use Google\Site_Kit\Core\Validation\Exception\Invalid_Report_Metrics_Exception;
use Google\Site_Kit\Core\Validation\Exception\Invalid_Report_Dimensions_Exception;
Expand All @@ -33,9 +36,13 @@
use Google\Site_Kit\Core\REST_API\Data_Request;
use Google\Site_Kit\Core\Tags\Guards\Tag_Environment_Type_Guard;
use Google\Site_Kit\Core\Tags\Guards\Tag_Verify_Guard;
use Google\Site_Kit\Core\Util\Date;
use Google\Site_Kit\Core\Util\Debug_Data;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Core\Util\BC_Functions;
use Google\Site_Kit\Core\Util\Sort;
use Google\Site_Kit\Core\Util\URL;
use Google\Site_Kit\Modules\Analytics\Account_Ticket;
use Google\Site_Kit\Modules\Analytics\Google_Service_AnalyticsProvisioning;
use Google\Site_Kit\Modules\Analytics\AMP_Tag;
Expand Down Expand Up @@ -63,12 +70,6 @@
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
use WP_Error;
use Exception;
use Google\Site_Kit\Core\Modules\Module_With_Data_Available_State;
use Google\Site_Kit\Core\Modules\Module_With_Data_Available_State_Trait;
use Google\Site_Kit\Core\Modules\Module_With_Service_Entity;
use Google\Site_Kit\Core\Util\BC_Functions;
use Google\Site_Kit\Core\Util\Sort;
use Google\Site_Kit\Core\Util\URL;

/**
* Class representing the Analytics module.
Expand Down Expand Up @@ -656,12 +657,12 @@ function ( $dimension_def ) {
}
} else {
$date_range = $data['dateRange'] ?: 'last-28-days';
$date_ranges[] = $this->parse_date_range( $date_range, $data['compareDateRanges'] ? 2 : 1 );
$date_ranges[] = Date::parse_date_range( $date_range, $data['compareDateRanges'] ? 2 : 1 );

// When using multiple date ranges, it changes the structure of the response,
// where each date range becomes an item in a list.
if ( ! empty( $data['multiDateRange'] ) ) {
$date_ranges[] = $this->parse_date_range( $date_range, 1, 1, true );
$date_ranges[] = Date::parse_date_range( $date_range, 1, 1, true );
}
}

Expand Down Expand Up @@ -862,7 +863,7 @@ function ( Google_Service_Analytics_Account $account ) {
} else {
$account_summaries = $this->get_service( 'analytics' )->management_accountSummaries->listManagementAccountSummaries();
$current_url = $this->context->get_reference_site_url();
$current_urls = $this->permute_site_url( $current_url );
$current_urls = URL::permute_site_url( $current_url );

foreach ( $account_summaries as $account_summary ) {
$found_property = $this->find_property( $account_summary->getWebProperties(), '', $current_urls );
Expand Down Expand Up @@ -911,7 +912,7 @@ function ( Google_Service_Analytics_Account $account ) {
}

$current_url = $this->context->get_reference_site_url();
$current_urls = $this->permute_site_url( $current_url );
$current_urls = URL::permute_site_url( $current_url );
$found_property = $this->find_property( $properties, '', $current_urls );

if ( ! is_null( $found_property ) ) {
Expand Down Expand Up @@ -1018,7 +1019,7 @@ protected function create_analytics_site_data_request( array $args = array() ) {

$dimension_filter_clauses = array();

$hostnames = $this->permute_site_hosts( URL::parse( $this->context->get_reference_site_url(), PHP_URL_HOST ) );
$hostnames = URL::permute_site_hosts( URL::parse( $this->context->get_reference_site_url(), PHP_URL_HOST ) );

$dimension_filter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimension_filter->setDimensionName( 'ga:hostname' );
Expand Down
Loading