Skip to content

Commit

Permalink
Update Budget Setup Card.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Aug 22, 2024
1 parent a9a9394 commit a57bf14
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,19 @@ import GridiconNoticeOutline from 'gridicons/dist/notice-outline';
/**
* Internal dependencies
*/
import useCountryKeyNameMap from '.~/hooks/useCountryKeyNameMap';
import useFetchBudgetRecommendationEffect from './useFetchBudgetRecommendationEffect';
import './index.scss';

/*
* If a merchant selects more than one country, the budget recommendation
* takes the highest country out from the selected countries.
*
* For example, a merchant selected Brunei (20 USD) and Croatia (15 USD),
* then the budget recommendation should be (20 USD).
*/
function getHighestBudget( recommendations ) {
return recommendations.reduce( ( defender, challenger ) => {
if ( challenger.daily_budget > defender.daily_budget ) {
return challenger;
}
return defender;
} );
}

function toRecommendationRange( isMultiple, ...values ) {
const conversionMap = { strong: <strong />, em: <em />, br: <br /> };
const template = isMultiple
? // translators: it's a range of recommended budget amount. 1: the value of the budget, 2: the currency of amount.
__(
'Google will optimize your ads to maximize performance across the country/s you select.<br /><em>Tip: Most merchants targeting similar countries <strong>set a daily budget of %1$f %2$s</strong></em>',
'We recommend running campaigns at least 1 month so it can learn to optimize for your business.<br /><em>Tip: Most merchants targeting similar countries <strong>set a daily budget of %1$f %2$s</strong></em>',
'google-listings-and-ads'
)
: // translators: it's a range of recommended budget amount. 1: the value of the budget, 2: the currency of amount 3: a country name selected by the merchant.
__(
'Google will optimize your ads to maximize performance across the country/s you select.<br /><em>Tip: Most merchants targeting <strong>%3$s set a daily budget of %1$f %2$s</strong></em>',
'We recommend running campaigns at least 1 month so it can learn to optimize for your business.<br /><em>Tip: Most merchants targeting <strong>%3$s set a daily budget of %1$f %2$s</strong></em>',
'google-listings-and-ads'
);

Expand All @@ -50,27 +32,22 @@ function toRecommendationRange( isMultiple, ...values ) {
}

const BudgetRecommendation = ( props ) => {
const { countryCodes, dailyAverageCost = Infinity } = props;
const { data } = useFetchBudgetRecommendationEffect( countryCodes );
const map = useCountryKeyNameMap();

if ( ! data ) {
return null;
}

const { currency, recommendations } = data;
const { daily_budget: dailyBudget, country } =
getHighestBudget( recommendations );
const {
countryCodes,
dailyBudget: recommendedBudget,
country: countryName,
currency,
value: currentBudget,
} = props;

Check warning on line 41 in js/src/components/paid-ads/budget-section/budget-recommendation/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/components/paid-ads/budget-section/budget-recommendation/index.js#L41

Added line #L41 was not covered by tests

const countryName = map[ country ];
const recommendationRange = toRecommendationRange(
recommendations.length > 1,
dailyBudget,
countryCodes.length > 1,
recommendedBudget,
currency,
countryName
);

const showLowerBudgetNotice = dailyAverageCost < dailyBudget;
const showLowerBudgetNotice = currentBudget < recommendedBudget;

Check warning on line 50 in js/src/components/paid-ads/budget-section/budget-recommendation/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/components/paid-ads/budget-section/budget-recommendation/index.js#L50

Added line #L50 was not covered by tests

return (
<div className="gla-budget-recommendation">
Expand Down
7 changes: 5 additions & 2 deletions js/src/components/paid-ads/budget-section/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const nonInteractableProps = {
*/
const BudgetSection = ( { formProps, disabled = false, children } ) => {
const { getInputProps, setValue, values } = formProps;
const { countryCodes, amount } = values;
const { countryCodes, amount, country, dailyBudget } = values;

Check warning on line 33 in js/src/components/paid-ads/budget-section/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/components/paid-ads/budget-section/index.js#L33

Added line #L33 was not covered by tests
const { googleAdsAccount } = useGoogleAdsAccount();
const monthlyMaxEstimated = getMonthlyMaxEstimated( amount );
// Display the currency code that will be used by Google Ads, but still use the store's currency formatting settings.
Expand Down Expand Up @@ -92,7 +92,10 @@ const BudgetSection = ( { formProps, disabled = false, children } ) => {
{ countryCodes.length > 0 && (
<BudgetRecommendation
countryCodes={ countryCodes }
dailyAverageCost={ amount }
{ ...getInputProps( 'amount' ) }
country={ country }
currency={ currency }
dailyBudget={ dailyBudget }
/>
) }
</Section.Card.Body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Form } from '@woocommerce/components';
import useGoogleAdsAccount from '.~/hooks/useGoogleAdsAccount';
import useTargetAudienceFinalCountryCodes from '.~/hooks/useTargetAudienceFinalCountryCodes';
import useGoogleAdsAccountBillingStatus from '.~/hooks/useGoogleAdsAccountBillingStatus';
import useFetchBudgetRecommendationEffect from '.~/components/paid-ads/budget-section/budget-recommendation/useFetchBudgetRecommendationEffect';
import AudienceSection from '.~/components/paid-ads/audience-section';
import BudgetSection from '.~/components/paid-ads/budget-section';
import BillingCard from '.~/components/paid-ads/billing-card';
Expand All @@ -32,11 +33,29 @@ import { GOOGLE_ADS_BILLING_STATUS } from '.~/constants';

const defaultPaidAds = {
amount: 0,
country: undefined,
countryCodes: [],
dailyBudget: 0,
isValid: false,
isReady: false,
};

/*
* If a merchant selects more than one country, the budget recommendation
* takes the highest country out from the selected countries.
*
* For example, a merchant selected Brunei (20 USD) and Croatia (15 USD),
* then the budget recommendation should be (20 USD).
*/
function getHighestBudget( recommendations ) {
return recommendations.reduce( ( defender, challenger ) => {
if ( challenger.daily_budget > defender.daily_budget ) {
return challenger;
}
return defender;
} );
}

/**
* Resolve the initial paid ads data from the given paid ads data with the loaded target audience.
* Parts of the resolved data are used in the `initialValues` prop of `Form` component.
Expand Down Expand Up @@ -78,6 +97,14 @@ export default function PaidAdsSetupSections( { onStatesReceived } ) {
const { hasGoogleAdsConnection } = useGoogleAdsAccount();
const { data: targetAudience } = useTargetAudienceFinalCountryCodes();
const { billingStatus } = useGoogleAdsAccountBillingStatus();
const { data: recommendationData } =
useFetchBudgetRecommendationEffect( targetAudience );
const { recommendations } = recommendationData || {};

const { daily_budget: dailyBudget, country } =
recommendations !== undefined
? getHighestBudget( recommendations )
: {};

const onStatesReceivedRef = useRef();
onStatesReceivedRef.current = onStatesReceived;
Expand Down Expand Up @@ -127,10 +154,16 @@ export default function PaidAdsSetupSections( { onStatesReceived } ) {
- https://github.com/woocommerce/google-listings-and-ads/blob/5b6522ca10ad75556e6b2de7c120cc712aab70b1/js/src/components/free-listings/setup-free-listings/index.js#L172-L186
*/
useEffect( () => {
setPaidAds( ( currentPaidAds ) =>
resolveInitialPaidAds( currentPaidAds, targetAudience )
);
}, [ targetAudience ] );
setPaidAds( ( currentPaidAds ) => {
currentPaidAds = {
...currentPaidAds,
amount: dailyBudget ? dailyBudget : currentPaidAds.amount,
country: country ? country : currentPaidAds.country,
dailyBudget,
};
return resolveInitialPaidAds( currentPaidAds, targetAudience );
} );
}, [ targetAudience, dailyBudget, country ] );

if ( ! targetAudience || ! billingStatus ) {
return (
Expand All @@ -143,6 +176,8 @@ export default function PaidAdsSetupSections( { onStatesReceived } ) {
const initialValues = {
countryCodes: paidAds.countryCodes,
amount: paidAds.amount,
country: paidAds.country,
dailyBudget: paidAds.dailyBudget,
};

return (
Expand Down

0 comments on commit a57bf14

Please sign in to comment.