File size: 3,927 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { isDomainTransfer, isDomainMapping } from '@automattic/calypso-products';
import { useSelect } from '@wordpress/data';
import { useCallback } from 'react';
import { USER_STORE, ONBOARD_STORE } from 'calypso/landing/stepper/stores';
import { SIGNUP_DOMAIN_ORIGIN, recordSignupComplete } from 'calypso/lib/analytics/signup';
import { useExperiment } from 'calypso/lib/explat';
import { getSignupIsNewUserAndClear } from 'calypso/signup/storageUtils';
import { useSite } from './use-site';
import type { UserSelect, OnboardSelect } from '@automattic/data-stores';

export const useRecordSignupComplete = ( flow: string | null ) => {
	const site = useSite();
	const siteId = site?.ID || null;

	useExperiment( 'calypso_signup_simplified_onboarding_remove_focused_launchpad', {
		isEligible: flow === 'onboarding',
	} );

	const {
		userId,
		domainCartItem,
		planCartItem,
		selectedDomain,
		signupDomainOrigin,
		goals,
		selectedDesign,
	} = useSelect( ( select ) => {
		return {
			userId: ( select( USER_STORE ) as UserSelect ).getCurrentUser()?.ID,
			domainCartItem: ( select( ONBOARD_STORE ) as OnboardSelect ).getDomainCartItem(),
			planCartItem: ( select( ONBOARD_STORE ) as OnboardSelect ).getPlanCartItem(),
			selectedDomain: ( select( ONBOARD_STORE ) as OnboardSelect ).getSelectedDomain(),
			signupDomainOrigin: ( select( ONBOARD_STORE ) as OnboardSelect ).getSignupDomainOrigin(),
			goals: ( select( ONBOARD_STORE ) as OnboardSelect ).getGoals(),
			selectedDesign: ( select( ONBOARD_STORE ) as OnboardSelect ).getSelectedDesign(),
		};
	}, [] );

	const theme = site?.options?.theme_slug || selectedDesign?.slug || '';

	// Avoid generating new reference on each call
	const stringifiedGoals = goals.length && goals?.join( ',' );

	return useCallback(
		( signupCompletionState: Record< string, unknown > ) => {
			const isNewUser = getSignupIsNewUserAndClear( userId ) ?? false;
			// Domain product slugs can be a domain purchases like dotcom_domain or dotblog_domain or a mapping like domain_mapping
			// When purchasing free subdomains the product_slugs is empty (since there is no actual produce being purchased)
			// so we avoid capturing the product slug in these instances.
			const domainProductSlug = domainCartItem?.product_slug ?? undefined;

			// Domain cart items can sometimes be included when free. So the selected domain is explicitly checked to see if it's free.
			// For mappings and transfers this attribute should be empty but it needs to be checked.
			const hasCartItems = !! ( domainProductSlug || planCartItem ); // see the function `dependenciesContainCartItem()

			// When there is no plan put in the cart, `planCartItem` is `null` instead of `undefined` like domainCartItem.
			// It worths a investigation of whether the both should behave the same.
			const planProductSlug = planCartItem?.product_slug ?? undefined;
			// To have a paid domain item it has to either be a paid domain or a different domain product like mapping or transfer.
			const hasPaidDomainItem =
				( selectedDomain && ! selectedDomain.is_free ) || !! domainProductSlug;

			recordSignupComplete(
				{
					flow,
					siteId: siteId ?? signupCompletionState?.siteId,
					isNewUser,
					hasCartItems,
					theme,
					intent: flow,
					startingPoint: flow,
					isBlankCanvas: theme?.includes( 'blank-canvas' ),
					planProductSlug,
					domainProductSlug,
					isMapping:
						hasPaidDomainItem && domainCartItem ? isDomainMapping( domainCartItem ) : undefined,
					isTransfer:
						hasPaidDomainItem && domainCartItem ? isDomainTransfer( domainCartItem ) : undefined,
					signupDomainOrigin: signupDomainOrigin ?? SIGNUP_DOMAIN_ORIGIN.NOT_SET,
					framework: 'stepper',
					goals: stringifiedGoals,
				},
				true
			);
		},
		[
			domainCartItem,
			flow,
			planCartItem,
			selectedDomain,
			signupDomainOrigin,
			siteId,
			theme,
			userId,
			stringifiedGoals,
		]
	);
};