-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathAudienceSegmentationSetupCTAWidget.js
344 lines (315 loc) · 10.9 KB
/
AudienceSegmentationSetupCTAWidget.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* KeyMetricsSetupCTAWidget component.
*
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { Fragment, useCallback, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useDispatch, useSelect } from 'googlesitekit-data';
import BannerGraphicsSVGDesktop from '../../../../../../svg/graphics/audience-segmentation-setup-desktop.svg';
import BannerGraphicsSVGTablet from '../../../../../../svg/graphics/audience-segmentation-setup-tablet.svg';
import BannerGraphicsSVGMobile from '../../../../../../svg/graphics/audience-segmentation-setup-mobile.svg';
import whenActive from '../../../../../util/when-active';
import { CORE_FORMS } from '../../../../../googlesitekit/datastore/forms/constants';
import { CORE_USER } from '../../../../../googlesitekit/datastore/user/constants';
import { CORE_SITE } from '../../../../../googlesitekit/datastore/site/constants';
import { CORE_NOTIFICATIONS } from '../../../../../googlesitekit/notifications/datastore/constants';
import {
MODULES_ANALYTICS_4,
AUDIENCE_SEGMENTATION_SETUP_FORM,
} from '../../../datastore/constants';
import { SETTINGS_VISITOR_GROUPS_SETUP_SUCCESS_NOTIFICATION } from '../settings/SettingsCardVisitorGroups/SetupSuccess';
import { Button, SpinnerButton } from 'googlesitekit-components';
import { Cell, Grid, Row } from '../../../../../material-components';
import {
BREAKPOINT_SMALL,
BREAKPOINT_TABLET,
useBreakpoint,
} from '../../../../../hooks/useBreakpoint';
import useViewContext from '../../../../../hooks/useViewContext';
import {
AdminMenuTooltip,
useShowTooltip,
useTooltipState,
} from '../../../../../components/AdminMenuTooltip';
import { withWidgetComponentProps } from '../../../../../googlesitekit/widgets/util';
import { WEEK_IN_SECONDS } from '../../../../../util';
import useEnableAudienceGroup from '../../../hooks/useEnableAudienceGroup';
import AudienceErrorModal from './AudienceErrorModal';
export const AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION =
'audience_segmentation_setup_cta-notification';
function AudienceSegmentationSetupCTAWidget( { Widget, WidgetNull } ) {
const viewContext = useViewContext();
const breakpoint = useBreakpoint();
const isMobileBreakpoint = breakpoint === BREAKPOINT_SMALL;
const isTabletBreakpoint = breakpoint === BREAKPOINT_TABLET;
const { invalidateResolution } = useDispatch( CORE_NOTIFICATIONS );
const { setValues } = useDispatch( CORE_FORMS );
const showTooltip = useShowTooltip(
AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION
);
const { isTooltipVisible } = useTooltipState(
AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION
);
const isDismissed = useSelect( ( select ) =>
select( CORE_USER ).isPromptDismissed(
AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION
)
);
const dismissCount = useSelect( ( select ) =>
select( CORE_USER ).getPromptDismissCount(
AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION
)
);
const dismissedPromptsLoaded = useSelect( ( select ) =>
select( CORE_USER ).hasFinishedResolution( 'getDismissedPrompts', [] )
);
const configuredAudiences = useSelect( ( select ) =>
select( CORE_USER ).getConfiguredAudiences()
);
const autoSubmit = useSelect( ( select ) =>
select( CORE_FORMS ).getValue(
AUDIENCE_SEGMENTATION_SETUP_FORM,
'autoSubmit'
)
);
const [ showErrorModal, setShowErrorModal ] = useState( false );
const { dismissItem, dismissPrompt } = useDispatch( CORE_USER );
const { apiErrors, failedAudiences, isSaving, onEnableGroups } =
useEnableAudienceGroup( {
onSuccess: () => {
invalidateResolution( 'getQueuedNotifications', [
viewContext,
] );
dismissPrompt( AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION, {
expiresInSeconds: 0,
} );
// Dismiss success notification in settings.
dismissItem(
SETTINGS_VISITOR_GROUPS_SETUP_SUCCESS_NOTIFICATION
);
},
onError: () => {
setShowErrorModal( true );
},
} );
const analyticsIsDataAvailableOnLoad = useSelect( ( select ) => {
// We should call isGatheringData() within this component for completeness
// as we do not want to rely on it being called in other components.
// This selector makes report requests which, if they return data, then the
// `data-available` transients are set. These transients are prefetched as
// a global on the next page load.
select( MODULES_ANALYTICS_4 ).isGatheringData();
return select( MODULES_ANALYTICS_4 ).isDataAvailableOnLoad();
} );
const audienceSegmentationSetupCompletedBy = useSelect( ( select ) =>
select( MODULES_ANALYTICS_4 ).getAudienceSegmentationSetupCompletedBy()
);
const handleDismissClick = async () => {
showTooltip();
// For the first dismissal, we show the notification again in two weeks.
if ( dismissCount < 1 ) {
const twoWeeksInSeconds = WEEK_IN_SECONDS * 2;
await dismissPrompt( AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION, {
expiresInSeconds: twoWeeksInSeconds,
} );
} else {
// For the second dismissal, dismiss the notification permanently.
await dismissPrompt( AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION );
}
};
const { clearPermissionScopeError } = useDispatch( CORE_USER );
const { setSetupErrorCode } = useDispatch( CORE_SITE );
const onCancel = useCallback( () => {
setValues( AUDIENCE_SEGMENTATION_SETUP_FORM, {
autoSubmit: false,
} );
clearPermissionScopeError();
setSetupErrorCode( null );
setShowErrorModal( false );
}, [ clearPermissionScopeError, setSetupErrorCode, setValues ] );
const setupErrorCode = useSelect( ( select ) =>
select( CORE_SITE ).getSetupErrorCode()
);
const hasOAuthError = autoSubmit && setupErrorCode === 'access_denied';
if ( isTooltipVisible ) {
return (
<Fragment>
<WidgetNull />
<AdminMenuTooltip
title={ __(
'You can always enable groups from Settings later',
'google-site-kit'
) }
content={ __(
'The visitors group section will be added to your dashboard once you set it up.',
'google-site-kit'
) }
dismissLabel={ __( 'Got it', 'google-site-kit' ) }
tooltipStateKey={
AUDIENCE_SEGMENTATION_SETUP_CTA_NOTIFICATION
}
/>
</Fragment>
);
}
if (
audienceSegmentationSetupCompletedBy !== null ||
configuredAudiences === undefined ||
configuredAudiences?.length ||
! analyticsIsDataAvailableOnLoad ||
isDismissed ||
! dismissedPromptsLoaded
) {
return null;
}
// TODO: We need to refactor this and the ConsentModeSetupCTAWidget to avoid this duplicate inlining of the widget context and area structure,
// and to ensure only one of these setup CTAs is shown at a time. This will be handled in a subsequent issue.
return (
<div className="googlesitekit-widget-context">
<Grid className="googlesitekit-widget-area">
<Row>
<Cell size={ 12 }>
<Widget
noPadding
className="googlesitekit-audience-segmentation-setup-cta-widget"
>
<Grid collapsed>
<Row>
<Cell
smSize={ 6 }
mdSize={ 8 }
lgSize={ 7 }
className="googlesitekit-widget-audience-segmentation-primary-cell"
>
<div className="googlesitekit-widget-audience-segmentation-text__wrapper">
<h3 className="googlesitekit-publisher-win__title">
{ __(
'Learn how different types of visitors interact with your site',
'google-site-kit'
) }
</h3>
<p>
{ __(
'Understand what brings new visitors to your site and keeps them coming back. Site Kit can now group your site visitors into relevant segments like "new" and "returning". To set up these new groups, Site Kit needs to update your Google Analytics property.',
'google-site-kit'
) }
</p>
</div>
<div className="googlesitekit-widget-audience-segmentation-actions__wrapper">
<Fragment>
<SpinnerButton
className="googlesitekit-audience-segmentation-cta-button"
onClick={ onEnableGroups }
isSaving={ isSaving }
>
{ isSaving
? __(
'Enabling groups',
'google-site-kit'
)
: __(
'Enable groups',
'google-site-kit'
) }
</SpinnerButton>
<Button
tertiary
onClick={
handleDismissClick
}
>
{ dismissCount < 1
? __(
'Maybe later',
'google-site-kit'
)
: __(
'Don’t show again',
'google-site-kit'
) }
</Button>
</Fragment>
</div>
</Cell>
{ ! isMobileBreakpoint &&
! isTabletBreakpoint && (
<Cell
alignBottom
className="googlesitekit-widget-audience-segmentation-svg__wrapper"
smSize={ 6 }
mdSize={ 3 }
lgSize={ 5 }
>
<BannerGraphicsSVGDesktop />
</Cell>
) }
{ isTabletBreakpoint && (
<Cell
className="googlesitekit-widget-audience-segmentation-svg__wrapper"
mdSize={ 8 }
>
<BannerGraphicsSVGTablet />
</Cell>
) }
{ isMobileBreakpoint && (
<Cell
className="googlesitekit-widget-audience-segmentation-svg__wrapper"
smSize={ 8 }
>
<BannerGraphicsSVGMobile />
</Cell>
) }
</Row>
</Grid>
</Widget>
</Cell>
</Row>
</Grid>
{ ( showErrorModal || hasOAuthError ) && (
<AudienceErrorModal
hasOAuthError={ hasOAuthError }
apiErrors={ apiErrors.length ? apiErrors : failedAudiences }
onRetry={ onEnableGroups }
inProgress={ isSaving }
onCancel={
hasOAuthError
? onCancel
: () => setShowErrorModal( false )
}
/>
) }
</div>
);
}
AudienceSegmentationSetupCTAWidget.propTypes = {
Widget: PropTypes.elementType.isRequired,
WidgetNull: PropTypes.elementType,
};
export default compose(
whenActive( { moduleName: 'analytics-4' } ),
withWidgetComponentProps( 'audienceSegmentationSetupCTA' )
)( AudienceSegmentationSetupCTAWidget );