Skip to content

feat: default APM phone dialing code from browser locale#272

Draft
roshan-gorasia-cko wants to merge 2 commits into
masterfrom
feat/apm-locale-default-dialing-code
Draft

feat: default APM phone dialing code from browser locale#272
roshan-gorasia-cko wants to merge 2 commits into
masterfrom
feat/apm-locale-default-dialing-code

Conversation

@roshan-gorasia-cko

@roshan-gorasia-cko roshan-gorasia-cko commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What

The embedded APM (client.apm.authorization / apm.tokenization) phone field now defaults its dialing code from the browser locale instead of always using the first country in the list.

Previously the default was dialing_codes[0], which — after the list is sorted alphabetically by country name — is the same country for everyone regardless of where the shopper is. Merchants can already prefill the number and dialing code via initialData.phone_number; this only changes the default when nothing is prefilled.

How

  • Add two helpers in src/apm/utils.ts:
    • getBrowserRegion() — extracts the region subtag from navigator.languages[0] || navigator.language (e.g. en-GBGB).
    • getDefaultDialingCode(dialing_codes) — returns the code whose region_code matches that region, else falls back to dialing_codes[0].
  • Use them in the Phone component (src/apm/elements/phone.ts) and the initial form seed (src/apm/views/NextSteps.ts).
  • Explicit initialData.phone_number.dialing_code still takes precedence; the shopper can still change the country.

Tests

  • Adds Vitest and unit tests for both helpers (test/apm/locale.test.ts).
  • Tests live under a top-level test/ directory, outside the SDK build, so no test code ships in the bundle.
  • yarn test is wired into the existing lint/build CI workflow.

roshan-gorasia-cko and others added 2 commits July 22, 2026 16:55
The new embedded APM (client.apm.authorization / apm.tokenization) phone
field previously defaulted its dialing code to dialing_codes[0], which is
just the first country alphabetically (~Afghanistan) after the API sorts
the list by name. Merchants could already prefill both the number and the
dialing code via initialData.phone_number, but there was no sensible
default when nothing is prefilled.

Derive the default dialing code from the browser locale's region subtag
(e.g. en-GB -> +44), falling back to dialing_codes[0] when the locale has
no region or no matching code. Explicit initialData still takes precedence.

- add getBrowserRegion / getDefaultDialingCode helpers in src/apm/utils.ts
- use them in the Phone component (the authoritative displayed/emitted
  default) and in the NextSteps initial form seed

Also introduce the repo's first test harness:
- Vitest, with tests under top-level test/ (kept out of every SDK build
  glob, so no test code ships in dist/processout.js)
- a loader that transpiles and evaluates the real namespace source with an
  injected navigator, so the shipped logic is tested without adding any
  test-only hooks to production files
- wire `yarn test` into the existing build-lint-test CI workflow

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the embedded APM phone field behavior to choose a default dialing code based on the shopper’s browser locale (region), rather than always picking the first alphabetically-sorted country entry. It also introduces a small Vitest-based unit test setup to validate the new locale helpers.

Changes:

  • Added getBrowserRegion() and getDefaultDialingCode() helpers in src/apm/utils.ts and used them when initializing phone values.
  • Updated APM form seeding (NextSteps) and the Phone element to use the locale-based default dialing code.
  • Added Vitest config + unit tests, and wired tests into CI and yarn verify.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
yarn.lock Adds lockfile entries for Vitest (and its dependency graph).
vitest.config.ts Introduces Vitest configuration for running TypeScript unit tests in Node.
test/support/loadNamespace.ts Adds a helper to transpile/evaluate src/apm/utils.ts for testing internal-namespace exports.
test/apm/locale.test.ts Adds unit tests for getBrowserRegion and getDefaultDialingCode.
src/apm/views/NextSteps.ts Uses the new helper when seeding default phone dialing code.
src/apm/utils.ts Adds the locale parsing + dialing code selection helpers.
src/apm/elements/phone.ts Uses the new helper to choose an initial/default dialing code.
package.json Adds test scripts, adds Vitest dependency, and extends verify to run tests.
eslint.config.mjs Marks tests and Vitest config as Node-environment files for ESLint globals.
.github/workflows/build-lint-test.yml Adds a CI step to run yarn test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/apm/utils.ts
Comment on lines +42 to +44
const region = getBrowserRegion();
const match = region && dialing_codes.filter(c => c.region_code && c.region_code.toUpperCase() === region)[0];
return (match || dialing_codes[0]).value;
Comment thread package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants