Developer forum

Forum » CMS - Standard features » Find out what country the user come from

Find out what country the user come from

Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Does Dynamicweb have some kind of tag that show what country a user come from based by ex. IP?
We need to set currency based on the user country, and would like to know if there are something in DW we can use, or if we have to code something our selves.


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Not something that is publicly available. However, it's easy to do with Geo IP: https://www.maxmind.com/en/geoip-demo

They provide an IP database and .NET classes that expose methods to convert an IP into a country code.

Imar

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

You could use the GeolocationService.

Example:

var ipAddress = Dynamicweb.Context.Current.Request.UserHostAddress;
var results = Dynamicweb.Analytics.Utilities.GeolocationService.LookupBasic(new[] { ipAddress });
var countryCode = results.ContainsKey(ipAddress) ? results[ipAddress].CountryCode : string.Empty;

However, I'm not sure how well it performs. You might want to persist/cache this for the current user or session.

Votes for this answer: 1
 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Thanks, works like a charm!

 
Nicolai Pedersen
Reply

I have to advice against this solution. It makes a call everytime to a service hosted by us used for the lead tool. It is slow and cannot be guarenteed to live forever...

BR Nicolai

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Do we have any alternatives to this?

 
Nicolai Pedersen
Reply

In 9.6 and newer we actually track this - but there is no public available API to get hold of the data. You can get hold of Dynamicweb.Tracking.Tracker.Current - it has an internal property with the session that again has a property of the country. But it is internal. You might be able to get hold of the information using reflection.

Or more simple, you can use this one:

https://dev.maxmind.com/geoip/geoip2/geolite2/

 

You must be logged in to post in the forum