In today’s global digital landscape, organizations must often tailor their online experiences to comply with regional regulations, language preferences, or specific marketing strategies. Implementing country-based access restrictions can be crucial for companies leveraging Salesforce Experience Cloud (formerly called Community Cloud). This ensures that content is delivered appropriately and compliantly to users based on their location.
One effective way to achieve this is by integrating the Google Maps API to determine a user’s location and enforce access controls accordingly. In this blog, we’ll explore how to implement country-based restrictions in Salesforce Experience Cloud using Google Maps API and discuss the importance of this approach.
Why Implement Country-Based Restrictions?
- Regulatory Compliance
- Localization
- Security
- Marketing Strategies
Integrating Google Maps API with Salesforce Experience Cloud
To implement country-based restrictions, we can use the Google Maps Geolocation and Geocoding APIs to determine the user’s location based on their IP address and coordinates. Here’s a step-by-step guide on how to achieve this:

1. Setting Up Custom Metadata for Country Restrictions
Firstly, create a custom metadata type in Salesforce to store allowed countries and the Google Maps API key.
public class LocationController {
@AuraEnabled(cacheable=true)
public static FCXMCountry__mdt getCountryRestrictions() {
return [SELECT AllowedCountries__c, API_Key__c
FROM FCXMCountry__mdt
WHERE DeveloperName = ‘Default_Restrictions’ LIMIT 1];
}
}
2. Fetching the User’s Location
In Lightning Web Component (LWC), use the Geolocation API to get the user’s approximate latitude and longitude.
fetchLocation(apiKey) {
const geolocationEndpoint = `https://www.googleapis.com/geolocation/v1/geolocate?key=${apiKey}`;
fetch(geolocationEndpoint, {
method: ‘POST’,
headers: { ‘Content-Type’: ‘application/json’ },
body: JSON.stringify({ considerIp: true })
})
.then(response => response.json())
.then(data => {
const { lat, lng } = data.location;
this.fetchCountry(lat, LNG);
})
.catch(error => {
this.error = ‘Error fetching location data’;
this.isLoading = false;
});
}
3. Determining the Country from Coordinates
Use the Geocoding API to convert the latitude and longitude into a country name.
@AuraEnabled(cacheable=true)
public static String fetchCountryFromApex(Double latitude, Double longitude) {
FCXMCountry__mdt mapsAPI = getCountryRestrictions();
if (mapsAPI == null || String.isBlank(mapsAPI.API_Key__c)) {
return ‘Invalid API Key’;
}
return getCountry(latitude, longitude, mapsAPI.API_Key__c);
}
@AuraEnabled(cacheable=true)
public static String getCountry(Double latitude, Double longitude, String mapsAPIKey) {
// … [Code to call the Geocoding API and parse the country] …
}
4. Implementing Access Control Logic
After determining the user’s country, compare it against the list of allowed countries and navigate the user accordingly.
processCountry(country) {
if (!country || country.trim() === ”) {
this.handleError(‘Invalid country’);
return;
}
this.location = { country: country };
if (!this.allowedCountries.includes(country)) {
this[NavigationMixin.Navigate]({
type: ‘standard__webPage’,
attributes: { url: ‘/restriction’ }
});
setTimeout(() => { this.removeUriParam(); }, 1000);
} else {
this.isLoading = false;
this.showContent = true;
}
}
5. Enhancing User Experience with Cookies
To optimize performance and user experience, store the user’s country in a cookie to avoid redundant API calls on subsequent visits.
getCookie(name) {
// …Code to get cookie value] …
}
setCookie(name, value, hours) {
// … [Code to set cookie with expiration] …
}
The Importance of This Approach
- Efficiency
- User Experience
- Compliance and Control
- Scalability
Best Practices and Considerations
- API Key Security
- Error Handling
- Performance Optimization
- Privacy Compliance
- Conclusion
Integrating Google Maps API with Salesforce Experience Cloud for country-based access control is a powerful way to enhance user experience while ensuring compliance and security. It allows organizations to dynamically serve content based on the user’s location, providing a personalized and legally compliant interface.
For any questions during implementation, please feel free to reach out to our company. We’re here to assist you in making your Salesforce Experience Cloud site more secure and user-friendly.
