feat(analytics): auto-provision GA4 custom dimensions#4657
feat(analytics): auto-provision GA4 custom dimensions#4657
Conversation
Provisions Newspack's standard set of GA4 event-scoped custom dimensions on the publisher's connected property, so downstream analytics features (Gate Intelligence, reader segmentation) can query Newspack event parameters without publishers configuring GA4 manually. Dimensions are provisioned via Site Kit's authenticated Admin API client, so the API call is attributed to Site Kit's Google Cloud project (which already has analyticsadmin.googleapis.com enabled) and uses Site Kit's stored credentials. Auto-trigger fires when Site Kit's GA4 propertyID is first set or changes, via a single-shot background action. A WP-CLI command is also provided for manual runs and dry-run status checks. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds automated GA4 Admin API provisioning of Newspack’s standard event-scoped custom dimensions on the connected GA4 property (via Site Kit’s authenticated client), plus a WP-CLI command for manual/dry-run execution.
Changes:
- Extends the Site Kit Analytics wrapper with GA4 custom-dimension list/create methods (Admin API).
- Introduces a GA4 custom-dimensions provisioner that schedules a one-shot cron run when Site Kit’s GA4
propertyIDis set/changed. - Adds
wp newspack ga4-dimensions provision [--dry-run]CLI support.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| includes/plugins/google-site-kit/class-googlesitekitanalytics.php | Adds Admin API helpers to list/create GA4 custom dimensions. |
| includes/plugins/google-site-kit/class-ga4-custom-dimensions.php | Implements scheduling, status, and provisioning logic for Newspack’s GA4 custom dimensions set. |
| includes/cli/class-initializer.php | Registers the new GA4 dimensions WP-CLI command. |
| includes/cli/class-ga4-dimensions.php | Implements newspack ga4-dimensions provision (incl. --dry-run). |
| includes/class-newspack.php | Boots the GA4 provisioning feature by including the new provisioner class. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Explicitly include class-googlesitekitanalytics.php from the plugin bootstrap instead of relying on classmap autoload. - Guard is_array on Site Kit settings in the add-option callback. - Guard missing/empty customDimensions in the list response. - Merge previous run's created list only when property_id matches so switching properties doesn't carry over stale entries. - Restore previous user ID after wp_set_current_user via try/finally so we don't leak an authenticated identity into subsequent operations. - Drop the obsolete 50-dim cap docblock wording; the code now handles property-level rejections per-dimension.
The previous commit unconditionally included class-googlesitekitanalytics.php from the plugin bootstrap to satisfy a review suggestion, but the class extends Site Kit's Module base class — which doesn't exist in the test environment where Site Kit isn't active. Parsing the file at bootstrap fatals with "Class Google\Site_Kit\Core\Modules\Module not found". The lazy-load pattern was intentional: the file is only required when setup_sitekit_ga4() runs (after confirming Site Kit is active), and GA4_Custom_Dimensions::with_analytics_module() already guards on GOOGLESITEKIT_PLUGIN_MAIN_FILE before any class_exists() / autoload lookup, so the lazy approach is safe for both runtime and tests.
|
The dry run works, but when trying to actually create the dimensions with my standard sitekit connection, I get a bunch of |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Context on the 403 Site Kit's Analytics module requests only Newspack's own OAuth already holds Edge case: sites with neither Newspack OAuth nor SK edit still 403 – they need to reconnect Site Kit. @leogermani – you can run the following snippet to check if you have either auth: |
All Submissions:
Changes proposed in this Pull Request:
Automatically provisions Newspack's standard set of GA4 event-scoped custom dimensions (27 total, covering Gate Intelligence, reader identity, prompt attribution, checkout, and content context) on the publisher's connected GA4 property.
Triggered automatically when Site Kit's
googlesitekit_analytics-4_settingsoption gains apropertyIDor the property changes, via a single-shotwp_schedule_single_eventkeyed on the property ID. Also exposed aswp newspack ga4-dimensions provision[--dry-run]. Provisioning is idempotent (existing parameter names are skipped) and per-dimension failures are logged but don't abort the run. Thenewspack_ga4_dimensions_provisionedoption stores a summary of the last run keyed on property ID.Auth source: Newspack OAuth primary, Site Kit fallback
Site Kit's Analytics module only requests
analytics.readonlyby default;analytics.editis granted on-demand for SK-initiated write actions, so publishers who connected an existing GA4 property via Site Kit hit a 403 on the Admin API. Newspack's own Google OAuth (_newspack_google_oauth) already hasanalytics.editinGoogle_OAuth::REQUIRED_SCOPES, soGA4_Custom_Dimensions::with_admin_client()now prefers it (via a raw-HTTP client againstanalyticsadmin.googleapis.com/v1beta) and falls back to Site Kit when Newspack OAuth isn't configured or the call errors. Active path is surfaced as anAuth source:line in CLI output and in the summary option. Prerequisite:analyticsadmin.googleapis.comenabled on the Newspack OAuth proxy's GCP project (done, prod + staging). Edge case: sites with neither Newspack OAuth nor Site Kit edit-scope still 403 – they need to reconnect Site Kit to grant edit.Closes NPPM-2738.
How to test the changes in this Pull Request:
wp newspack ga4-dimensions provision --dry-run. It should error out with "No GA4 property ID configured in Site Kit."newspack_ga4_provision_dimensionsscheduled action appears (wp cron event list | grep ga4); wait for or manually trigger it. Verify all 27 Newspack dimensions exist in GA4 (Admin → Property → Custom definitions → Custom dimensions).wp newspack ga4-dimensions provisionand... --dry-run. Both should be no-ops (0 missing, every dimension reports as already existing).wp option get _newspack_google_oauthreturns a populated option) and Site Kit in readonly mode, runwp newspack ga4-dimensions provision --dry-run. Output should includeAuth source: newspackand successfully list existing event-scoped dimensions. On a site with Site Kit in edit mode and no Newspack OAuth, the same command should printAuth source: sitekit.Other information: