Newsletter: add Tracks analytics to settings page#47100
Conversation
Use Tracks to track interactions on the newsletter settings page. Events capture the module toggle, settings toggles, section saves and link clicks. The type of site is recorded on all events. This breaks compatibility of event names for both the calypso and previous jetpack setting screens. The former as this is no longer calypso and is applicable more widely than just simple sites, and the latter as it uses a very generic naming system that is specific to the jetpack settings screen.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
There was a problem hiding this comment.
Pull request overview
This PR adds Tracks analytics instrumentation to the Newsletter settings wp-admin UI so user interactions can be measured consistently across site types (simple/atomic/jetpack).
Changes:
- Introduce a Newsletter settings analytics helper (
analytics.ts) and addsite_typeto tracked events. - Track key interactions: module toggle, specific setting changes, section saves, and relevant link/button clicks.
- Wire Tracks identity data from PHP (
Jetpack_Tracks_Client) and enqueue the Tracks script; add@automattic/jetpack-analyticsdependency.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| projects/packages/newsletter/src/settings/types.ts | Adds tracksUserData to the PHP-provided settings typings. |
| projects/packages/newsletter/src/settings/sections/welcome-email-section.tsx | Tracks “welcome email” section save clicks with site_type. |
| projects/packages/newsletter/src/settings/sections/subscriptions-section.tsx | Tracks subscriptions section saves; passes siteType into editor-link toggles. |
| projects/packages/newsletter/src/settings/sections/paid-newsletter-section.tsx | Tracks paid plans CTA clicks and varies button text based on plan state. |
| projects/packages/newsletter/src/settings/sections/newsletter-section.tsx | Tracks newsletter module toggle and “manage subscribers” link clicks. |
| projects/packages/newsletter/src/settings/sections/newsletter-categories-section.tsx | Tracks newsletter categories section save clicks. |
| projects/packages/newsletter/src/settings/sections/email-sender-settings-section.tsx | Tracks sender settings section save clicks. |
| projects/packages/newsletter/src/settings/index.tsx | Initializes analytics, tracks auto-saved setting changes, and passes plan state into Paid section. |
| projects/packages/newsletter/src/settings/components/toggle-with-link.tsx | Adds a hook to track “preview and edit” link clicks via onLinkClick. |
| projects/packages/newsletter/src/settings/analytics.ts | New centralized tracking helpers and getSiteType() utility. |
| projects/packages/newsletter/src/class-settings.php | Enqueues Tracks script and exposes Tracks identity data to JS. |
| projects/packages/newsletter/package.json | Adds @automattic/jetpack-analytics dependency. |
| projects/packages/newsletter/changelog/dotcom-15397-ensure-tracking-events-are-in-place | Adds changelog entry for analytics addition. |
| pnpm-lock.yaml | Locks the new workspace dependency. |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
projects/packages/newsletter/src/settings/sections/paid-newsletter-section.tsx
Outdated
Show resolved
Hide resolved
Code Coverage SummaryCoverage changed in 1 file.
|
Replace manually maintained BOOLEAN_SETTINGS and STRING_SETTINGS arrays with runtime typeof checks. New settings are now automatically tracked based on their value type without requiring code changes.
| @@ -0,0 +1,129 @@ | |||
| /** | |||
There was a problem hiding this comment.
I'm slightly unsure about this big utility file.
AFAICS, all these track* functions, with the exception of trackSectionSave, are used only once, and with scant opportunities of being reused elsewhere.
I'd probably just call analytics.tracks.recordEvent directly from the various event handlers, leaving just getSiteType as a shared function — which then might be nice to move somewhere it can be used across the repo.
There was a problem hiding this comment.
Fair enough, I was a bit on the fence about the big file myself.
getSiteType can be temporarily shifted elsewhere. I spotted an opportunity for some more code reuse across the package yesterday but didn't want to bloat this pr.
There was a problem hiding this comment.
AFAICS similar getSiteTypes are present in two other packages:
I don't know whether those isWoASite and isSimpleSite are available in our case, but not having to rely on jetpackSettings would let us call the function directly where it's needed, instead of prop-drilling. 🤔
didn't want to bloat this pr
Agreed! IMHO let's just see if there are opportunities to simplify this package, and consider extracting the function elsewhere in a nice-to-have follow up.
FWIW my comment here is mostly about having these events searchable directly, rather than finding the function first, and then having to look for where they are used. Very minor, anyway! 👍
There was a problem hiding this comment.
We don't need to extract this getSiteType anywhere, i'll just throw it in utilities or something for now.
I'll open a separate PR to make better use of the assets php package and script-data js package, they have utility functions like this, and already provide some of the data that window.jetpackNewsletterSettings does.
Replace single-use tracking wrapper functions with direct analytics.tracks.recordEvent calls. Keep only getSiteType as a utility function in the renamed utils.ts file.
| // Initialize analytics with user data | ||
| useEffect( () => { | ||
| const tracksUserData = jetpackSettings?.tracksUserData; | ||
| if ( tracksUserData && typeof tracksUserData === 'object' ) { |
There was a problem hiding this comment.
The check typeof tracksUserData === 'object' will incorrectly pass if tracksUserData is null (since typeof null === 'object' in JavaScript). While the PHP function returns false rather than null, it's better to be defensive. Consider using a more explicit check like tracksUserData && tracksUserData !== false or restructuring the condition to be more explicit about what's expected.
| // Initialize analytics with user data | ||
| useEffect( () => { | ||
| const tracksUserData = jetpackSettings?.tracksUserData; | ||
| if ( tracksUserData && typeof tracksUserData === 'object' ) { |
There was a problem hiding this comment.
The analytics initialization should verify that the required properties exist before calling analytics.initialize(). Following the pattern used in other parts of the codebase (e.g., projects/js-packages/idc/tools/tracking.jsx), add checks for userid and username properties using Object.hasOwn() to ensure they exist before passing them to the initialization function. This prevents potential runtime errors if the tracksUserData object structure is unexpected.
| if ( tracksUserData && typeof tracksUserData === 'object' ) { | |
| if ( | |
| tracksUserData && | |
| typeof tracksUserData === 'object' && | |
| Object.hasOwn( tracksUserData, 'userid' ) && | |
| Object.hasOwn( tracksUserData, 'username' ) | |
| ) { |
Fixes DOTCOM-15397
Proposed changes:
Jetpack_Tracks_ClientOther information:
Jetpack product discussion
peKye1-1Z1-p2
Does this pull request change what data or activity we track or use?
No - this consolidates existing tracking from the Jetpack and Calypso newsletter settings implementations into unified event names with the
jetpack_newsletter_*prefix.Testing instructions:
add_filter( 'jetpack_wp_admin_newsletter_settings_enabled', '__return_true' );pixel.wp.compixel.wp.comwith the expected event names and propertiessite_typeproperty