Set Geo Coordinates using Update Interaction Contact (best record) BAdI in Marketing Cloud
Using below steps you can add Longitude and Latitude to any contact before it is created in system.
The logic is ignored if the incoming payload already has longitude/latitude information in it.
the BAdI logic will read the Geo Location from the available free database openstreetmap.org based on the contact’s City and Country and will update the contact
Go to marketing cloud
open app Custom Communication Scenario
go to outbound services, give description and service id like shown in below screenshot
save and publish
go to communication system and create a new communication system with system ID, Name, Host Name and Port like below.
make sure to give User for outbound communication as None
now go to Communication arrangement
create a new one with the scenario ID which was created in first stem
use the system created in above step
it ill select the user as None from communication system
and it will show the outbound service from scenario
now go to Custom fields and Logic App
go to tab custom Logic
click on + icon and create a new BADI
select the business context as Marketing: Contact and Corporate
and choose the Badi Update Interaction Contact(best record)
and create the badi with some meaningful description
1 use the communication scenario and outbound service to access the service
2 loop at the contact data available in BAdI and check if country is filled and longitude&Latitude are initial.
3 build the query by selecting city and country value from contact
4 send the request to service and receive the address fields as response
5 set contact’s longitude and latitude from the incoming response
DATA: lr_ic_root_new TYPE REF TO cuan_s_ce_ic_rt_badi. DATA: ls_body TYPE string. "create http client to access the outbound service DATA(lo_client) = cl_ble_http_client=>create( communication_scenario = 'YY1_API_GPS_OPENSTREETMAPS' outbound_service = 'YY1_APIOPENSTREETMAPS_REST'). "creation of the service request DATA(lo_request) = cl_ble_http_request=>create( )->set_method( if_ble_http_request=>co_method-get ). "process all updated interaction contacts LOOP AT new_contact_root REFERENCE INTO lr_ic_root_new. if lr_ic_root_new->country is not initial and lr_ic_root_new->latitude = 0 and lr_ic_root_new->longitude = 0. *fetch the data on city level DATA(lv_query) = '?format=json' && '&city=' && lr_ic_root_new->city1 && '&country=' && lr_ic_root_new->country. "extend the uri by the specified query parameters lo_request->set_resource_extension( lv_query ). TRY. "send a request and receive a response" DATA(lo_response) = lo_client->send( lo_request ). CATCH cx_ble_http_exception . lr_ic_root_new->yy1_street2_mps = 'exception'. ENDTRY. "get the body of the response ls_body = lo_response->get_body( ). ""lr_ic_root_new->yy1_street2_mps = ls_body . "find the appropreate coordinates from the answer lr_ic_root_new->latitude = substring_before( val = substring_after( val = ls_body sub = '"lat":"') sub = '"' ). lr_ic_root_new->longitude = substring_before( val = substring_after( val = ls_body sub = '"lon":"') sub = '"' ). endif. ENDLOOP.
How to Test the functionality
To test the feature, simple go to Contacts app and create a contact with valid city and country and save.
to enable the fields longitude and latitude on contact Profile, open the contact, click on Adapt UI from the right side menu
right click anywhere below the Additional Data
click on Add Field from the popup
and select longitude and latitude
save, save and exit.
How to Test in Postman
it is also possible to call this free service from postman to do quick test and compare the results.
use below link in Postman, you can change the country and city and test further. also you can add a street to receive more precise coordinates.
If you are receiving contacts to Marketing cloud without Geo Coordinates and if you still wish to segment the contacts based on their Geo Location, you can enable this feature.
Depending on the data that you receive, you can easily change the above implementation to use different address fields like Street/Pincode to determine the Geo Coordinates.
Please share yours thoughts or feedback by leaving a comment below. 🙂