Skip to content

Commit

Permalink
Merge pull request #6415 from google/enhancement/#6348-rest-ga4-conve…
Browse files Browse the repository at this point in the history
…rsion-events

Add REST datapoint for GA4 conversion events
  • Loading branch information
eugene-manuilov authored Jan 17, 2023
2 parents 47c8dde + 72245e3 commit 7fa4d67
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
19 changes: 19 additions & 0 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ protected function get_datapoint_definitions() {
),
'GET:webdatastreams' => array( 'service' => 'analyticsadmin' ),
'GET:webdatastreams-batch' => array( 'service' => 'analyticsadmin' ),
'GET:conversion-events' => array( 'service' => 'analyticsadmin' ),
);
}

Expand Down Expand Up @@ -502,6 +503,22 @@ protected function create_data_request( Data_Request $data ) {
return $this->get_tagmanager_service()->accounts_containers_destinations->listAccountsContainersDestinations(
"accounts/{$data['accountID']}/containers/{$data['internalContainerID']}"
);
case 'GET:conversion-events':
if ( ! isset( $data['propertyID'] ) ) {
return new WP_Error(
'missing_required_param',
/* translators: %s: Missing parameter name */
sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'propertyID' ),
array( 'status' => 400 )
);
}

$analyticsadmin = $this->get_service( 'analyticsadmin' );
$property_id = self::normalize_property_id( $data['propertyID'] );

return $analyticsadmin
->properties_conversionEvents // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
->listPropertiesConversionEvents( $property_id );
}

return parent::create_data_request( $data );
Expand Down Expand Up @@ -555,6 +572,8 @@ function( $property ) {
return self::parse_webdatastreams_batch( $response );
case 'GET:container-destinations':
return (array) $response->getDestination();
case 'GET:conversion-events':
return (array) $response->getConversionEvents();
}

return parent::parse_data_response( $data, $response );
Expand Down
76 changes: 75 additions & 1 deletion tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
use Google\Site_Kit\Tests\FakeHttpClient;
use Google\Site_Kit\Tests\TestCase;
use Google\Site_Kit\Tests\UserAuthenticationTrait;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaConversionEvent;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaDataStream;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaDataStreamWebStreamData;
use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdmin\GoogleAnalyticsAdminV1betaListConversionEventsResponse;
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\Request;
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\Response;
use Google\Site_Kit_Dependencies\GuzzleHttp\Stream\Stream;
Expand Down Expand Up @@ -244,6 +246,7 @@ public function test_get_datapoints() {
'accounts',
'container-lookup',
'container-destinations',
'conversion-events',
'create-property',
'create-webdatastream',
'properties',
Expand Down Expand Up @@ -895,6 +898,55 @@ public function test_report__no_property_id( $access_token ) {
$this->assertEquals( array( 'status' => 500 ), $data->get_error_data( 'missing_required_setting' ) );
}

/**
* @dataProvider data_access_token
*
* When an access token is provided, the user will be authenticated for the test.
*
* @param string $access_token Access token, or empty string if none.
*/
public function test_get_conversion_events( $access_token ) {
$this->setup_user_authentication( $access_token );

$property_id = '123456789';

$this->analytics->get_settings()->merge(
array(
'propertyID' => $property_id,
)
);

// Grant scopes so request doesn't fail.
$this->authentication->get_oauth_client()->set_granted_scopes(
$this->analytics->get_scopes()
);

$http_client = $this->create_fake_http_client( $property_id );
$this->analytics->get_client()->setHttpClient( $http_client );
$this->analytics->register();

// Fetch conversion events.
$data = $this->analytics->get_data(
'conversion-events',
array(
'propertyID' => $property_id,
)
);

$this->assertNotWPError( $data );

// Verify the conversion events are returned by checking an event name.
$this->assertEquals( 'some-event', $data[0]['eventName'] );

// Verify the request URL and params were correctly generated.
$this->assertCount( 1, $this->request_handler_calls );

$request_url = $this->request_handler_calls[0]['url'];

$this->assertEquals( 'analyticsadmin.googleapis.com', $request_url['host'] );
$this->assertEquals( "/v1beta/properties/$property_id/conversionEvents", $request_url['path'] );
}

/**
* Returns a date string for the given number of days ago.
*
Expand Down Expand Up @@ -953,7 +1005,13 @@ function ( Request $request ) use ( $property_id ) {
'params' => $params,
);

if ( 'analyticsdata.googleapis.com' !== $url['host'] ) {
if (
! in_array(
$url['host'],
array( 'analyticsdata.googleapis.com', 'analyticsadmin.googleapis.com' ),
true
)
) {
return new Response( 200 );
}

Expand Down Expand Up @@ -983,6 +1041,22 @@ function ( Request $request ) use ( $property_id ) {
)
);

case "/v1beta/properties/$property_id/conversionEvents":
$conversion_event = new GoogleAnalyticsAdminV1betaConversionEvent();
$conversion_event->setName( "properties/$property_id/conversionEvents/some-name" );
$conversion_event->setEventName( 'some-event' );

$conversion_events = new GoogleAnalyticsAdminV1betaListConversionEventsResponse();
$conversion_events->setConversionEvents( array( $conversion_event ) );

return new Response(
200,
array(),
Stream::factory(
json_encode( $conversion_events )
)
);

default:
return new Response( 200 );
}
Expand Down

0 comments on commit 7fa4d67

Please sign in to comment.