Skip to content

Commit

Permalink
Merge pull request #3081 from google/enhancement/3012-analytics-accou…
Browse files Browse the repository at this point in the history
…nt-summaries

Enhancement/3012 analytics account summaries
  • Loading branch information
tofumatt authored Apr 15, 2021
2 parents 53b5b47 + a4d8be8 commit d6ea055
Showing 1 changed file with 65 additions and 60 deletions.
125 changes: 65 additions & 60 deletions includes/Modules/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,26 +762,16 @@ function ( Google_Service_Analytics_Account $account ) {
if ( in_array( $account_id, $account_ids, true ) ) {
$properties_profiles = $this->get_data( 'properties-profiles', array( 'accountID' => $account_id ) );
} else {
$fallback_properties_profiles = null;
// Iterate over the first 25 accounts to avoid making too many requests.
foreach ( array_slice( $accounts, 0, 25 ) as $account ) {
/* @var Google_Service_Analytics_Account $account Analytics account object. */
$properties_profiles = $this->get_data( 'properties-profiles', array( 'accountID' => $account->getId() ) );

if ( is_wp_error( $properties_profiles ) ) {
$properties_profiles = $fallback_properties_profiles;
// Stop iteration to avoid potential quota errors.
$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 );

foreach ( $account_summaries as $account_summary ) {
$found_property = $this->find_property( $account_summary->getWebProperties(), '', $current_urls );
if ( ! is_null( $found_property ) ) {
$properties_profiles = $this->get_data( 'properties-profiles', array( 'accountID' => $account_summary->getId() ) );
break;
}
// If we found a matchedProperty, we're all done.
if ( isset( $properties_profiles['matchedProperty'] ) ) {
break;
}
// If we didn't find a matched property yet,
// save the response as a fallback, if none set yet.
if ( null === $fallback_properties_profiles ) {
$fallback_properties_profiles = $properties_profiles;
}
}
}
}
Expand All @@ -807,8 +797,9 @@ function ( Google_Service_Analytics_Account $account ) {
return $response;
case 'GET:properties-profiles':
/* @var Google_Service_Analytics_Webproperties $response listManagementWebproperties response. */
$properties = (array) $response->getItems();
$response = array(
$properties = (array) $response->getItems();
$found_property = null;
$response = array(
'properties' => $properties,
'profiles' => array(),
);
Expand All @@ -817,32 +808,19 @@ function ( Google_Service_Analytics_Account $account ) {
return $response;
}

$found_property = new Google_Service_Analytics_Webproperty();
$current_url = $this->context->get_reference_site_url();

// If requested for a specific property, only match by property ID.
if ( ! empty( $data['existingPropertyID'] ) ) {
$property_id = $data['existingPropertyID'];
$current_urls = array();
$found_property = $this->find_property( $properties, $data['existingPropertyID'], array() );
} else {
$option = $this->get_settings()->get();
$property_id = $option['propertyID'];
$current_urls = $this->permute_site_url( $current_url );
$current_url = $this->context->get_reference_site_url();
$current_urls = $this->permute_site_url( $current_url );
$found_property = $this->find_property( $properties, '', $current_urls );
}

// If there's no match for the saved account ID, try to find a match using the properties of each account.
foreach ( $properties as $property ) {
/* @var Google_Service_Analytics_Webproperty $property Property instance. */
if (
// Attempt to match by property ID.
$property->getId() === $property_id ||
// Attempt to match by site URL, with and without http/https and 'www' subdomain.
in_array( untrailingslashit( $property->getWebsiteUrl() ), $current_urls, true )
) {
$found_property = $property;
$response['matchedProperty'] = $property;
break;
}
if ( ! is_null( $found_property ) ) {
$response['matchedProperty'] = $found_property;
} else {
$found_property = new Google_Service_Analytics_Webproperty();
}

// If no match is found, fetch profiles for the first property if available.
Expand Down Expand Up @@ -1064,22 +1042,26 @@ protected function has_access_to_property( $property_id ) {
);
}

$account_id = $this->parse_account_id( $property_id );
$account_id = $this->parse_account_id( $property_id );
$account_summaries = $this->get_service( 'analytics' )->management_accountSummaries->listManagementAccountSummaries();

/**
* Helper method to check check if a given account
* contains the property_id
*/
$has_property = function ( $account_id ) use ( $property_id ) {
$response = $this->get_data( 'properties-profiles', array( 'accountID' => $account_id ) );
if ( is_wp_error( $response ) ) {
return false;
}
foreach ( $response['properties'] as $property ) {
if ( $property->getId() === $property_id ) {
return true;
$has_property = function ( $account_id ) use ( $property_id, $account_summaries ) {
foreach ( $account_summaries as $account_summary ) {
if ( $account_summary->getId() !== $account_id ) {
continue;
}

foreach ( $account_summary->getWebProperties() as $property ) {
if ( $property->getId() === $property_id ) {
return true;
}
}
}

return false;
};

Expand All @@ -1091,17 +1073,10 @@ protected function has_access_to_property( $property_id ) {
);
}

// Check all of the accounts for this user.
$user_accounts_properties_profiles = $this->get_data( 'accounts-properties-profiles' );
$user_account_ids = is_wp_error( $user_accounts_properties_profiles ) ? array() : wp_list_pluck( $user_accounts_properties_profiles['accounts'], 'id' );
foreach ( $user_account_ids as $user_account_id ) {
// Skip the inferred account id, that ship has sailed.
if ( $account_id === $user_account_id ) {
continue;
}
if ( $has_property( $user_account_id ) ) {
foreach ( $account_summaries as $account_summary ) {
if ( $has_property( $account_summary->getId() ) ) {
return array(
'accountID' => $user_account_id,
'accountID' => $account_id,
'permission' => true,
);
}
Expand Down Expand Up @@ -1301,4 +1276,34 @@ private function register_tag() {
}
}

/**
* Finds a property in the properties list.
*
* @since n.e.x.t
*
* @param array $properties An array of Analytics properties to search in.
* @param string $property_id Optional. The Analytics property ID. Default is the current property ID from the Analytics settings.
* @param array $urls Optional. An array of URLs that searched property can have.
* @return mixed A property instance on success, otherwise NULL.
*/
protected function find_property( array $properties, $property_id = '', array $urls = array() ) {
if ( strlen( $property_id ) === 0 ) {
$option = $this->get_settings()->get();
$property_id = $option['propertyID'];
}

foreach ( $properties as $property ) {
/* @var Google_Service_Analytics_Webproperty $property Property instance. */
$id = $property->getId();
$website_url = $property->getWebsiteUrl();
$website_url = untrailingslashit( $website_url );

if ( $id === $property_id || ( 0 < count( $urls ) && in_array( $website_url, $urls, true ) ) ) {
return $property;
}
}

return null;
}

}

0 comments on commit d6ea055

Please sign in to comment.