Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/9132 acr datastore partial #9251

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/js/modules/analytics-4/datastore/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const baseModuleStore = Modules.createModuleStore( 'analytics-4', {
'adsLinkedLastSyncedAt',
'availableAudiences',
'availableAudiencesLastSyncedAt',
'detectedEvents',
],
submitChanges,
rollbackChanges,
Expand Down
56 changes: 56 additions & 0 deletions assets/js/modules/analytics-4/datastore/conversion-reporting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* `modules/analytics-4` data store: coversion reporting.
tofumatt marked this conversation as resolved.
Show resolved Hide resolved
*
* 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<string>} 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,
};
Original file line number Diff line number Diff line change
@@ -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 );
} );
} );
} );
} );
2 changes: 2 additions & 0 deletions assets/js/modules/analytics-4/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -44,6 +45,7 @@ const store = combineStores(
baseModuleStore,
containers,
conversionEvents,
conversionReporting,
createSnapshotStore( MODULES_ANALYTICS_4 ),
customDimensions,
customDimensionsGatheringData,
Expand Down
2 changes: 1 addition & 1 deletion includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function ( $audience ) {
'adSenseLinkedLastSyncedAt' => 0,
'adsLinked' => false,
'adsLinkedLastSyncedAt' => 0,
'recentEvents' => array(),
'detectedEvents' => array(),
'availableAudiencesLastSyncedAt' => 0,
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/Modules/Analytics_4/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function get_default() {
'adsLinkedLastSyncedAt' => 0,
'availableAudiences' => null,
'availableAudiencesLastSyncedAt' => 0,
'recentEvents' => array(),
'detectedEvents' => array(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function test_get_default() {
'adsLinkedLastSyncedAt' => 0,
'availableAudiences' => null,
'availableAudiencesLastSyncedAt' => 0,
'recentEvents' => array(),
'detectedEvents' => array(),
),
get_option( Settings::OPTION )
);
Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
Expand Down Expand Up @@ -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 )
);
Expand Down Expand Up @@ -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 )
);
Expand Down Expand Up @@ -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 )
);
Expand Down Expand Up @@ -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 )
);
Expand Down
Loading