diff --git a/assets/js/modules/analytics-4/datastore/base.js b/assets/js/modules/analytics-4/datastore/base.js index cdace064b20..507730ddba7 100644 --- a/assets/js/modules/analytics-4/datastore/base.js +++ b/assets/js/modules/analytics-4/datastore/base.js @@ -63,6 +63,7 @@ const baseModuleStore = Modules.createModuleStore( 'analytics-4', { 'adsLinkedLastSyncedAt', 'availableAudiences', 'availableAudiencesLastSyncedAt', + 'detectedEvents', ], submitChanges, rollbackChanges, diff --git a/assets/js/modules/analytics-4/datastore/conversion-reporting.js b/assets/js/modules/analytics-4/datastore/conversion-reporting.js new file mode 100644 index 00000000000..12fd45a200f --- /dev/null +++ b/assets/js/modules/analytics-4/datastore/conversion-reporting.js @@ -0,0 +1,56 @@ +/** + * `modules/analytics-4` data store: conversion reporting. + * + * Site Kit by Google, Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal dependencies + */ +import { createRegistrySelector } from 'googlesitekit-data'; +import { MODULES_ANALYTICS_4 } from './constants'; + +export const selectors = { + /** + * Checks whether the provided conversion reporting events are available. + * + * @since n.e.x.t + * + * @param {Object} state Data store's state. + * @param {string|Array} events Conversion reporting events to check. + * @return {(boolean|undefined)} True if all provided custom dimensions are available, otherwise false. Undefined if available custom dimensions are not loaded yet. + */ + hasConversionReportingEvents: createRegistrySelector( + ( select ) => ( state, events ) => { + // Ensure events is always an array, even if a string is passed. + const eventsToCheck = Array.isArray( events ) ? events : [ events ]; + + const detectedEvents = + select( MODULES_ANALYTICS_4 ).getDetectedEvents(); + + if ( ! detectedEvents?.length ) { + return false; + } + + return eventsToCheck.some( ( event ) => + detectedEvents.includes( event ) + ); + } + ), +}; + +export default { + selectors, +}; diff --git a/assets/js/modules/analytics-4/datastore/conversion-reporting.test.js b/assets/js/modules/analytics-4/datastore/conversion-reporting.test.js new file mode 100644 index 00000000000..aa923e6ba84 --- /dev/null +++ b/assets/js/modules/analytics-4/datastore/conversion-reporting.test.js @@ -0,0 +1,85 @@ +/** + * `modules/analytics-4` data store: conversion-reporting tests. + * + * Site Kit by Google, Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal dependencies + */ +import { MODULES_ANALYTICS_4 } from './constants'; +import { + createTestRegistry, + provideModules, + provideUserAuthentication, +} from '../../../../../tests/js/utils'; + +describe( 'modules/analytics-4 conversion-reporting', () => { + let registry; + + beforeEach( () => { + registry = createTestRegistry(); + + provideUserAuthentication( registry ); + provideModules( registry ); + } ); + + describe( 'selectors', () => { + describe( 'hasConversionReportingEvents', () => { + it( 'returns false when no conversion reporting events are available', () => { + registry + .dispatch( MODULES_ANALYTICS_4 ) + .setSettings( { detectedEvents: [] } ); + + const hasConversionReportingEvents = registry + .select( MODULES_ANALYTICS_4 ) + .hasConversionReportingEvents( [ 'test-event' ] ); + + expect( hasConversionReportingEvents ).toBe( false ); + } ); + + it( 'returns true when provided conversion reporting event is available', () => { + registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( { + detectedEvents: [ 'test-event', 'test-event-2' ], + } ); + + const hasConversionReportingEvents = registry + .select( MODULES_ANALYTICS_4 ) + .hasConversionReportingEvents( [ 'test-event' ] ); + + expect( hasConversionReportingEvents ).toBe( true ); + } ); + + it( 'returns true when some of provided conversion reporting events are available', () => { + registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( { + detectedEvents: [ + 'test-event', + 'test-event-2', + 'test-event-3', + ], + } ); + + const hasConversionReportingEvents = registry + .select( MODULES_ANALYTICS_4 ) + .hasConversionReportingEvents( [ + 'no-event', + 'test-event', + ] ); + + expect( hasConversionReportingEvents ).toBe( true ); + } ); + } ); + } ); +} ); diff --git a/assets/js/modules/analytics-4/datastore/index.js b/assets/js/modules/analytics-4/datastore/index.js index 802f2b68767..2ed957d9a78 100644 --- a/assets/js/modules/analytics-4/datastore/index.js +++ b/assets/js/modules/analytics-4/datastore/index.js @@ -26,6 +26,7 @@ import audiences from './audiences'; import baseModuleStore from './base'; import containers from './containers'; import conversionEvents from './conversion-events'; +import conversionReporting from './conversion-reporting'; import customDimensions from './custom-dimensions'; import customDimensionsGatheringData from './custom-dimensions-gathering-data'; import enhancedMeasurement from './enhanced-measurement'; @@ -44,6 +45,7 @@ const store = combineStores( baseModuleStore, containers, conversionEvents, + conversionReporting, createSnapshotStore( MODULES_ANALYTICS_4 ), customDimensions, customDimensionsGatheringData, diff --git a/includes/Modules/Analytics_4.php b/includes/Modules/Analytics_4.php index 5d7091a90ba..e52b758eada 100644 --- a/includes/Modules/Analytics_4.php +++ b/includes/Modules/Analytics_4.php @@ -297,7 +297,7 @@ function ( $audience ) { 'adSenseLinkedLastSyncedAt' => 0, 'adsLinked' => false, 'adsLinkedLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), 'availableAudiencesLastSyncedAt' => 0, ) ); diff --git a/includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_Sync.php b/includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_Sync.php index 98fb5949a05..ec4b16a9594 100644 --- a/includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_Sync.php +++ b/includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_Sync.php @@ -75,7 +75,7 @@ public function check_for_events() { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase if ( empty( $report->rowCount ) ) { - $this->settings->merge( array( 'recentEvents' => array() ) ); + $this->settings->merge( array( 'detectedEvents' => array() ) ); return; } @@ -84,7 +84,7 @@ public function check_for_events() { $detected_events[] = $row['dimensionValues'][0]['value']; } - $this->settings->merge( array( 'recentEvents' => $detected_events ) ); + $this->settings->merge( array( 'detectedEvents' => $detected_events ) ); } /** diff --git a/includes/Modules/Analytics_4/Settings.php b/includes/Modules/Analytics_4/Settings.php index f69ab5aa69f..2774f8fbfb9 100644 --- a/includes/Modules/Analytics_4/Settings.php +++ b/includes/Modules/Analytics_4/Settings.php @@ -102,7 +102,7 @@ protected function get_default() { 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ); } diff --git a/tests/phpunit/integration/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_SyncTest.php b/tests/phpunit/integration/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_SyncTest.php index ba87bdc8e3a..5306259bc3b 100644 --- a/tests/phpunit/integration/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_SyncTest.php +++ b/tests/phpunit/integration/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_SyncTest.php @@ -64,7 +64,7 @@ public function test_check_for_events( $detected_events, $report_rows ) { $event_check = $this->get_instance(); $event_check->check_for_events(); - $this->assertEquals( $detected_events, $this->settings->get()['recentEvents'] ); + $this->assertEquals( $detected_events, $this->settings->get()['detectedEvents'] ); } public function get_instance() { diff --git a/tests/phpunit/integration/Modules/Analytics_4/SettingsTest.php b/tests/phpunit/integration/Modules/Analytics_4/SettingsTest.php index 9ebdbcbd91d..650a4c044f0 100644 --- a/tests/phpunit/integration/Modules/Analytics_4/SettingsTest.php +++ b/tests/phpunit/integration/Modules/Analytics_4/SettingsTest.php @@ -86,7 +86,7 @@ public function test_get_default() { 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), get_option( Settings::OPTION ) ); diff --git a/tests/phpunit/integration/Modules/Analytics_4Test.php b/tests/phpunit/integration/Modules/Analytics_4Test.php index 9660cc1ff37..619d0211f15 100644 --- a/tests/phpunit/integration/Modules/Analytics_4Test.php +++ b/tests/phpunit/integration/Modules/Analytics_4Test.php @@ -549,7 +549,7 @@ function ( Request $request ) use ( $property_id, $webdatastream_id, $measuremen 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), $options->get( Settings::OPTION ) ); @@ -582,7 +582,7 @@ function ( Request $request ) use ( $property_id, $webdatastream_id, $measuremen 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), $options->get( Settings::OPTION ) ); @@ -708,7 +708,7 @@ function ( Request $request ) use ( $property_id, $webdatastream_id, $measuremen 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), $options->get( Settings::OPTION ) ); @@ -835,7 +835,7 @@ function ( Request $request ) use ( $property_id, $webdatastream_id, $measuremen 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), $options->get( Settings::OPTION ) ); @@ -871,7 +871,7 @@ function ( Request $request ) use ( $property_id, $webdatastream_id, $measuremen 'adsLinkedLastSyncedAt' => 0, 'availableAudiences' => null, 'availableAudiencesLastSyncedAt' => 0, - 'recentEvents' => array(), + 'detectedEvents' => array(), ), $options->get( Settings::OPTION ) );