Skip to content

feat(premium-analytics): add site-sync package into analytics - #49267

Merged
chihsuan merged 15 commits into
trunkfrom
wooa7s-1321-integrate-site-sync-package-into-analytics
Jun 16, 2026
Merged

feat(premium-analytics): add site-sync package into analytics#49267
chihsuan merged 15 commits into
trunkfrom
wooa7s-1321-integrate-site-sync-package-into-analytics

Conversation

@chihsuan

@chihsuan chihsuan commented May 29, 2026

Copy link
Copy Markdown
Member

Proposed changes

Adds a site-sync internal package to @automattic/jetpack-premium-analytics and configures apiFetch auth in the init module. Provides useSyncStatus — a React hook that polls Jetpack's native sync endpoints, exposes analytics-scoped progress, completion gating, and stall detection for the analytics dashboard.

This is a redesign, not a straight port of WooCommerce-Analytics' @wc-analytics/site-sync. The original hit a single custom WooCommerce REST endpoint; that PHP was split out separately (#49211, now merged), so the JS composes three Jetpack-native sources.

Data sources (replacing the original single endpoint)

Field Source
initial_full_sync_finished (milestone) JetpackScriptData.premium_analytics.initial_full_sync_finished (int) — page-load seed, refreshed live from /sync/status (#49602)
started / finished / per-module progress poll GET /jetpack/v4/sync/status
analytics percentage computed client-side from progress.woocommerce_analytics.{sent,total}
trigger POST /jetpack/v4/sync/full-sync

Adaptations from @wc-analytics/site-sync

Original Here Why
GET /wc/v3/woocommerce-analytics/sync-status GET /jetpack/v4/sync/status + client-side % Custom WC endpoint not ported; consume Jetpack core directly
POST .../sync-trigger POST /jetpack/v4/sync/full-sync No duplicate trigger endpoint (per backend WOOA7S-1436)
isConnected field dropped Pure passthrough — nothing branched on it; avoids a connection dep
milestone re-read every poll seeded from script-data, refreshed live from the poll Backend persists it; injected via script-data and surfaced on /sync/status (#49602)
text domain woocommerce-analytics jetpack-premium-analytics Monorepo convention
symlink build hack wp-build packages/* autodiscovery Internal-package convention (no link: self-dep, matching datetime/data)

What's intentionally not here

Parent-level wiring (projects/packages/premium-analytics/)

The nested packages/* are intentionally not pnpm workspace members; their runtime deps are declared on the parent package.json so the build resolves them (same model the README documents and datetime/data use). This PR adds:

  • dependencies: @automattic/jetpack-script-data (consumed by both init and site-sync).
  • devDependencies: @testing-library/react (the useSyncStatus hook test uses renderHook).

Does this pull request change what data or activity we track or use?

No.

Testing instructions

Requires Node 24 (repo engineStrict).

pnpm install
cd projects/packages/premium-analytics
pnpm test         # jest — 121 tests pass (incl. site-sync status helpers + useSyncStatus hook)
pnpm typecheck    # tsgo --noEmit — passes
pnpm exec eslint . # clean
pnpm build        # wp-build — passes

Quick visual test (optional — via companion branch)

The review-only branch wooa7s-1321-site-sync-test-harness-v2 wires useSyncStatus into the dashboard as a debug panel + Trigger full sync button. The real woocommerce_analytics sync module isn't registered yet, so it points the watched module (and the backend milestone filter) at posts for real progress. Do not merge.

git fetch origin wooa7s-1321-site-sync-test-harness-v2 && git checkout wooa7s-1321-site-sync-test-harness-v2
pnpm install && cd projects/packages/premium-analytics && pnpm build
wp post generate --count=1500

On a Jetpack-connected site, open the dashboard and click Trigger full sync: percentage climbs 0 → 100, isRunning flips true → false, isComplete becomes true, and initialFullSyncFinished flips 0 → <timestamp> live (#49602, no reload).

Screen.Recording.2026-06-15.at.1.49.38.PM.mov

Note: the @jetpack-premium-analytics/site-sync specifier is a temporary bridge inherited from the source repo. Once name-based identity (WordPress/gutenberg#78822, mirrored monorepo-wide by #48089) lands, the specifier becomes the package's own name (@automattic/jetpack-premium-analytics-site-sync) and the bare-scope alias/link: drop out — so long term there's no @jetpack-premium-analytics/... to maintain. The package's canonical name is @automattic/jetpack-premium-analytics-site-sync.

@github-actions

github-actions Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the wooa7s-1321-integrate-site-sync-package-into-analytics branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack wooa7s-1321-integrate-site-sync-package-into-analytics
bin/jetpack-downloader test jetpack-mu-wpcom-plugin wooa7s-1321-integrate-site-sync-package-into-analytics

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

github-actions Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label May 29, 2026
@chihsuan chihsuan self-assigned this May 29, 2026
@chihsuan
chihsuan marked this pull request as draft May 29, 2026 06:50
@simison

simison commented May 29, 2026

Copy link
Copy Markdown
Member

Would it make sense to rather have this be as part of the sync module as a feature? cc @Automattic/jetpack-vulcan who maintain sync.

@chihsuan

Copy link
Copy Markdown
Member Author

Would it make sense to rather have this be as part of the sync module as a feature? cc @Automattic/jetpack-vulcan who maintain sync.

Good call-out, thanks! I The way I see it, the package/hook is really doing two things: the generic part (polling /sync/status, kicking off a full sync, the polling loop + stall detection) and the analytics-specific part (scoping progress to the woocommerce_analytics module and reading the premium_analytics milestone from script-data).

The analytics-specific stuff pretty clearly belongs here. The generic part is the interesting question. It'd be reusable, so sync could be a natural home. The only snag is that packages/sync is PHP-only right now, so we'd be standing up a brand-new JS layer there just for this one consumer.

If @Automattic/jetpack-vulcan want to own a shared sync-status/trigger hook in sync, I'm genuinely happy with that.

@simison

simison commented May 29, 2026

Copy link
Copy Markdown
Member

Seems like a good opportunity for projects/js-packages/sync then for generic parts but really up to Vulcan team.

Can be easily done in follow-up PR btw, too.

@fgiannar

Copy link
Copy Markdown
Contributor

Thank you both for bringing this to our attention!

If @Automattic/jetpack-vulcan want to own a shared sync-status/trigger hook in sync, I'm genuinely happy with that.

For now we don't plan to add a JS layer in the sync package, however this could indeed be implemented as part of the following two potential future projects:

  • pf5801-1If-p2
  • pghT6q-9Z-p2

Actually, when we were brainstorming on the first one we did mention that we could potentially "steal" parts of your implementation, since you are the only plugin that exposes a Sync related UI. For now, feel free to proceed with this approach and we can refactor in the future as needed, ideally without duplicating the effort.

@jp-launch-control

jp-launch-control Bot commented Jun 2, 2026

Copy link
Copy Markdown

Code Coverage Summary

This PR did not change code coverage!

That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷

Full summary · PHP report · JS report

Base automatically changed from wooa7s-1320-configure-workspace-and-typescript-for-internal-packages to trunk June 5, 2026 02:37
@chihsuan
chihsuan force-pushed the wooa7s-1321-integrate-site-sync-package-into-analytics branch 2 times, most recently from 5d21be9 to 10783a2 Compare June 10, 2026 07:04
@chihsuan
chihsuan marked this pull request as ready for review June 10, 2026 07:12
@chihsuan
chihsuan force-pushed the wooa7s-1321-integrate-site-sync-package-into-analytics branch from 10783a2 to 0bf911c Compare June 10, 2026 07:28
@chihsuan
chihsuan requested review from a team, kangzj, louwie17 and retrofox June 10, 2026 08:01
…e-site-sync-package-into-analytics

# Conflicts:
#	pnpm-lock.yaml
#	projects/packages/premium-analytics/package.json
@chihsuan
chihsuan requested a review from a team June 15, 2026 02:56
@chihsuan
chihsuan changed the base branch from trunk to wooa7s-1542-expose-sync-milestone-live June 15, 2026 05:06
chihsuan added 5 commits June 15, 2026 13:24
…) into dashboard

DO NOT MERGE. Testing-only branch for PR #49267. Wires useSyncStatus into the dashboard route (debug panel + trigger). The real woocommerce_analytics sync module is not registered yet, so points ANALYTICS_SYNC_MODULE and the jetpack_premium_analytics_sync_modules milestone filter at posts so reviewers can exercise the flow. Adds a temporary link: dep on site-sync.
…ad of stopping

A single failed /sync/status fetch tore down polling permanently; recovery
required a full-sync re-trigger. Keep polling through transient errors and
only give up after MAX_POLL_FAILURES consecutive failures. Add tests for
continued polling, unmount cleanup, triggerSync resume, and error self-heal.
…egistration

init() registered root-URL/nonce middleware onto the shared apiFetch chain on
every call. Latch behind a module flag so re-mounts or a second boot don't
stack duplicates; only latch once registration actually happened.
chihsuan added a commit that referenced this pull request Jun 15, 2026
…) into dashboard

DO NOT MERGE. Testing-only branch for PR #49267. Wires useSyncStatus into the dashboard route (debug panel + trigger). The real woocommerce_analytics sync module is not registered yet, so points ANALYTICS_SYNC_MODULE and the jetpack_premium_analytics_sync_modules milestone filter at posts so reviewers can exercise the flow. Adds a temporary link: dep on site-sync.
@chihsuan chihsuan added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jun 15, 2026
@chihsuan chihsuan changed the title feat(premium-analytics): integrate site-sync package into analytics feat(premium-analytics): add site-sync package into analytics Jun 15, 2026
louwie17
louwie17 previously approved these changes Jun 15, 2026

@louwie17 louwie17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tested well, and code looks good!
I did leave one inline comment with something Claude pointed out, at initial glance it seems not super likely, but figured I mention it. ( I didn't have time to dig into it further ). But for the sake of this PR, I think we can merge this and address it as a follow up if you think it makes sense.

return;
}

if ( isSyncStalled( status ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude seemed to think this could potentially fall short because of this, I don't have quite enough time to dig into this further today, but figured I mention it now:

[suggestion] Stall detection can misfire on a normally-completing sync and stop polling irrecoverably.

  use-sync-status.ts:83-89 clears the polling interval and surfaces a "Sync has stalled" error whenever a poll returns isStarted && !isRunning && !isSyncComplete. And isSyncComplete requires
  both percentage >= 100 and milestone > 0 (status.ts:42-43).

  The problem is that those two inputs come from different backend signals:
  - finished (→ isRunning) reflects Jetpack core's full-sync module state, returned by /jetpack/v4/sync/status.
  - initial_full_sync_finished (the milestone) is set separately in Sync_Status_Tracker::on_sync_processed_actions — only once the jetpack_full_sync_end action is confirmed processed — and then
  bolted onto the same response via enrich_sync_status_response (rest_post_dispatch).

  They ride on the same HTTP response, but they don't share a source of truth. So if any poll observes finished: true before the milestone option has flipped, the hook computes: percentage 100,
  isRunning false, isComplete false (milestone still 0) → isStalled = true → it calls clearPolling() and tears down polling permanently. The very next poll that would have carried the milestone
  never runs. The user is left on a false "stalled" error until they manually hit retry (triggerSync).

  Why it may or may not reproduce: with Full_Sync_Immediately (inline action processing), the module-finished flip and the jetpack_sync_processed_actions fire in the same cycle, so milestone and
  finished likely land together — no gap. With the legacy queue-based path, or any lag between "module marked finished" and "end-action processed," the window opens.

  Suggested fixes (either):
  - Don't clear polling on the first stalled observation — require N consecutive stalls, mirroring the MAX_POLL_FAILURES pattern the error path already uses.
  - Or treat finished && milestone === 0 as "finishing, keep polling" rather than stalled, so a one-poll lag self-heals.

@chihsuan chihsuan Jun 16, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did leave one inline comment with something Claude pointed out, at initial glance it seems not super likely, but figured I mention it. ( I didn't have time to dig into it further ). But for the sake of this PR, I think we can merge this and address it as a follow up if you think it makes sense

Thanks for flagging this! @louwie17 Yes, I dug into the actual flow, and I think we're safe on the path that ships.

On the default path it can't happen but happy to do it as a follow-up.

Base automatically changed from wooa7s-1542-expose-sync-milestone-live to trunk June 16, 2026 01:46
@chihsuan
chihsuan dismissed louwie17’s stale review June 16, 2026 01:46

The base branch was changed.

@chihsuan
chihsuan merged commit 66f9ae0 into trunk Jun 16, 2026
96 of 98 checks passed
@chihsuan
chihsuan deleted the wooa7s-1321-integrate-site-sync-package-into-analytics branch June 16, 2026 02:15
@github-actions github-actions Bot added [Status] UI Changes Add this to PRs that change the UI so documentation can be updated. and removed [Status] In Progress [Status] Needs Review This PR is ready for review. labels Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Premium Analytics [Status] UI Changes Add this to PRs that change the UI so documentation can be updated.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants