-
Notifications
You must be signed in to change notification settings - Fork 292
/
webdatastreams.js
565 lines (510 loc) · 14.9 KB
/
webdatastreams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**
* `modules/analytics-4` data store: webdatastreams.
*
* Site Kit by Google, Copyright 2021 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';
import { pick, difference } from 'lodash';
/**
* Internal dependencies
*/
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';
import { isValidPropertyID } from '../utils/validation';
const { createRegistryControl, createRegistrySelector } = Data;
const fetchGetWebDataStreamsStore = createFetchStore( {
baseName: 'getWebDataStreams',
controlCallback( { propertyID } ) {
return API.get(
'modules',
'analytics-4',
'webdatastreams',
{ propertyID },
{
useCache: false,
}
);
},
reducerCallback( state, webDataStreams, { propertyID } ) {
return {
...state,
webdatastreams: {
...state.webdatastreams,
[ propertyID ]: Array.isArray( webDataStreams )
? webDataStreams
: [],
},
};
},
argsToParams( propertyID ) {
return { propertyID };
},
validateParams( { propertyID } = {} ) {
invariant(
isValidPropertyID( propertyID ),
'A valid GA4 propertyID is required.'
);
},
} );
const fetchGetWebDataStreamsBatchStore = createFetchStore( {
baseName: 'getWebDataStreamsBatch',
controlCallback( { propertyIDs } ) {
return API.get(
'modules',
'analytics-4',
'webdatastreams-batch',
{ propertyIDs },
{
useCache: false,
}
);
},
reducerCallback( state, webDataStreams ) {
return {
...state,
webdatastreams: {
...state.webdatastreams,
...( webDataStreams || {} ),
},
};
},
argsToParams( propertyIDs ) {
return { propertyIDs };
},
validateParams( { propertyIDs } = {} ) {
invariant(
Array.isArray( propertyIDs ),
'GA4 propertyIDs must be an array.'
);
propertyIDs.forEach( ( propertyID ) => {
invariant(
isValidPropertyID( propertyID ),
'A valid GA4 propertyID is required.'
);
} );
},
} );
const fetchCreateWebDataStreamStore = createFetchStore( {
baseName: 'createWebDataStream',
controlCallback( { propertyID } ) {
return API.set( 'modules', 'analytics-4', 'create-webdatastream', {
propertyID,
} );
},
reducerCallback( state, webDataStream, { propertyID } ) {
return {
...state,
webdatastreams: {
...state.webdatastreams,
[ propertyID ]: [
...( state.webdatastreams[ propertyID ] || [] ),
webDataStream,
],
},
};
},
argsToParams( propertyID ) {
return { propertyID };
},
validateParams( { propertyID } = {} ) {
invariant(
isValidPropertyID( propertyID ),
'A valid GA4 propertyID is required.'
);
},
} );
// Actions
const WAIT_FOR_WEBDATASTREAMS = 'WAIT_FOR_WEBDATASTREAMS';
const baseInitialState = {
webdatastreams: {},
};
const baseActions = {
/**
* Creates a new GA4 web data stream.
*
* @since 1.31.0
*
* @param {string} propertyID GA4 property ID.
* @return {Object} Object with `response` and `error`.
*/
createWebDataStream: createValidatedAction(
( propertyID ) => {
invariant( propertyID, 'GA4 propertyID is required.' );
},
function* ( propertyID ) {
const { response, error } =
yield fetchCreateWebDataStreamStore.actions.fetchCreateWebDataStream(
propertyID
);
return { response, error };
}
),
/**
* Matches web data stream for provided property.
*
* @since 1.38.0
*
* @param {string} propertyID GA4 property ID.
* @return {Object|null} Matched web data stream object on success, otherwise NULL.
*/
*matchWebDataStream( propertyID ) {
yield baseActions.waitForWebDataStreams( propertyID );
const registry = yield Data.commonActions.getRegistry();
return registry
.select( MODULES_ANALYTICS_4 )
.getMatchingWebDataStreamByPropertyID( propertyID );
},
/**
* Waits for web data streams to be loaded for a property.
*
* @since 1.31.0
*
* @param {string} propertyID GA4 property ID.
*/
*waitForWebDataStreams( propertyID ) {
yield {
payload: { propertyID },
type: WAIT_FOR_WEBDATASTREAMS,
};
},
};
const baseControls = {
[ WAIT_FOR_WEBDATASTREAMS ]: createRegistryControl(
( { __experimentalResolveSelect } ) => {
return async ( { payload } ) => {
const { propertyID } = payload;
await __experimentalResolveSelect(
MODULES_ANALYTICS_4
).getWebDataStreams( propertyID );
};
}
),
};
const baseReducer = ( state, { type } ) => {
switch ( type ) {
default: {
return state;
}
}
};
const baseResolvers = {
*getWebDataStreams( propertyID ) {
const registry = yield Data.commonActions.getRegistry();
// Only fetch web data streams if there are none in the store for the given property.
const webdatastreams = registry
.select( MODULES_ANALYTICS_4 )
.getWebDataStreams( propertyID );
if ( webdatastreams === undefined ) {
yield fetchGetWebDataStreamsStore.actions.fetchGetWebDataStreams(
propertyID
);
}
},
*getWebDataStreamsBatch( propertyIDs ) {
const registry = yield Data.commonActions.getRegistry();
const webdatastreams =
registry
.select( MODULES_ANALYTICS_4 )
.getWebDataStreamsBatch( propertyIDs ) || {};
const availablePropertyIDs = Object.keys( webdatastreams );
const remainingPropertyIDs = difference(
propertyIDs,
availablePropertyIDs
);
if ( remainingPropertyIDs.length > 0 ) {
for (
let i = 0;
i < remainingPropertyIDs.length;
i += MAX_WEBDATASTREAMS_PER_BATCH
) {
const chunk = remainingPropertyIDs.slice(
i,
i + MAX_WEBDATASTREAMS_PER_BATCH
);
yield fetchGetWebDataStreamsBatchStore.actions.fetchGetWebDataStreamsBatch(
chunk
);
}
}
},
};
const baseSelectors = {
/**
* Gets all GA4 web data streams this account can access.
*
* @since 1.31.0
*
* @param {Object} state Data store's state.
* @param {string} propertyID The GA4 property ID to fetch web data streams for.
* @return {(Array.<Object>|undefined)} An array of GA4 web data streams; `undefined` if not loaded.
*/
getWebDataStreams( state, propertyID ) {
return state.webdatastreams[ propertyID ];
},
/**
* Gets matched web data stream from a list of web data streams.
*
* @since 1.31.0
* @since 1.90.0 Updated to match a data stream from a list of provided web data streams.
*
* @param {Object} state Data store's state.
* @param {Array} datastreams A list of web data streams.
* @return {(Object|null)} A web data stream object if found, otherwise null.
*/
getMatchingWebDataStream: createRegistrySelector(
( select ) => ( state, datastreams ) => {
const matchedWebDataStreams =
select( MODULES_ANALYTICS_4 ).getMatchingWebDataStreams(
datastreams
);
return matchedWebDataStreams[ 0 ] || null;
}
),
/**
* Gets web data streams that match the current reference URL.
*
* @since 1.98.0
*
* @param {Object} state Data store's state.
* @param {Array} datastreams A list of web data streams.
* @return {Array.<Object>} An array of found matched web data streams.
*/
getMatchingWebDataStreams: createRegistrySelector(
( select ) => ( state, datastreams ) => {
invariant(
Array.isArray( datastreams ),
'datastreams must be an array.'
);
const matchedWebDataStreams = [];
for ( const datastream of datastreams ) {
if (
select( CORE_SITE ).isSiteURLMatch(
// eslint-disable-next-line sitekit/acronym-case
datastream.webStreamData?.defaultUri
)
) {
matchedWebDataStreams.push( datastream );
}
}
return matchedWebDataStreams;
}
),
/**
* Gets matched web data stream for selected property.
*
* @since 1.90.0
*
* @param {Object} state Data store's state.
* @param {string} propertyID The GA4 property ID to find matched web data stream.
* @return {(Object|null|undefined)} A web data stream object if found, otherwise null; `undefined` if web data streams are not loaded.
*/
getMatchingWebDataStreamByPropertyID: createRegistrySelector(
( select ) => ( _state, propertyID ) => {
const datastreams =
select( MODULES_ANALYTICS_4 ).getWebDataStreams( propertyID );
if ( datastreams === undefined ) {
return undefined;
}
const matchingDataStream =
select( MODULES_ANALYTICS_4 ).getMatchingWebDataStream(
datastreams
);
return matchingDataStream || null;
}
),
/**
* Gets web data streams in batch for selected properties.
*
* @since 1.32.0
*
* @param {Object} state Data store's state.
* @param {Array.<string>} propertyIDs GA4 property IDs.
* @return {Object} Web data streams.
*/
getWebDataStreamsBatch( state, propertyIDs ) {
return pick( state.webdatastreams, propertyIDs );
},
/**
* Gets the matched measurement IDs for the given property IDs.
*
* @since 1.88.0
* @since 1.90.0 Use propertyIDs instead of property objects for matching.
*
* @param {Object} state Data store's state.
* @param {Array.<string>} propertyIDs GA4 property IDs array of strings.
* @return {(Object|undefined)} Object with matched property ID as the key and measurement ID as the value, or an empty object if no matches are found; `undefined` if web data streams are not loaded.
*/
getMatchedMeasurementIDsByPropertyIDs: createRegistrySelector(
( select ) => ( _state, propertyIDs ) => {
invariant(
Array.isArray( propertyIDs ) && propertyIDs?.length,
'propertyIDs must be an array containing GA4 propertyIDs.'
);
propertyIDs.forEach( ( propertyID ) => {
invariant(
isValidPropertyID( propertyID ),
'A valid GA4 propertyID is required.'
);
} );
const datastreams =
select( MODULES_ANALYTICS_4 ).getWebDataStreamsBatch(
propertyIDs
);
if ( datastreams === undefined ) {
return undefined;
}
return propertyIDs.reduce( ( acc, currentPropertyID ) => {
if ( ! datastreams[ currentPropertyID ]?.length ) {
return acc;
}
const matchingDataStream = select(
MODULES_ANALYTICS_4
).getMatchingWebDataStream( datastreams[ currentPropertyID ] );
if ( ! matchingDataStream ) {
return acc;
}
const measurementID =
// eslint-disable-next-line sitekit/acronym-case
matchingDataStream.webStreamData.measurementId;
return {
...acc,
[ currentPropertyID ]: measurementID,
};
}, {} );
}
),
/**
* Gets an Analytics config that matches one of provided measurement IDs.
*
* @since 1.94.0
*
* @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;
}
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const datastreams =
select( MODULES_ANALYTICS_4 ).getWebDataStreamsBatch(
propertyIDs
);
const resolvedDataStreams = select(
MODULES_ANALYTICS_4
).hasFinishedResolution( 'getWebDataStreamsBatch', [
propertyIDs,
] );
// Return undefined if web data streams haven't been resolved yet.
if ( ! resolvedDataStreams ) {
return undefined;
}
let firstlyFoundConfig;
for ( const propertyID in datastreams ) {
if ( ! datastreams[ propertyID ]?.length ) {
continue;
}
for ( const datastream of datastreams[ propertyID ] ) {
const { _id: webDataStreamID, webStreamData } = datastream;
const {
defaultUri: defaultURI,
measurementId: measurementID, // eslint-disable-line sitekit/acronym-case
} = webStreamData;
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(
fetchGetWebDataStreamsStore,
fetchGetWebDataStreamsBatchStore,
fetchCreateWebDataStreamStore,
{
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;