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/6372 selector to lookup ga4 props #6500

Merged
merged 13 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
105 changes: 105 additions & 0 deletions assets/js/modules/analytics-4/datastore/webdatastreams.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import difference from 'lodash/difference';
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { createValidatedAction } from '../../../googlesitekit/data/utils';
import { MODULES_ANALYTICS } from '../../analytics/datastore/constants';
import { MODULES_ANALYTICS_4, MAX_WEBDATASTREAMS_PER_BATCH } from './constants';
import { CORE_SITE } from '../../../googlesitekit/datastore/site/constants';
import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store';
Expand Down Expand Up @@ -407,6 +408,110 @@ const baseSelectors = {
}, {} );
}
),

/**
* Gets an Analytics config that matches one of provided measurement IDs.
*
* @since n.e.x.t
*
* @param {Object} state Data store's state.
* @param {string|Array.<string>} measurements Single GA4 measurement ID, or array of GA4 measurement ID strings.
* @return {(Object|null|undefined)} An Analytics config that matches provided one of measurement IDs on success, NULL if no matching config is found, undefined if data hasn't been resolved yet.
*/
getAnalyticsConfigByMeasurementIDs: createRegistrySelector(
( select ) => ( state, measurements ) => {
const measurementIDs = Array.isArray( measurements )
? measurements
: [ measurements ];

const summaries =
select( MODULES_ANALYTICS_4 ).getAccountSummaries();
if ( ! Array.isArray( summaries ) ) {
return undefined;
}

// Sort summaries to have the current account at the very beginning,
// so we can check it first because its more likely that the current
// account will contain a measurement ID that we are looking for.
const currentAccountID = select( MODULES_ANALYTICS ).getAccountID();
summaries.sort( ( { _id: accountID } ) =>
accountID === currentAccountID ? -1 : 0
);

const info = {};
const propertyIDs = [];

summaries.forEach( ( { _id: accountID, propertySummaries } ) => {
propertySummaries.forEach( ( { _id: propertyID } ) => {
propertyIDs.push( propertyID );
info[ propertyID ] = {
accountID,
propertyID,
};
} );
} );

if ( propertyIDs.length === 0 ) {
return null;
}

const datastreams =
select( MODULES_ANALYTICS_4 ).getWebDataStreamsBatch(
propertyIDs
);

// Return undefined if web data streams haven't been resolved yet.
if ( datastreams === undefined ) {
return undefined;
}

let firstlyFoundConfig;

for ( const propertyID in datastreams ) {
if ( ! datastreams[ propertyID ]?.length ) {
continue;
}

for ( const datastream of datastreams[ propertyID ] ) {
const { _id: webDataStreamID, webDataStream } = datastream;
const {
defaultUri: defaultURI,
measurementId: measurementID, // eslint-disable-line sitekit/acronym-case
} = webDataStream;

if ( ! measurementIDs.includes( measurementID ) ) {
continue;
}

const config = {
...info[ propertyID ],
webDataStreamID,
measurementID,
};

// Remember the firstly found config to return it at the end
// if we don't manage to find the most suitable config.
if ( ! firstlyFoundConfig ) {
firstlyFoundConfig = config;
}

// If only one measurement ID is provided, then we don't need
// to check whether its default URI matches the current
// reference URL. Otherwise, if we have many measurement IDs
// then we need to find the one that matches the current
// reference URL.
if (
measurementIDs.length === 1 ||
select( CORE_SITE ).isSiteURLMatch( defaultURI )
) {
return config;
}
}
}

return firstlyFoundConfig || null;
}
),
};

const store = Data.combineStores(
Expand Down
206 changes: 206 additions & 0 deletions assets/js/modules/analytics-4/datastore/webdatastreams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
waitForDefaultTimeouts,
} from '../../../../../tests/js/utils';
import { MODULES_ANALYTICS_4 } from './constants';
import { MODULES_ANALYTICS } from '../../analytics/datastore/constants';
import * as fixtures from './__fixtures__';

describe( 'modules/analytics-4 webdatastreams', () => {
Expand Down Expand Up @@ -610,5 +611,210 @@ describe( 'modules/analytics-4 webdatastreams', () => {
} );
} );
} );

describe( 'getAnalyticsConfigByMeasurementIDs', () => {
const accountSummaries = [
{
_id: '123456',
propertySummaries: [
{ _id: '1122334455' },
{ _id: '1122334456' },
{ _id: '1122334457' },
],
},
{
_id: '123457',
propertySummaries: [
{ _id: '1122334465' },
{ _id: '1122334466' },
],
},
{
_id: '123458',
propertySummaries: [ { _id: '1122334475' } ],
},
];

const propertyIDs = accountSummaries
.map( ( { propertySummaries } ) =>
propertySummaries.map( ( { _id } ) => _id )
)
.reduce( ( acc, propIDs ) => [ ...acc, ...propIDs ], [] );

const datastreams = {
1122334455: [
{
_id: '110',
webDataStream: {
defaultUri: 'http://example-1.test',
measurementId: 'G-1101', // eslint-disable-line sitekit/acronym-case
},
},
{
_id: '111',
webDataStream: {
defaultUri: 'http://example-2.test',
measurementId: 'G-1102', // eslint-disable-line sitekit/acronym-case
},
},
],
1122334465: [
{
_id: '112',
webDataStream: {
defaultUri: 'http://example-3.test',
measurementId: 'G-1103', // eslint-disable-line sitekit/acronym-case
},
},
{
_id: '113',
webDataStream: {
defaultUri: 'http://example-4.test',
measurementId: 'G-1104', // eslint-disable-line sitekit/acronym-case
},
},
],
1122334475: [
{
_id: '114',
webDataStream: {
defaultUri: 'http://example-5.test',
measurementId: 'G-1105', // eslint-disable-line sitekit/acronym-case
},
},
{
_id: '115',
webDataStream: {
defaultUri: 'http://example.com',
measurementId: 'G-1106', // eslint-disable-line sitekit/acronym-case
},
},
{
_id: '116',
webDataStream: {
defaultUri: 'http://example-7.test',
measurementId: 'G-1107', // eslint-disable-line sitekit/acronym-case
},
},
],
1122334456: [],
1122334457: [],
1122334466: [],
};

beforeEach( () => {
provideSiteInfo( registry );
registry.dispatch( MODULES_ANALYTICS ).receiveGetSettings( {
accountID: 'UA-abcd',
} );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetSettings( {} );
} );

it( 'should return NULL when no summaries are returned from the endpoint', () => {
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( [] );

const config = registry
.select( MODULES_ANALYTICS_4 )
.getAnalyticsConfigByMeasurementIDs( 'G-012345' );

expect( config ).toBeNull();
} );

it( 'should return the first config when found configs dont match the current site URL', () => {
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( datastreams, {
propertyIDs,
} );

const config = registry
.select( MODULES_ANALYTICS_4 )
.getAnalyticsConfigByMeasurementIDs( [
'G-1107',
'G-1103',
] );

expect( config ).toEqual( {
accountID: '123457',
measurementID: 'G-1103',
propertyID: '1122334465',
webDataStreamID: '112',
} );
} );

it( 'should return the correct config when there is a config that matches the current site URL', () => {
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( datastreams, {
propertyIDs,
} );

const config = registry
.select( MODULES_ANALYTICS_4 )
.getAnalyticsConfigByMeasurementIDs( [
'G-1101',
'G-1106',
] );

expect( config ).toEqual( {
accountID: '123458',
measurementID: 'G-1106',
propertyID: '1122334475',
webDataStreamID: '115',
} );
} );

it( 'should return NULL when there are no matching configs', () => {
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( datastreams, {
propertyIDs,
} );

const config = registry
.select( MODULES_ANALYTICS_4 )
.getAnalyticsConfigByMeasurementIDs( [
'G-12345',
'G-12346',
] );

expect( config ).toBeNull();
} );

it( 'should return the correct config even if it doesnt match the site URL when only one measurement ID is requested', () => {
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetAccountSummaries( accountSummaries );
registry
.dispatch( MODULES_ANALYTICS_4 )
.receiveGetWebDataStreamsBatch( datastreams, {
propertyIDs,
} );

const config = registry
.select( MODULES_ANALYTICS_4 )
.getAnalyticsConfigByMeasurementIDs( 'G-1103' );

expect( config ).toEqual( {
accountID: '123457',
measurementID: 'G-1103',
propertyID: '1122334465',
webDataStreamID: '112',
} );
} );
} );
} );
} );