Skip to content
Draft
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
63 changes: 63 additions & 0 deletions assets/js/googlesitekit-admin-post-list-ga4-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Site Kit by Google, Copyright 2026 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 {
chunkArray,
parseRowsToPathMap,
} from './googlesitekit-admin-post-list-ga4-helpers';

describe( 'chunkArray', () => {
it( 'returns empty array for empty input', () => {
expect( chunkArray( [], 3 ) ).toStrictEqual( [] );
} );

it( 'chunks into fixed sizes', () => {
expect( chunkArray( [ 1, 2, 3, 4, 5 ], 2 ) ).toStrictEqual( [
[ 1, 2 ],
[ 3, 4 ],
[ 5 ],
] );
} );
} );

describe( 'parseRowsToPathMap', () => {
it( 'returns empty object when rows missing', () => {
expect( parseRowsToPathMap( {} ) ).toStrictEqual( {} );
expect( parseRowsToPathMap( { rows: [] } ) ).toStrictEqual( {} );
} );

it( 'maps dimension to first metric value', () => {
const report = {
rows: [
{
dimensionValues: [ { value: '/foo' } ],
metricValues: [ { value: '10' } ],
},
{
dimensionValues: [ { value: '/bar?x=1' } ],
metricValues: [ { value: '3' } ],
},
],
};
expect( parseRowsToPathMap( report ) ).toStrictEqual( {
'/foo': '10',
'/bar?x=1': '3',
} );
} );
} );
81 changes: 81 additions & 0 deletions assets/js/googlesitekit-admin-post-list-ga4-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Site Kit by Google, Copyright 2026 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.
*/

/**
* Minimal GA4 report row shape from `get( 'modules', …, 'report', … )`.
*
* @since n.e.x.t
*/
export interface ReportRow {
dimensionValues?: Array< { value?: string } >;
metricValues?: Array< { value?: string } >;
}

/**
* Report payload accepted by {@link parseRowsToPathMap}.
*
* @since n.e.x.t
*/
export interface ReportPayload {
rows?: ReportRow[];
}

/**
* Splits an array into chunks of at most `size` elements.
*
* @since n.e.x.t
*
* @param arr Input array.
* @param size Chunk size (positive integer).
* @return Chunk arrays.
*/
export function chunkArray< T >( arr: T[], size: number ): T[][] {
const out: T[][] = [];
for ( let index = 0; index < arr.length; index += size ) {
out.push( arr.slice( index, index + size ) );
}
return out;
}

/**
* Maps GA4 report rows to path → first metric value.
*
* @since n.e.x.t
*
* @param report Report payload from `get()`.
* @return Path keys to metric value strings.
*/
export function parseRowsToPathMap(
report: ReportPayload | null | undefined
): Record< string, string > {
const map: Record< string, string > = {};
if ( ! report?.rows?.length ) {
return map;
}
for ( const row of report.rows ) {
const dimValue = row.dimensionValues?.[ 0 ]?.value;
const metValue = row.metricValues?.[ 0 ]?.value;
if (
dimValue !== undefined &&
dimValue !== null &&
metValue !== undefined &&
metValue !== null
) {
map[ dimValue ] = metValue;
}
}
return map;
}
161 changes: 161 additions & 0 deletions assets/js/googlesitekit-admin-post-list-ga4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* Site Kit by Google, Copyright 2026 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.
*/

/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready';

/**
* External dependencies
*/
import Data from 'googlesitekit-data';
import { get } from 'googlesitekit-api';

/**
* Internal dependencies
*/
import { CORE_USER } from '@/js/googlesitekit/datastore/user/constants';
import { DATE_RANGE_OFFSET } from '@/js/modules/analytics-4/datastore/constants';
import { numFmt } from '@/js/util/i18n';
import {
chunkArray,
parseRowsToPathMap,
type ReportPayload,
} from './googlesitekit-admin-post-list-ga4-helpers';

const CHUNK_SIZE = 80;

/**
* Inline config from PHP `Script_Data` (`googlesitekit-admin-post-list-ga4-data` → `_googlesitekitPostListGA4Data`).
*
* @since n.e.x.t
*/
export interface PostListGA4Config {
metric?: string;
dateRangeSlug?: string;
moduleSlug?: string;
}

declare global {
interface Window {
_googlesitekitPostListGA4Data?: PostListGA4Config;
}
}

function getConfig(): PostListGA4Config {
if ( typeof window === 'undefined' ) {
return {};
}
return window._googlesitekitPostListGA4Data ?? {};
}

function collectPaths(): string[] {
const spans = document.querySelectorAll(
'.googlesitekit-post-list-ga4-views[data-page-path]'
);
const paths = new Set< string >();
spans.forEach( ( element ) => {
const p = element.getAttribute( 'data-page-path' );
if ( p ) {
paths.add( p );
}
} );
return Array.from( paths );
}

async function fetchReportForPaths(
paths: string[],
metric: string,
startDate: string,
endDate: string,
moduleSlug: string
): Promise< Record< string, string > > {
const merged: Record< string, string > = {};
for ( const batch of chunkArray( paths, CHUNK_SIZE ) ) {
if ( ! batch.length ) {
continue;
}
const report = ( await get( 'modules', moduleSlug, 'report', {
dimensions: [ { name: 'pagePathPlusQueryString' } ],
metrics: [ { name: metric } ],
dimensionFilters: {
pagePathPlusQueryString: batch,
},
startDate,
endDate,
limit: Math.min( batch.length, 1000 ),
} ) ) as ReportPayload;
Object.assign( merged, parseRowsToPathMap( report ) );
}
return merged;
}

function applyValuesToDom( pathToValue: Record< string, string > ): void {
document
.querySelectorAll(
'.googlesitekit-post-list-ga4-views[data-page-path]'
)
.forEach( ( element ) => {
const path = element.getAttribute( 'data-page-path' );
if ( ! path ) {
return;
}
const raw = pathToValue[ path ];
if ( raw === undefined ) {
element.textContent = numFmt( 0 );
return;
}
const num = parseFloat( raw );
if ( Number.isNaN( num ) ) {
element.textContent = String( raw );
} else {
element.textContent = numFmt( num );
}
} );
}

domReady( async () => {
const cfg = getConfig();
const { metric, dateRangeSlug, moduleSlug } = cfg;
if ( ! metric || ! dateRangeSlug || ! moduleSlug ) {
return;
}

const paths = collectPaths();
if ( ! paths.length ) {
return;
}

try {
Data.dispatch( CORE_USER ).setDateRange( dateRangeSlug );
const { startDate, endDate } = Data.select(
CORE_USER
).getDateRangeDates( { offsetDays: DATE_RANGE_OFFSET } );

const pathToValue = await fetchReportForPaths(
paths,
metric,
startDate,
endDate,
moduleSlug
);

applyValuesToDom( pathToValue );
} catch {
// Keep loading placeholder on failure.
}
} );
2 changes: 2 additions & 0 deletions assets/webpack/modules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ module.exports = function ( mode, rules ) {
'./js/googlesitekit-metric-selection.js',
'googlesitekit-key-metrics-setup':
'./js/googlesitekit-key-metrics-setup.js',
'googlesitekit-admin-post-list-ga4':
'./js/googlesitekit-admin-post-list-ga4.ts',
// Old Modules
'googlesitekit-activation': './js/googlesitekit-activation.js',
'googlesitekit-adminbar': './js/googlesitekit-adminbar.js',
Expand Down
3 changes: 1 addition & 2 deletions includes/Core/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ function ( $tag, $handle ) {
'admin_print_scripts-edit.php',
function () {
global $post_type;
if ( 'post' !== $post_type ) {
// For CONTEXT_ADMIN_POSTS we only load scripts for the 'post' post type.
if ( empty( $post_type ) || ! is_string( $post_type ) || ! is_post_type_viewable( $post_type ) ) {
return;
}
$assets = $this->get_assets();
Expand Down
Loading
Loading