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

Enhance/6079 - Google Tag container lookup and destinations list JS selectors #6367

Merged
merged 21 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"6065484567": {
"98369876": [
{
"path": "accounts/6065484567/containers/98369876/destinations/1",
"accountId": "6065484567",
"containerId": "98369876",
"destinationLinkId": "1",
"destinationId": "G-2B7M8YQ1K6",
"name": "example.com",
"fingerprint": "1670408765154",
"tagManagerUrl": "https://tagmanager.google.com/#/admin/accounts/6065484567/containers/98369876/tagdetails?apiLink=destination"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"G-2B7M8YQ1K6": {
"path": "accounts/6065484567/containers/98369876",
"accountId": "6065484567",
"containerId": "98369876",
"name": "example.com",
"publicId": "G-2B7M8YQ1K6",
"usageContext": [
"web"
],
"fingerprint": "1670406303456",
"tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/6065484567/containers/98369876/workspaces?apiLink=container",
"features": {
"supportUserPermissions": false,
"supportEnvironments": false,
"supportWorkspaces": false,
"supportGtagConfigs": true,
"supportBuiltInVariables": true,
"supportClients": false,
"supportFolders": false,
"supportTags": false,
"supportTemplates": false,
"supportTriggers": false,
"supportVariables": false,
"supportVersions": false,
"supportZones": false
},
"tagIds": [
"G-2B7M8YQ1K6",
"GT-NBQN9V2"
]
}
}
2 changes: 2 additions & 0 deletions assets/js/modules/analytics-4/datastore/__fixtures__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

export { default as accountSummaries } from './account-summaries.json';
export { default as container } from './container.json';
export { default as containerDestinations } from './container-destinations.json';
export { default as createProperty } from './create-property.json';
export { default as createWebDataStream } from './create-webdatastream.json';
export { default as defaultSettings } from './default-settings.json';
Expand Down
188 changes: 188 additions & 0 deletions assets/js/modules/analytics-4/datastore/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* `modules/analytics-4` data store: containers.
*
* Site Kit by Google, Copyright 2023 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.
*/

/**
* External dependencies
*/
import invariant from 'invariant';

/**
* Internal dependencies
*/
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { MODULES_ANALYTICS_4 } from './constants';
import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store';
import { createReducer } from '../../../googlesitekit/data/create-reducer';

const fetchGetGoogleTagContainerStore = createFetchStore( {
baseName: 'getGoogleTagContainer',
controlCallback( { measurementID } ) {
return API.get(
'modules',
'analytics-4',
'container-lookup',
{ destinationID: measurementID },
{
useCache: false,
}
);
},
reducerCallback: createReducer( ( state, container, { measurementID } ) => {
state.containers[ measurementID ] = container;
} ),
argsToParams( measurementID ) {
return { measurementID };
},
validateParams( { measurementID } = {} ) {
invariant( measurementID, 'measurementID is required.' );
},
} );

const fetchGetGoogleTagContainerDestinationsStore = createFetchStore( {
baseName: 'getGoogleTagContainerDestinations',
controlCallback( { gtmAccountID, gtmContainerID } ) {
return API.get(
'modules',
'analytics-4',
'container-destinations',
{ accountID: gtmAccountID, internalContainerID: gtmContainerID },
{
useCache: false,
}
);
},
reducerCallback: createReducer(
( state, containerDestinations, { gtmAccountID, gtmContainerID } ) => {
state.containerDestinations[ gtmAccountID ] =
state.containerDestinations[ gtmAccountID ] || {};

state.containerDestinations[ gtmAccountID ][ gtmContainerID ] =
state.containerDestinations[ gtmAccountID ][ gtmContainerID ] ||
[];

state.containerDestinations[ gtmAccountID ][ gtmContainerID ].push(
...containerDestinations
);
}
),
argsToParams( gtmAccountID, gtmContainerID ) {
return { gtmAccountID, gtmContainerID };
},
validateParams( { gtmAccountID, gtmContainerID } = {} ) {
invariant( gtmAccountID, 'gtmAccountID is required.' );
invariant( gtmContainerID, 'gtmContainerID is required.' );
},
} );

const baseInitialState = {
containers: {},
containerDestinations: {},
};

const baseActions = {};

const baseControls = {};

const baseReducer = ( state, { type } ) => {
switch ( type ) {
default: {
return state;
}
}
};

const baseResolvers = {
*getGoogleTagContainer( measurementID ) {
const registry = yield Data.commonActions.getRegistry();
const container = registry
.select( MODULES_ANALYTICS_4 )
.getGoogleTagContainer( measurementID );

if ( container === undefined ) {
yield fetchGetGoogleTagContainerStore.actions.fetchGetGoogleTagContainer(
measurementID
);
}
},

*getGoogleTagContainerDestinations( gtmAccountID, gtmContainerID ) {
const registry = yield Data.commonActions.getRegistry();
const containerDestinations = registry
.select( MODULES_ANALYTICS_4 )
.getGoogleTagContainerDestinations( gtmAccountID, gtmContainerID );

if ( containerDestinations === undefined ) {
yield fetchGetGoogleTagContainerDestinationsStore.actions.fetchGetGoogleTagContainerDestinations(
gtmAccountID,
gtmContainerID
);
}
},
};

const baseSelectors = {
/**
* Gets the Google Tag container for the given measurementID.
*
* @since n.e.x.t
*
* @param {Object} state Data store's state.
* @param {string} measurementID Measurement ID.
* @return {Object|undefined} Google Tag container object, or undefined if not loaded.
*/
getGoogleTagContainer( state, measurementID ) {
return state.containers[ measurementID ];
},

/**
* Gets the Google Tag container destinations for the given accountID and containeID.
*
* @since n.e.x.t
*
* @param {Object} state Data store's state.
* @param {string} gtmAccountID Google Tag Manager account ID.
* @param {string} gtmContainerID Google Tag Manager container ID.
* @return {(Array.<Object>|undefined)} Google Tag container destinations list, or undefined if not loaded.
*/
getGoogleTagContainerDestinations( state, gtmAccountID, gtmContainerID ) {
return state.containerDestinations[ gtmAccountID ]?.[ gtmContainerID ];
},
};

const store = Data.combineStores(
fetchGetGoogleTagContainerStore,
fetchGetGoogleTagContainerDestinationsStore,
{
initialState: baseInitialState,
actions: baseActions,
controls: baseControls,
reducer: baseReducer,
resolvers: baseResolvers,
selectors: baseSelectors,
}
);

export const initialState = store.initialState;
export const actions = store.actions;
export const controls = store.controls;
export const reducer = store.reducer;
export const resolvers = store.resolvers;
export const selectors = store.selectors;

export default store;
Loading