Suppose you are an e-commerce business that operates in multiple countries and receives orders from customers all over the world. To ensure smooth communication with your customers, you need to store their phone numbers with the correct international prefix. However, manually formatting each phone number can be time-consuming and error-prone, especially if you have a large customer base.
Approach
To handle the complication of phone numbers with international prefixes, this code utilizes an approach that involves an HTTP request to the restcountries.com REST API service to retrieve the international dialing code for the country associated with the phone number.
If the request is successful, the international dialing code is added to the phone number using the "addInternationalCode" function. The function checks if the phone number already has the international prefix and adds it only if it's not already present. The formatted phone number and international dialing code are then saved to the formattedPhone and internationalDialingCode respectively.
Please note that in order for the transformation to work correctly, it requires the presence of the 'country' field in English and the 'phone' field in the event object. In the provided code, these fields are accessed using the following lines:
const country = event.context?.traits?.address?.country;
const phone = event.context?.traits?.phone;
Thank you for contributing to RudderStack Transformations. Your submission will be reviewed soon.
Do follow the transformations-challenge channel on RudderStack slack community for updates on the challenge.
Contact Details
p.giannone36@gmail.com
Language
Javascript
Category
Data Processing and Enrichment
Description
Purpose
Suppose you are an e-commerce business that operates in multiple countries and receives orders from customers all over the world. To ensure smooth communication with your customers, you need to store their phone numbers with the correct international prefix. However, manually formatting each phone number can be time-consuming and error-prone, especially if you have a large customer base.
Approach
To handle the complication of phone numbers with international prefixes, this code utilizes an approach that involves an HTTP request to the restcountries.com REST API service to retrieve the international dialing code for the country associated with the phone number. If the request is successful, the international dialing code is added to the phone number using the "addInternationalCode" function. The function checks if the phone number already has the international prefix and adds it only if it's not already present. The formatted phone number and international dialing code are then saved to the
formattedPhone
andinternationalDialingCode
respectively.Please note that in order for the transformation to work correctly, it requires the presence of the 'country' field in English and the 'phone' field in the event object. In the provided code, these fields are accessed using the following lines:
Code Block
Input Payload for testing
License