Adding geolocation to Contact

Graham Mott asked on August 23, 2019 12:11

Hello,

I have followed the tutorial on how to add geolite2 to my system but I was confused on how I would go about binding their location to their contact in MVC. Currently we create a new ContactInfo and bind their lastname, firstname and email, then we use the provider to submit the new contact. How would I add the city, country etc.?

ContactInfo newContact = new ContactInfo
            {
                ContactLastName = user.LastName,
                ContactFirstName = user.FirstName,
                ContactEmail = user.Email,
            };
ContactInfoProvider.SetContactInfo(newContact);

Recent Answers


David te Kloese answered on August 23, 2019 16:31

Did you follow the "MVC" docs:

docs.kentico.com/k12sp/.../using-geolocation-for-contacts

Based on docs the following should create a contact if there isn't one identified based on context.

// Gets the current contact
ContactInfo currentContact = ContactManagementContext.GetCurrentContact();

Not sure but would assume if everything is setup correctly, it would also try to map the GEO data?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 23, 2019 16:34 (last edited on August 23, 2019 16:34)

You need to use GeoIpHelperClass and take a look at this answer

0 votesVote for this answer Mark as a Correct answer

Graham Mott answered on August 23, 2019 16:35

Hey David,

I did follow the first part and I happened to test the GetCurrentContact() about 5 minutes ago but it gives empty values for the location values.

0 votesVote for this answer Mark as a Correct answer

Graham Mott answered on August 23, 2019 18:01

Hi Peter,

I have tried using that and I either receive null or empty strings for the different implementations from the stackoverflow

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 23, 2019 19:33 (last edited on August 23, 2019 20:52)

Well, you may get the client IP address from the last user logon (as it is described below in the tread) and then use GeoIpHelper to resolve it. Essentially you can query cms_user table select Convert(xml,UserLastLogonInfo) from CMS_User Where UserID = 53 UserLastlogonInfo will contain xml like so:

<info>
  <agent>Mozilla/5.0</agent>
  <ip>127.0.1.1</ip>
</info>

You can grab the ip and resolve it with GeoIpHelper

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.