Skip to content

Commit

Permalink
Add get and post audience-settings datapoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-t committed Mar 14, 2024
1 parent 5bfdaf4 commit ce2eba0
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Google\Site_Kit\Modules\Analytics\Settings as Analytics_Settings;
use Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
use Google\Site_Kit\Modules\Analytics_4\AMP_Tag;
use Google\Site_Kit\Modules\Analytics_4\Audience_Settings;
use Google\Site_Kit\Modules\Analytics_4\Custom_Dimensions_Data_Available;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_Property;
use Google\Site_Kit\Modules\Analytics_4\GoogleAnalyticsAdmin\AccountProvisioningService;
Expand Down Expand Up @@ -135,6 +136,14 @@ final class Analytics_4 extends Module
*/
protected $custom_dimensions_data_available;

/**
* Audience_Settings instance.
*
* @since n.e.x.t
* @var Audience_Settings
*/
protected $audience_settings;

/**
* Constructor.
*
Expand All @@ -155,6 +164,7 @@ public function __construct(
) {
parent::__construct( $context, $options, $user_options, $authentication, $assets );
$this->custom_dimensions_data_available = new Custom_Dimensions_Data_Available( $this->transients );
$this->audience_settings = new Audience_Settings( $this->user_options );
}

/**
Expand Down Expand Up @@ -506,6 +516,12 @@ protected function get_datapoint_definitions() {
'scopes' => array( Analytics::EDIT_SCOPE ),
'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create new audiences for your Analytics 4 property on your behalf.', 'google-site-kit' ),
);
$datapoints['GET:audience-settings'] = array(
'service' => '',
);
$datapoints['POST:audience-settings'] = array(
'service' => '',
);
}

return $datapoints;
Expand Down Expand Up @@ -942,6 +958,53 @@ protected function create_data_request( Data_Request $data ) {
$property_id,
$post_body
);
case 'GET:audience-settings':
return function() {
return $this->audience_settings->get();
};
case 'POST:audience-settings':
$settings = $data['settings'];
if ( ! isset( $settings['configuredAudiences'] ) ) {
return new WP_Error(
'missing_required_param',
/* translators: %s: Missing parameter name */
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'configuredAudiences' ),
array( 'status' => 400 )
);
}

if ( ! is_array( $settings['configuredAudiences'] ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Invalid parameter */
sprintf( __( 'Invalid parameter: %s.', 'google-site-kit' ), 'configuredAudiences' ),
array( 'status' => 400 )
);
}

if ( ! isset( $settings['isAudienceSegmentationWidgetHidden'] ) ) {
return new WP_Error(
'missing_required_param',
/* translators: %s: Missing parameter name */
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'isAudienceSegmentationWidgetHidden' ),
array( 'status' => 400 )
);
}

if ( ! is_bool( $settings['isAudienceSegmentationWidgetHidden'] ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Invalid parameter */
sprintf( __( 'Invalid parameter: %s.', 'google-site-kit' ), 'isAudienceSegmentationWidgetHidden' ),
array( 'status' => 400 )
);
}

$this->audience_settings->merge( $data['settings'] );

return function() {
return $this->audience_settings->get();
};
case 'POST:create-account-ticket':
if ( empty( $data['displayName'] ) ) {
throw new Missing_Required_Param_Exception( 'displayName' );
Expand Down

0 comments on commit ce2eba0

Please sign in to comment.