Skip to content

Commit bb5366e

Browse files
authored
Merge pull request #11330 from google/enhancement/11287-add-survey
2 parents 977c26e + a96bed5 commit bb5366e

10 files changed

Lines changed: 76 additions & 8 deletions

File tree

assets/js/googlesitekit/datastore/site/google-tag-gateway.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { setUsingCache } from 'googlesitekit-api';
2626
import {
2727
createTestRegistry,
2828
muteFetch,
29+
provideSiteInfo,
2930
provideUserAuthentication,
3031
subscribeUntil,
3132
untilResolved,
@@ -48,6 +49,7 @@ describe( 'core/site Google tag gateway', () => {
4849

4950
beforeEach( () => {
5051
registry = createTestRegistry();
52+
provideSiteInfo( registry );
5153
} );
5254

5355
afterAll( () => {

assets/js/googlesitekit/datastore/user/surveys.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import { CORE_USER, GLOBAL_SURVEYS_TIMEOUT_SLUG } from './constants';
3636
import { createFetchStore } from '../../data/create-fetch-store';
3737
import { createValidatedAction } from '../../data/utils';
38+
import { CORE_SITE } from '../site/constants';
3839

3940
const fetchTriggerSurveyStore = createFetchStore( {
4041
baseName: 'triggerSurvey',
@@ -158,6 +159,8 @@ const baseActions = {
158159
isSurveyTriggerLocked,
159160
getSurveyTimeouts,
160161
} = select( CORE_USER );
162+
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
163+
const { isUsingProxy } = select( CORE_SITE );
161164

162165
// The lock prevents multiple concurrent triggers for the same ID.
163166
if ( isSurveyTriggerLocked( triggerID ) ) {
@@ -166,12 +169,15 @@ const baseActions = {
166169
yield lockSurveyTrigger( triggerID );
167170

168171
try {
169-
// Wait for user authentication state to be available before selecting.
172+
// Wait for state to be available before selecting.
170173
yield commonActions.await(
171-
resolveSelect( CORE_USER ).getAuthentication()
174+
Promise.all( [
175+
resolveSelect( CORE_USER ).getAuthentication(),
176+
resolveSelect( CORE_SITE ).getSiteInfo(),
177+
] )
172178
);
173179

174-
if ( ! isAuthenticated() ) {
180+
if ( ! isAuthenticated() || ! isUsingProxy() ) {
175181
return {};
176182
}
177183

assets/js/googlesitekit/datastore/user/surveys.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import {
2525
untilResolved,
2626
muteFetch,
2727
provideUserAuthentication,
28+
provideSiteInfo,
2829
} from '../../../../../tests/js/utils';
2930
import {
3031
surveyEventEndpoint,
3132
surveyTimeoutsEndpoint,
3233
surveyTriggerEndpoint,
3334
} from '../../../../../tests/js/mock-survey-endpoints';
35+
import { CORE_SITE } from '../site/constants';
3436

3537
describe( 'core/user surveys', () => {
3638
let registry;
@@ -69,6 +71,7 @@ describe( 'core/user surveys', () => {
6971

7072
it( 'should not throw when called with only a triggerID', async () => {
7173
provideUserAuthentication( registry );
74+
provideSiteInfo( registry );
7275

7376
muteFetch( surveyTriggerEndpoint );
7477

@@ -85,6 +88,22 @@ describe( 'core/user surveys', () => {
8588

8689
it( 'should not fetch if user is not authenticated', async () => {
8790
provideUserAuthentication( registry, { authenticated: false } );
91+
provideSiteInfo( registry );
92+
93+
await registry
94+
.dispatch( CORE_USER )
95+
.triggerSurvey( 'userInput_answered_other__goals' );
96+
97+
expect( fetchMock ).not.toHaveFetched( surveyTriggerEndpoint, {
98+
body: {
99+
data: { triggerID: 'userInput_answered_other__goals' },
100+
},
101+
} );
102+
} );
103+
104+
it( 'should not fetch if site is not using proxy', async () => {
105+
provideUserAuthentication( registry );
106+
provideSiteInfo( registry, { usingProxy: false } );
88107

89108
await registry
90109
.dispatch( CORE_USER )
@@ -98,6 +117,7 @@ describe( 'core/user surveys', () => {
98117
} );
99118

100119
it( 'should wait for authentication to be resolved before making a network request', async () => {
120+
provideSiteInfo( registry );
101121
muteFetch( surveyTriggerEndpoint );
102122

103123
registry.dispatch( CORE_USER ).receiveGetSurveyTimeouts( [] );
@@ -133,6 +153,7 @@ describe( 'core/user surveys', () => {
133153

134154
it( 'should make network requests to survey endpoint', async () => {
135155
provideUserAuthentication( registry );
156+
provideSiteInfo( registry );
136157

137158
muteFetch( surveyTriggerEndpoint );
138159

@@ -158,6 +179,7 @@ describe( 'core/user surveys', () => {
158179
const triggerID = 'userInput_answered_other__goals';
159180

160181
provideUserAuthentication( registry );
182+
provideSiteInfo( registry );
161183

162184
registry
163185
.dispatch( CORE_USER )
@@ -177,6 +199,7 @@ describe( 'core/user surveys', () => {
177199
fetchMock.post( surveyTriggerEndpoint, {} );
178200

179201
provideUserAuthentication( registry );
202+
provideSiteInfo( registry );
180203
registry.dispatch( CORE_USER ).receiveGetSurveyTimeouts( [] );
181204

182205
await Promise.all( [
@@ -194,6 +217,7 @@ describe( 'core/user surveys', () => {
194217
const triggerID = 'userInput_answered_other__goals';
195218

196219
provideUserAuthentication( registry );
220+
provideSiteInfo( registry );
197221

198222
await registry.resolveSelect( CORE_USER ).getAuthentication();
199223

@@ -202,6 +226,7 @@ describe( 'core/user surveys', () => {
202226
registry.dispatch( CORE_USER ).receiveGetSurveyTimeouts( [] );
203227

204228
await registry.resolveSelect( CORE_USER ).getSurveyTimeouts();
229+
await registry.resolveSelect( CORE_SITE ).getSiteInfo();
205230

206231
jest.useFakeTimers();
207232

assets/js/googlesitekit/datastore/user/user-input-settings.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { setUsingCache } from 'googlesitekit-api';
2323
import {
2424
createTestRegistry,
2525
muteFetch,
26+
provideSiteInfo,
2627
provideUserAuthentication,
2728
subscribeUntil,
2829
untilResolved,
@@ -66,6 +67,7 @@ describe( 'core/user user-input-settings', () => {
6667
registry = createTestRegistry();
6768
store = registry.stores[ CORE_USER ].store;
6869
registry.dispatch( CORE_USER ).receiveIsUserInputCompleted( true );
70+
provideSiteInfo( registry );
6971
} );
7072

7173
afterAll( () => {

assets/js/modules/ads/components/notifications/AdsModuleSetupCTABanner.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ import { __ } from '@wordpress/i18n';
3939
import { useDispatch, useSelect } from 'googlesitekit-data';
4040
import { CORE_NOTIFICATIONS } from '../../../../googlesitekit/notifications/datastore/constants';
4141
import { CORE_USER } from '../../../../googlesitekit/datastore/user/constants';
42-
import { MINUTE_IN_SECONDS, WEEK_IN_SECONDS } from '../../../../util';
42+
import {
43+
DAY_IN_SECONDS,
44+
MINUTE_IN_SECONDS,
45+
WEEK_IN_SECONDS,
46+
} from '../../../../util';
4347
import {
4448
ADS_WOOCOMMERCE_REDIRECT_MODAL_CACHE_KEY,
4549
MODULES_ADS,
@@ -54,6 +58,7 @@ import SetupCTA from '../../../../googlesitekit/notifications/components/layout/
5458
import BannerSVGDesktop from '@/svg/graphics/banner-ads-setup-cta.svg?url';
5559
import BannerSVGMobile from '@/svg/graphics/banner-ads-setup-cta-mobile.svg?url';
5660
import LearnMoreLink from '@/js/googlesitekit/notifications/components/common/LearnMoreLink';
61+
import SurveyViewTrigger from '../../../../components/surveys/SurveyViewTrigger';
5762

5863
export default function AdsModuleSetupCTABanner( { id, Notification } ) {
5964
const [ openDialog, setOpenDialog ] = useState( false );
@@ -101,7 +106,11 @@ export default function AdsModuleSetupCTABanner( { id, Notification } ) {
101106

102107
const activateModule = useActivateModuleCallback( MODULE_SLUG_ADS );
103108

109+
const { triggerSurvey } = useDispatch( CORE_USER );
110+
104111
const onSetupCallback = useCallback( () => {
112+
triggerSurvey( 'accept_ads_setup_cta' );
113+
105114
if (
106115
! shouldShowWooCommerceRedirectModal ||
107116
isWooCommerceRedirectModalDismissed
@@ -113,6 +122,7 @@ export default function AdsModuleSetupCTABanner( { id, Notification } ) {
113122

114123
setOpenDialog( true );
115124
}, [
125+
triggerSurvey,
116126
shouldShowWooCommerceRedirectModal,
117127
activateModule,
118128
isWooCommerceRedirectModalDismissed,
@@ -210,6 +220,10 @@ export default function AdsModuleSetupCTABanner( { id, Notification } ) {
210220
dialogActive
211221
/>
212222
) }
223+
<SurveyViewTrigger
224+
triggerID="view_ads_setup_cta"
225+
ttl={ DAY_IN_SECONDS }
226+
/>
213227
</Notification>
214228
);
215229
}

assets/js/modules/ads/components/notifications/AdsModuleSetupCTABanner.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
provideModuleRegistrations,
3838
provideModules,
3939
provideSiteInfo,
40+
provideUserAuthentication,
4041
provideUserCapabilities,
4142
render,
4243
} from '../../../../../../tests/js/test-utils';
@@ -75,7 +76,9 @@ describe( 'AdsModuleSetupCTABanner', () => {
7576
connected: false,
7677
},
7778
] );
78-
provideSiteInfo( registry );
79+
provideUserAuthentication( registry );
80+
// Avoid invoking survey triggers for now.
81+
provideSiteInfo( registry, { usingProxy: false } );
7982

8083
registry.dispatch( MODULES_ADS ).receiveModuleData( {
8184
plugins: {

assets/js/modules/ads/components/setup/SetupMainPAX.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default function SetupMainPAX( { finishSetup } ) {
9999
return select( CORE_LOCATION ).isNavigatingTo( oAuthURL );
100100
} );
101101

102+
const { triggerSurvey } = useDispatch( CORE_USER );
102103
const { navigateTo } = useDispatch( CORE_LOCATION );
103104
const {
104105
setPaxConversionID,
@@ -150,6 +151,7 @@ export default function SetupMainPAX( { finishSetup } ) {
150151
}
151152

152153
trackEvent( `${ viewContext }_pax`, 'pax_campaign_created' );
154+
triggerSurvey( 'pax_campaign_created' );
153155

154156
setUserID( customerData.userId );
155157
setCustomerID( customerData.customerId );
@@ -179,7 +181,11 @@ export default function SetupMainPAX( { finishSetup } ) {
179181
notification: PAX_SETUP_SUCCESS_NOTIFICATION,
180182
}
181183
);
182-
await trackEvent( `${ viewContext }_pax`, 'pax_setup_completed' );
184+
await Promise.all( [
185+
trackEvent( `${ viewContext }_pax`, 'pax_setup_completed' ),
186+
triggerSurvey( 'pax_setup_completed' ),
187+
] );
188+
183189
finishSetup( redirectURL );
184190
}, [ registry, finishSetup, viewContext ] );
185191

@@ -202,9 +208,10 @@ export default function SetupMainPAX( { finishSetup } ) {
202208
const onLaunch = useCallback(
203209
( app ) => {
204210
trackEvent( `${ viewContext }_pax`, 'pax_launch' );
211+
triggerSurvey( 'pax_launch' );
205212
paxAppRef.current = app;
206213
},
207-
[ viewContext ]
214+
[ viewContext, triggerSurvey ]
208215
);
209216

210217
const onSetupCallback = useCallback( async () => {
@@ -214,10 +221,14 @@ export default function SetupMainPAX( { finishSetup } ) {
214221
}
215222

216223
// awaiting because `createAccount` may trigger a navigation.
217-
await trackEvent( viewContext, 'start_setup_pax' );
224+
await Promise.all( [
225+
trackEvent( viewContext, 'start_setup_pax' ),
226+
triggerSurvey( 'start_setup_pax' ),
227+
] );
218228

219229
createAccount();
220230
}, [
231+
triggerSurvey,
221232
isWooCommerceActivated,
222233
isWooCommerceRedirectModalDismissed,
223234
setOpenDialog,

assets/js/modules/analytics-4/datastore/audiences.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
freezeFetch,
2525
muteFetch,
2626
provideModules,
27+
provideSiteInfo,
2728
provideUserAuthentication,
2829
provideUserCapabilities,
2930
provideUserInfo,
@@ -619,6 +620,7 @@ describe( 'modules/analytics-4 audiences', () => {
619620
},
620621
] );
621622

623+
provideSiteInfo( registry );
622624
provideUserAuthentication( registry );
623625

624626
registry.dispatch( MODULES_ANALYTICS_4 ).setSettings( {

assets/js/modules/reader-revenue-manager/components/dashboard/ReaderRevenueManagerSetupCTABanner.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
provideModules,
3434
waitFor,
3535
provideUserAuthentication,
36+
provideSiteInfo,
3637
} from '../../../../../../tests/js/test-utils';
3738
import { withNotificationComponentProps } from '../../../../googlesitekit/notifications/util/component-props';
3839
import { CORE_USER } from '../../../../googlesitekit/datastore/user/constants';
@@ -73,6 +74,7 @@ describe( 'ReaderRevenueManagerSetupCTABanner', () => {
7374
activateModuleMock = jest.fn( () => activateModuleCallbackMock );
7475

7576
provideUserAuthentication( registry );
77+
provideSiteInfo( registry );
7678

7779
registry.dispatch( CORE_USER ).receiveGetDismissedPrompts( [] );
7880

includes/Core/Assets/Assets.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ private function get_assets() {
521521
'googlesitekit-data',
522522
'googlesitekit-api',
523523
'googlesitekit-user-data',
524+
'googlesitekit-datastore-site',
524525
),
525526
)
526527
),

0 commit comments

Comments
 (0)