Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social: Add preview modal analytics #38714

Merged
merged 8 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added tracking for the publicize settings changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { useCallback } from 'react';
import usePublicizeConfig from '../../hooks/use-publicize-config';
import useSocialMediaConnections from '../../hooks/use-social-media-connections';
import PublicizeConnection from '../connection';
Expand All @@ -7,12 +9,22 @@ import styles from './styles.module.scss';
import { useConnectionState } from './use-connection-state';

export const ConnectionsList: React.FC = () => {
const { recordEvent } = useAnalytics();

const { connections, toggleById } = useSocialMediaConnections();

const { canBeTurnedOn, shouldBeDisabled } = useConnectionState();

const { needsUserConnection } = usePublicizeConfig();

const toggleConnection = useCallback(
( connectionId: string ) => () => {
toggleById( connectionId );
recordEvent( 'jetpack_social_connection_toggled', { location: 'editor' } );
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
},
[ recordEvent, toggleById ]
);

return (
<div>
<ul className={ styles[ 'connections-list' ] }>
Expand All @@ -28,7 +40,7 @@ export const ConnectionsList: React.FC = () => {
id={ currentId }
label={ display_name }
name={ service_name }
toggleConnection={ toggleById }
toggleConnection={ toggleConnection( currentId ) }
profilePicture={ profile_picture }
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export default function PublicizeForm() {

{ ! isPublicizeDisabledBySitePlan && (
<Fragment>
{ isPublicizeEnabled && hasEnabledConnections && <SharePostForm /> }
{ isPublicizeEnabled && hasEnabledConnections && (
<SharePostForm analyticsData={ { location: 'editor' } } />
) }
<AdvancedPlanNudge />
</Fragment>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@ import useSocialMediaMessage from '../../hooks/use-social-media-message';
import MediaSection from '../media-section';
import MessageBoxControl from '../message-box-control';

export const SharePostForm: React.FC = () => {
type SharePostFormProps = {
analyticsData?: object;
};

/**
* The SharePostForm component.
* @param {object} props - The component props.
* @param {object} [props.analyticsData] - Data for tracking analytics.
* @returns {object} The SharePostForm component.
*/
export const SharePostForm: React.FC< SharePostFormProps > = ( { analyticsData = null } ) => {
const { message, updateMessage, maxLength } = useSocialMediaMessage();

const { isEnhancedPublishingEnabled } = usePublicizeConfig();

return (
<>
<MessageBoxControl maxLength={ maxLength } onChange={ updateMessage } message={ message } />
{ isEnhancedPublishingEnabled && <MediaSection /> }
<MessageBoxControl
maxLength={ maxLength }
onChange={ updateMessage }
message={ message }
analyticsData={ analyticsData }
/>
{ isEnhancedPublishingEnabled && <MediaSection analyticsData={ analyticsData } /> }
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ThemeProvider, getRedirectUrl } from '@automattic/jetpack-components';
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { Disabled, ExternalLink, Notice, BaseControl } from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand All @@ -16,14 +17,17 @@ const ADD_MEDIA_LABEL = __( 'Choose Media', 'jetpack' );
* @param {boolean} [props.disabled=false] - Indicates whether the MediaSection is disabled or not.
* @param {string} [props.disabledNoticeMessage=''] - An optional notice that's displayed when the section is disabled.
* @param {import('react').ReactNode} [props.CustomNotice=null] - An optional custom notice that's displayed.
* @param {object} [props.analyticsData] - Data for tracking analytics.
* @returns {object} The media section.
*/
export default function MediaSection( {
disabled = false,
disabledNoticeMessage = '',
CustomNotice = null,
analyticsData,
} ) {
const { attachedMedia, updateAttachedMedia } = useAttachedMedia();
const { recordEvent } = useAnalytics();

const [ mediaDetails ] = useMediaDetails( attachedMedia[ 0 ]?.id );

Expand All @@ -32,11 +36,13 @@ export default function MediaSection( {
if ( ! media ) {
updateAttachedMedia( [] );
} else {
recordEvent( 'jetpack_social_media_attached', analyticsData );

const { id, url, mime: type } = media;
updateAttachedMedia( [ { id, url, type } ] );
}
},
[ updateAttachedMedia ]
[ analyticsData, recordEvent, updateAttachedMedia ]
);

const MediaWrapper = disabled ? Disabled : Fragment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { TextareaControl } from '@wordpress/components';
import { __, _n, sprintf } from '@wordpress/i18n';
import { useCallback, useState } from 'react';

/**
* Wrapper around a textbox to restrict the number of characters and
Expand All @@ -10,16 +12,37 @@ import { __, _n, sprintf } from '@wordpress/i18n';
* @param {Function} props.onChange - Callback to invoke as the message changes.
* @param {boolean} [props.disabled] - Whether the control is disabled.
* @param {number} props.maxLength - The maximum character length of the message.
* @param {object} props.analyticsData - Data for tracking analytics.
* @returns {object} The message box component.
*/
export default function MessageBoxControl( { message = '', onChange, disabled, maxLength } ) {
export default function MessageBoxControl( {
message = '',
onChange,
disabled,
maxLength,
analyticsData = null,
} ) {
const { recordEvent } = useAnalytics();
const [ isFirstChange, setIsFirstChange ] = useState( true );
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved

const charactersRemaining = maxLength - message.length;

const handleChange = useCallback(
newMessage => {
if ( isFirstChange ) {
recordEvent( 'jetpack_social_custom_message_changed', analyticsData );
setIsFirstChange( false );
}
onChange( newMessage );
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
},
[ analyticsData, isFirstChange, onChange, recordEvent ]
);

return (
<TextareaControl
value={ message }
label={ __( 'Message', 'jetpack' ) }
onChange={ onChange }
onChange={ handleChange }
disabled={ disabled }
maxLength={ maxLength }
placeholder={ __( 'Write a message for your audience here.', 'jetpack' ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { Modal, PanelRow, Button } from '@wordpress/components';
import { useReducer } from '@wordpress/element';
import { useCallback, useReducer } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { close } from '@wordpress/icons';
import { PreviewSection } from './preview-section';
Expand All @@ -13,6 +14,14 @@ import styles from './styles.module.scss';
*/
export function SocialPostModal() {
const [ isModalOpen, toggleModal ] = useReducer( state => ! state, false );
const { recordEvent } = useAnalytics();

const handleOpenModal = useCallback( () => {
if ( ! isModalOpen ) {
recordEvent( 'jetpack_social_preview_modal_opened' );
}
toggleModal();
}, [ isModalOpen, recordEvent ] );

return (
<PanelRow className={ styles.panel }>
Expand All @@ -35,7 +44,7 @@ export function SocialPostModal() {
/>
</Modal>
) }
<Button variant="secondary" onClick={ toggleModal }>
<Button variant="secondary" onClick={ handleOpenModal }>
{ __( 'Preview social posts', 'jetpack' ) }
</Button>
</PanelRow>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { TabPanel } from '@wordpress/components';
import { ToggleControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
Expand All @@ -16,6 +17,8 @@ import styles from './styles.module.scss';
* @returns {import('react').ReactNode} - Preview section of the social post modal.
*/
export function PreviewSection() {
const { recordEvent } = useAnalytics();

const getService = useService();

const { canBeTurnedOn, shouldBeDisabled } = useConnectionState();
Expand Down Expand Up @@ -64,8 +67,9 @@ export function PreviewSection() {
const toggleConnection = useCallback(
( connectionId: string ) => () => {
toggleConnectionById( connectionId );
recordEvent( 'jetpack_social_connection_toggled', { location: 'preview-modal' } );
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
},
[ toggleConnectionById ]
[ recordEvent, toggleConnectionById ]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function SettingsSection() {
<h2>{ __( 'Social Preview', 'jetpack' ) }</h2>
</div>
<div className={ styles[ 'settings-content' ] }>
<SharePostForm />
<SharePostForm analyticsData={ { location: 'preview-modal' } } />
</div>
</div>
);
Expand Down
Loading