Capture IP Address and Geolocation Data with Dynamics 365 Marketing Forms

Dynamics 365 Marketing forms can be embed on your own external website to submit back both mapped (to contact or lead) or unmapped (stored as form submissions) values into Dynamics 365.

In terms of capturing extra gems on form submissions on previous posts I have covered:

This post was inspired by a question sent in from one of the three people who read my blog (not including my mum, she doesn’t care either) asking if its possible to capture the IP Address on the form submission. I trotted off to ask my good friend google, mushed together some super handy information from this Stack Overflow post with my basic knowledge of marketing Forms and JavaScript to find out that you can indeed have the IP Address… AND LOADS OF OTHER COOL STUFF TOO!

The steps in this post show you how to create the form embed script and get hold of the correct Field ID where you want to capture the IP Address so I will skip straight to the scripty bit you came for.

Register for a free IP lookup service

As per the advice of the really smart person on the stack overflow post above, you need to get hold of an IP lookup service. He provides a detailed analysis/run down on each of them, including limitations. I went with the first one I saw - Abstract API. It allows up to 10,000 requests a month for free and options to upgrade/scale as needed (please do you own due diligence on this one before going live with a free account!). You need to sign up and obtain your free API key, hereon in referenced to as 'YourAPIKey’.

Capture IP Address and other Geolocation data

The API we are using returns the IP Address as well as heaps of other geolocation-based information such as country, postcode, connection and timezone details (full list here Abstract API Geolocation Docs). The example below extracts the IP address specifically and also the entire data set as JSON into a separate field for your perusal. You need to replace the following items in the below script with your own items - instructions on getting field IDs in this post and instructions on the API key above.

  • YourAPIKey - the API key for Abstract API that you signed up for as a free account

  • IPAdddressFieldID - the marketing form field ID where you want to capture the IP Address

  • AbstractAPIResponseFieldID - the marketing form field ID where you want to capture the full Geolocation data set

Add the JavaScript to the page <body>

<div style="display:none"><script>
MsCrmMkt.MsCrmFormLoader.on("afterFormLoad", 
function() {
    let apiKey = 'YourAPIKey';
    $.getJSON('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey, function(data) {
        let AbstractAPI = JSON.stringify(data, null, 2);
        let IPAddress = data.ip_address;
        document.getElementById("IPAdddressFieldID").value = IPAddress;  
        document.getElementById("AbstractAPIResponseFieldID").value = AbstractAPI;
    }); 
});
</script></div>

Add the JavaScript reference to the page <head>

And finally, add the below script reference into the header of your page

                        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

You asked for IP address, you received a head of geolocation data. Thank you Rachel for the question and learning/blog inspiration. Enjoy!

P.S I have no affiliation to Abstract API it was just easy to find and use.

Links
Official Microsoft instructions here on a simple embedded form scenario
Further JavaScript for embedded form extensibility options documentation
Abstract API Geolocation Docs
A sample IP Address and Geolocation data capture marketing form

Previous
Previous

Add the Insights tab to Lead and Contact forms in Dynamics 365 Marketing

Next
Next

‘Distribution List’ Emails and Appointments with Power Automate