-
Notifications
You must be signed in to change notification settings - Fork 328
Closed
Labels
P0High priorityHigh priorityTeam MIssues for Squad 2Issues for Squad 2Type: BugSomething isn't workingSomething isn't working
Description
Bug Description
When WooCommerce purchases include a phone number that already contains a country code prefix (e.g., +94), Site Kit's Enhanced Conversions implementation adds the country code again programmatically, resulting in a duplicated prefix in the data layer.
Example:
- User enters:
+94771770589 - Data layer outputs:
+9494771770589(country code+94duplicated)
Expected behavior:
If the user-entered phone number already contains a country code prefix, Site Kit should not prepend it again.
Bug Bashing Asana ticket for reference.
Steps to reproduce
Steps to reproduce
- Install and activate WooCommerce
- Enable Site Kit's Enhanced Conversions (ensure
gtagUserDatafeature flag is enabled) - Create a test product in WooCommerce
- Proceed to checkout
- Enter billing information with a phone number that includes the country code, e.g.,
+94771770589 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Observe the
user_data.phone_numberfield shows a duplicated country code:+9494771770589
Additional Scenarios
Scenario 1: Short national number without country code
- Input:
7800099 - Expected:
+447800099(if billing country is UK) - Actual: Phone number doesn't appear in events (fails length validation after normalization)
Scenario 2: User enters a different country code than the billing country
- Input:
+2307800099(Mauritius +230) - Billing Country: UK (which has +44)
- Expected:
+2307800099(respect user's explicit country code) - Actual:
+442307800099(billing country code incorrectly prepended, overriding user's input)
Screenshots
Additional Context
- PHP Version:
- OS: [e.g. iOS]
- Browser: [e.g. chrome, safari]
- Plugin Version: [e.g. 22]
- Device: [e.g. iPhone6]
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Phone numbers entered with a country code prefix (e.g.,
+94771770589) should not have the country code duplicated in the data layer. - Phone numbers entered without a country code prefix (national format) should have the billing country code prepended correctly.
- Phone numbers entered with a different country code than the billing country should respect the user's explicitly provided country code.
- All phone numbers should pass E.164 format validation (11-15 characters including
+).
Implementation Brief
- Update the
get_normalized_phone()method inincludes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.phpto detect and handle existing country codes:- Preserve the original input to detect if user explicitly provided a
+prefix usingstrpos( trim( $phone ), '+' ) === 0 - Extract digits using
preg_replace( '/[^0-9]/', '', $phone ) - Get the billing country calling code digits (without the
+sign) usingltrim( $calling_code, '+' ) - If the phone number already starts with the billing country code digits, format as E.164 without duplication:
'+' . $phone_digits - If the user provided a
+prefix but the digits don't match billing country code, respect the user's country code:'+' . $phone_digits - Only prepend the billing country code for national numbers (no existing
+prefix and doesn't start with country code): Remove leading zeros withltrim( $phone_digits, '0' )then prepend$calling_code - Validate the final number is between 11-15 characters (E.164 format)
- Preserve the original input to detect if user explicitly provided a
Test Coverage
- Add unit tests for
get_normalized_phone()method intests/phpunit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerceTest.php:- Test phone number with correct country code prefix is not duplicated (e.g.,
+94771770589→+94771770589) - Test phone number with different country code than billing is respected (e.g.,
+2307800099with UK billing →+2307800099) - Test national number without country code gets it prepended (e.g.,
771770589→+94771770589) - Test national number with leading zero (e.g.,
0771770589→+94771770589)
- Test phone number with correct country code prefix is not duplicated (e.g.,
QA Brief
- Follow the steps to reproduce.
- Inspect the data layer in Tag Assistant.
- Observe the
user_data.phone_numberfield shows a correct country code for example+94771770589.
Test Case 1: Phone with correct country code (main bug fix)
- Go to WooCommerce checkout
- Set billing country to Sri Lanka
- Enter phone number with country code:
+94771770589 - Complete the purchase
- On the thank you page, open Tag Assistant and inspect the data layer
- Verify:
user_data.phone_numbershows+94771770589(no duplication) - Expected: Country code
+94should appear only once
Test Case 2: Phone with different country code than billing
- Go to WooCommerce checkout
- Set billing country to United Kingdom
- Enter phone number with Mauritius country code:
+2307800099 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Verify:
user_data.phone_numbershows+2307800099(respects user's country code) - Expected: Should NOT be changed to
+442307800099
Test Case 3: National number without country code
- Go to WooCommerce checkout
- Set billing country to Sri Lanka
- Enter phone number without country code:
771770589 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Verify:
user_data.phone_numbershows+94771770589(country code prepended) - Expected: Billing country code
+94should be automatically added
Test Case 4: National number with leading zero
- Go to WooCommerce checkout
- Set billing country to Sri Lanka
- Enter phone number with leading zero:
0771770589 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Verify:
user_data.phone_numbershows+94771770589(leading zero removed, country code prepended) - Expected: Leading zero should be stripped before adding country code
Test Case 5: International format with 00 prefix (same country)
- Go to WooCommerce checkout
- Set billing country to Sri Lanka
- Enter phone with international 00 prefix:
0094770601017 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Verify:
user_data.phone_numbershows+94770601017(00 prefix removed, no duplication) - Expected: Should strip
00and format as E.164, NOT+9494770601017
Test Case 6: International format with 00 prefix (different country)
- Go to WooCommerce checkout
- Set billing country to Sri Lanka
- Enter Indian phone with 00 prefix:
00919361653826 - Complete the purchase
- Inspect the data layer in Tag Assistant
- Verify:
user_data.phone_numbershows+919361653826(00 prefix removed, India country code respected) - Expected: Should strip
00and preserve India's +91 code, NOT+94919361653826
Changelog entry
- Fix incorrect formatting of WooCommerce enhanced conversions phone numbers.
Metadata
Metadata
Assignees
Labels
P0High priorityHigh priorityTeam MIssues for Squad 2Issues for Squad 2Type: BugSomething isn't workingSomething isn't working