Skip to content

Commit

Permalink
Merge pull request #6790 from google/enhancement/6623-ga4-widgets-rea…
Browse files Browse the repository at this point in the history
…l-data

Enhancement/6623 ga4 widgets real data
  • Loading branch information
techanvil authored Apr 5, 2023
2 parents 6ea9e69 + ac2339a commit a886611
Show file tree
Hide file tree
Showing 17 changed files with 1,399 additions and 780 deletions.
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 @@ -64,12 +71,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 @@ -684,12 +685,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 @@ -890,7 +891,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 @@ -939,7 +940,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 @@ -1046,7 +1047,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

0 comments on commit a886611

Please sign in to comment.