Skip to content

Commit

Permalink
Add event tracking to log how many times and what fields the store ad…
Browse files Browse the repository at this point in the history
…dress is having validation errors.
  • Loading branch information
eason9487 committed Jul 26, 2023
1 parent 79a11af commit 99408e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
46 changes: 41 additions & 5 deletions js/src/components/contact-information/store-address-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';
import { useRef, createInterpolateElement } from '@wordpress/element';
import { CardDivider } from '@wordpress/components';
import { Spinner } from '@woocommerce/components';
import { update as updateIcon } from '@wordpress/icons';
import { getPath, getQuery } from '@woocommerce/navigation';
import { recordEvent } from '@woocommerce/tracks';

/**
* Internal dependencies
Expand All @@ -31,10 +32,21 @@ import './store-address-card.scss';
* @property {string|undefined} [subpath] The subpath used in the page, e.g. `"/edit-store-address"` or `undefined` when there is no subpath.
*/

/**
* Track how many times and what fields the store address is having validation errors.
*
* @event gla_wc_store_address_validation
* @property {string} path The path used in the page from which the event tracking was sent, e.g. `"/google/setup-mc"` or `"/google/settings"`.
* @property {string|undefined} [subpath] The subpath used in the page, e.g. `"/edit-store-address"` or `undefined` when there is no subpath.
* @property {string} country_code The country code of store address, e.g. `"US"`.
* @property {string} missing_fields The string of the missing required fields of store address separated by comma, e.g. `"city,postcode"`.
*/

/**
* Renders a component with a given store address.
*
* @fires gla_edit_wc_store_address Whenever "Edit in WooCommerce Settings" button is clicked.
* @fires gla_wc_store_address_validation Whenever the new store address data is fetched after clicking "Refresh to sync" button.
*
* @param {Object} props React props.
* @param {boolean} [props.showValidation=false] Whether to show validation error messages.
Expand All @@ -43,15 +55,39 @@ import './store-address-card.scss';
*/
const StoreAddressCard = ( { showValidation = false } ) => {
const { loaded, data, refetch } = useStoreAddress();
const path = getPath();
const { subpath } = getQuery();
const editButton = (

const refetchedCallbackRef = useRef( null );

if ( loaded && refetchedCallbackRef.current ) {
refetchedCallbackRef.current( data );
refetchedCallbackRef.current = null;
}

const handleRefreshClick = () => {
refetch();

refetchedCallbackRef.current = ( storeAddress ) => {
const eventProps = {
path,
subpath,
country_code: storeAddress.countryCode,
missing_fields: storeAddress.missingRequiredFields.join( ',' ),
};

recordEvent( 'gla_wc_store_address_validation', eventProps );
};
};

const refreshButton = (
<AppButton
isSecondary
icon={ updateIcon }
iconSize={ 20 }
iconPosition="right"
text={ __( 'Refresh to sync', 'google-listings-and-ads' ) }
onClick={ refetch }
onClick={ handleRefreshClick }
disabled={ ! loaded }
/>
);
Expand All @@ -72,7 +108,7 @@ const StoreAddressCard = ( { showValidation = false } ) => {
type="external"
href="admin.php?page=wc-settings"
eventName="gla_edit_wc_store_address"
eventProps={ { path: getPath(), subpath } }
eventProps={ { path, subpath } }
/>
),
}
Expand Down Expand Up @@ -113,7 +149,7 @@ const StoreAddressCard = ( { showValidation = false } ) => {
alignIcon="top"
alignIndicator="top"
description={ description }
indicator={ editButton }
indicator={ refreshButton }
>
<CardDivider />
<Section.Card.Body>
Expand Down
4 changes: 3 additions & 1 deletion js/src/hooks/useStoreAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ export default function useStoreAddress( source = 'wc' ) {
const postcode = storeAddress?.postal_code || '';

const [ address, address2 = '' ] = streetAddress.split( '\n' );
const country = countryNameDict[ storeAddress?.country ];
const country = countryNameDict[ storeAddress?.country ] || '';
const countryCode = storeAddress?.country || '';
const isAddressFilled = ! missingRequiredFields.length;

data = {
countryCode,
address,
address2,
city,
Expand Down

0 comments on commit 99408e1

Please sign in to comment.