Skip to content

Commit a9d2b7d

Browse files
authored
Track user viewport and url when using WooPay (#5453)
* Track user viewport and url when using WooPay * Add changelog entry * Remove console.log * Add tracking data on express checkout * Fix white space
1 parent 93cd56c commit a9d2b7d

File tree

7 files changed

+101
-1
lines changed

7 files changed

+101
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Track user viewport and url when using WooPay

client/checkout/platform-checkout/email-input-iframe.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ export const handlePlatformCheckoutEmailInput = async (
231231
return;
232232
}
233233

234+
const viewportWidth = window.document.documentElement.clientWidth;
235+
const viewportHeight = window.document.documentElement.clientHeight;
236+
234237
const urlParams = new URLSearchParams();
235238
urlParams.append( 'email', email );
236239
urlParams.append( 'testMode', getConfig( 'testMode' ) );
@@ -239,6 +242,12 @@ export const handlePlatformCheckoutEmailInput = async (
239242
fullScreenModalBreakpoint > window.innerWidth
240243
);
241244
urlParams.append( 'wcpayVersion', getConfig( 'wcpayVersionNumber' ) );
245+
urlParams.append( 'is_blocks', isBlocksCheckout ? 'true' : 'false' );
246+
urlParams.append( 'source_url', window.location.href );
247+
urlParams.append(
248+
'viewport',
249+
`${ viewportWidth }x${ viewportHeight }`
250+
);
242251

243252
iframe.src = `${ getConfig(
244253
'platformCheckoutHost'

client/checkout/platform-checkout/express-button/express-checkout-iframe.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ export const expressCheckoutIframe = async ( api, context, emailSelector ) => {
165165
return;
166166
}
167167

168+
const viewportWidth = window.document.documentElement.clientWidth;
169+
const viewportHeight = window.document.documentElement.clientHeight;
170+
168171
const urlParams = new URLSearchParams();
169172
urlParams.append( 'testMode', getConfig( 'testMode' ) );
170173
urlParams.append(
@@ -177,6 +180,14 @@ export const expressCheckoutIframe = async ( api, context, emailSelector ) => {
177180
userEmail = email;
178181
urlParams.append( 'email', email );
179182
}
183+
urlParams.append( 'is_blocks', !! wcSettings.wcBlocksConfig );
184+
urlParams.append( 'is_express', 'true' );
185+
urlParams.append( 'express_context', context );
186+
urlParams.append( 'source_url', window.location.href );
187+
urlParams.append(
188+
'viewport',
189+
`${ viewportWidth }x${ viewportHeight }`
190+
);
180191

181192
iframe.src = `${ getConfig(
182193
'platformCheckoutHost'

client/components/platform-checkout/save-user/checkout-page-save-user.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
4444
isBlocksCheckout
4545
);
4646
const cart = useDispatch( WC_STORE_CART );
47+
const viewportWidth = window.document.documentElement.clientWidth;
48+
const viewportHeight = window.document.documentElement.clientHeight;
4749

4850
const getPhoneFieldValue = () => {
4951
let phoneFieldValue = '';
@@ -73,6 +75,9 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
7375
? {}
7476
: {
7577
save_user_in_platform_checkout: isSaveDetailsChecked,
78+
platform_checkout_source_url: window.location.href,
79+
platform_checkout_is_blocks: true,
80+
platform_checkout_viewport: `${ viewportWidth }x${ viewportHeight }`,
7681
platform_checkout_user_phone_field: {
7782
full: phoneNumber,
7883
},
@@ -93,7 +98,13 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
9398
} );
9499
} );
95100
},
96-
[ isSaveDetailsChecked, phoneNumber, cart ]
101+
[
102+
isSaveDetailsChecked,
103+
phoneNumber,
104+
cart,
105+
viewportWidth,
106+
viewportHeight,
107+
]
97108
);
98109

99110
const handleCheckboxClick = ( e ) => {
@@ -268,6 +279,16 @@ const CheckoutPageSaveUser = ( { isBlocksCheckout } ) => {
268279
className="save-details-form form-row"
269280
data-testid="save-user-form"
270281
>
282+
<input
283+
type="hidden"
284+
name="platform_checkout_source_url"
285+
value={ window.location.href }
286+
/>
287+
<input
288+
type="hidden"
289+
name="platform_checkout_viewport"
290+
value={ `${ viewportWidth }x${ viewportHeight }` }
291+
/>
271292
<PhoneNumberInput
272293
value={
273294
null === phoneNumber

client/components/platform-checkout/save-user/test/checkout-page-save-user.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ describe( 'CheckoutPageSaveUser', () => {
186186
namespace: 'platform-checkout',
187187
data: {
188188
save_user_in_platform_checkout: true,
189+
platform_checkout_source_url: 'http://localhost/',
190+
platform_checkout_is_blocks: true,
191+
platform_checkout_viewport: '0x0',
189192
platform_checkout_user_phone_field: {
190193
full: '+12015555555',
191194
},

includes/platform-checkout-user/class-platform-checkout-save-user.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function maybe_add_userdata_to_metadata( $metadata, $order ) {
8383
$platform_checkout_phone = $this->platform_checkout_util->get_platform_checkout_phone();
8484

8585
if ( $should_save_platform_customer && $platform_checkout_phone ) {
86+
$platform_checkout_source_url = $this->platform_checkout_util->get_platform_checkout_source_url();
87+
$platform_checkout_is_blocks = $this->platform_checkout_util->get_platform_checkout_is_blocks();
88+
$platform_checkout_viewport = $this->platform_checkout_util->get_platform_checkout_viewport();
89+
8690
// Add the metadata.
8791
$metadata['platform_checkout_primary_first_name'] = wc_clean( $order->get_billing_first_name() );
8892
$metadata['platform_checkout_primary_last_name'] = wc_clean( $order->get_billing_last_name() );
@@ -93,6 +97,9 @@ public function maybe_add_userdata_to_metadata( $metadata, $order ) {
9397
$metadata['platform_checkout_secondary_phone'] = wc_clean( $order->get_shipping_phone() );
9498
$metadata['platform_checkout_secondary_company'] = wc_clean( $order->get_shipping_company() );
9599
$metadata['platform_checkout_phone'] = $platform_checkout_phone;
100+
$metadata['platform_checkout_source_url'] = $platform_checkout_source_url;
101+
$metadata['platform_checkout_is_blocks'] = $platform_checkout_is_blocks;
102+
$metadata['platform_checkout_viewport'] = $platform_checkout_viewport;
96103
}
97104

98105
return $metadata;

includes/platform-checkout/class-platform-checkout-utilities.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,49 @@ public function get_platform_checkout_phone() {
151151

152152
return '';
153153
}
154+
155+
/**
156+
* Get the url marketing where the user have chosen marketing options.
157+
*
158+
* @return mixed|string
159+
*/
160+
public function get_platform_checkout_source_url() {
161+
$session_data = WC()->session->get( Platform_Checkout_Extension::PLATFORM_CHECKOUT_SESSION_KEY );
162+
163+
if ( ! empty( $_POST['platform_checkout_source_url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
164+
return wc_clean( wp_unslash( $_POST['platform_checkout_source_url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
165+
} elseif ( ! empty( $session_data['platform_checkout_source_url'] ) ) {
166+
return $session_data['platform_checkout_source_url'];
167+
}
168+
169+
return '';
170+
}
171+
172+
/**
173+
* Get if the request comes from blocks checkout.
174+
*
175+
* @return boolean
176+
*/
177+
public function get_platform_checkout_is_blocks() {
178+
$session_data = WC()->session->get( Platform_Checkout_Extension::PLATFORM_CHECKOUT_SESSION_KEY );
179+
180+
return ( isset( $_POST['platform_checkout_is_blocks'] ) && filter_var( wp_unslash( $_POST['platform_checkout_is_blocks'] ), FILTER_VALIDATE_BOOLEAN ) ) || ( isset( $session_data['platform_checkout_is_blocks'] ) && filter_var( $session_data['platform_checkout_is_blocks'], FILTER_VALIDATE_BOOLEAN ) ); // phpcs:ignore WordPress.Security.NonceVerification
181+
}
182+
183+
/**
184+
* Get the user viewport.
185+
*
186+
* @return mixed|string
187+
*/
188+
public function get_platform_checkout_viewport() {
189+
$session_data = WC()->session->get( Platform_Checkout_Extension::PLATFORM_CHECKOUT_SESSION_KEY );
190+
191+
if ( ! empty( $_POST['platform_checkout_viewport'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
192+
return wc_clean( wp_unslash( $_POST['platform_checkout_viewport'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
193+
} elseif ( ! empty( $session_data['platform_checkout_viewport'] ) ) {
194+
return $session_data['platform_checkout_viewport'];
195+
}
196+
197+
return '';
198+
}
154199
}

0 commit comments

Comments
 (0)