-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathasset-group.js
174 lines (161 loc) · 6.17 KB
/
asset-group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { ASSET_FORM_KEY } from '~/constants';
import { useAdaptiveFormContext } from '~/components/adaptive-form';
import StepContent from '~/components/stepper/step-content';
import StepContentHeader from '~/components/stepper/step-content-header';
import StepContentFooter from '~/components/stepper/step-content-footer';
import StepContentActions from '~/components/stepper/step-content-actions';
import AppButton from '~/components/app-button';
import AssetGroupSection from './asset-group-section';
import Faqs from './faqs';
import { recordGlaEvent } from '~/utils/tracks';
import useTargetAudienceFinalCountryCodes from '~/hooks/useTargetAudienceFinalCountryCodes';
export const ACTION_SUBMIT_CAMPAIGN_AND_ASSETS = 'submit-campaign-and-assets';
export const ACTION_SUBMIT_CAMPAIGN_ONLY = 'submit-campaign-only';
/**
* @typedef {import('~/data/actions').Campaign} Campaign
*/
/**
* Clicking on the submit button on the campaign creation or editing page.
* If a value is recorded as `unknown`, it's because no assets are imported and therefore unknown.
*
* @event gla_submit_campaign_button_click
* @property {string} context Indicate the place where the button is located. Possible values: `campaign-creation`, `campaign-editing`.
* @property {string} action Indicate which submit button is clicked. Possible values: `submit-campaign-and-assets`, `submit-campaign-only`.
* @property {string} audiences Country codes of the campaign audience countries, e.g. `US,JP,AU`.
* @property {string} budget Daily average cost of the campaign.
* @property {string} assets_validation Whether all asset values are valid or at least one invalid. Possible values: `valid`, `invalid`, `unknown`.
* @property {string} number_of_business_name The number of this asset in string type or `unknown`.
* @property {string} number_of_marketing_image Same as above.
* @property {string} number_of_square_marketing_image Same as above.
* @property {string} number_of_portrait_marketing_image Same as above.
* @property {string} number_of_logo Same as above.
* @property {string} number_of_headline Same as above.
* @property {string} number_of_long_headline Same as above.
* @property {string} number_of_description Same as above.
* @property {string} number_of_call_to_action_selection Same as above.
* @property {string} number_of_final_url Same as above.
* @property {string} number_of_display_url_path Same as above.
*/
/**
* Renders the container of the form content for managing an asset group of a campaign.
*
* Please note that this component relies on an AdaptiveForm's context, so it expects
* a context provider component (`AdaptiveForm`) to existing in its parents.
*
* @param {Object} props React props.
* @param {Campaign} [props.campaign] Campaign data to be edited. If not provided, this component will show campaign creation UI.
*
* @fires gla_submit_campaign_button_click
*/
export default function AssetGroup( { campaign } ) {
const isCreation = ! campaign;
const { isValidForm, handleSubmit, adapter, values } =
useAdaptiveFormContext();
const { data: countryCodes } = useTargetAudienceFinalCountryCodes();
const { isValidAssetGroup, isSubmitting, isSubmitted, submitter } = adapter;
const currentAction = submitter?.dataset.action;
function recordSubmissionClickEvent( event ) {
const audiences = isCreation ? countryCodes : campaign.displayCountries;
const finalUrl = values[ ASSET_FORM_KEY.FINAL_URL ];
const eventProps = {
context: isCreation ? 'campaign-creation' : 'campaign-editing',
action: event.target.dataset.action,
audiences: audiences.join( ',' ),
budget: values.amount.toString(),
assets_validation: isValidAssetGroup ? 'valid' : 'invalid',
};
if ( ! finalUrl ) {
eventProps.assets_validation = 'unknown';
}
Object.values( ASSET_FORM_KEY ).forEach( ( key ) => {
const name = `number_of_${ key }`;
const num = [ values[ key ] ].flat().filter( Boolean ).length;
eventProps[ name ] = finalUrl ? num.toString() : 'unknown';
} );
recordGlaEvent( 'gla_submit_campaign_button_click', eventProps );
}
const handleSkipClick = ( event ) => {
handleSubmit( event );
recordSubmissionClickEvent( event );
};
const handleLaunchClick = ( event ) => {
if ( isValidAssetGroup ) {
handleSubmit( event );
} else {
adapter.showValidation();
}
recordSubmissionClickEvent( event );
};
return (
<StepContent>
<StepContentHeader
title={ __(
'Optimize your campaign',
'google-listings-and-ads'
) }
description={ __(
'Drive greater performance by adding text and image assets to create personalized and engaging ads',
'google-listings-and-ads'
) }
/>
<AssetGroupSection />
<StepContentFooter>
<StepContentActions>
{ ( isCreation || adapter.isEmptyAssetEntityGroup ) && (
// Currently, the PMax Assets feature in this extension doesn't offer the function
// to delete the asset entity group, so it needs to hide the skip button if the editing
// asset group is not considered empty.
<AppButton
isTertiary
data-action={ ACTION_SUBMIT_CAMPAIGN_ONLY }
disabled={
! isValidForm ||
isSubmitted ||
currentAction ===
ACTION_SUBMIT_CAMPAIGN_AND_ASSETS
}
loading={
isSubmitting &&
currentAction === ACTION_SUBMIT_CAMPAIGN_ONLY
}
onClick={ handleSkipClick }
>
{ __(
'Skip this step',
'google-listings-and-ads'
) }
</AppButton>
) }
<AppButton
isPrimary
data-action={ ACTION_SUBMIT_CAMPAIGN_AND_ASSETS }
disabled={
! adapter.baseAssetGroup[
ASSET_FORM_KEY.FINAL_URL
] ||
isSubmitted ||
currentAction === ACTION_SUBMIT_CAMPAIGN_ONLY
}
loading={
isSubmitting &&
currentAction === ACTION_SUBMIT_CAMPAIGN_AND_ASSETS
}
onClick={ handleLaunchClick }
>
{ isCreation
? __( 'Create campaign', 'google-listings-and-ads' )
: __( 'Save changes', 'google-listings-and-ads' ) }
</AppButton>
</StepContentActions>
<Faqs />
</StepContentFooter>
</StepContent>
);
}