Using Flow to get the country of a website visitor

Using Flow to get the country of a website visitor

There are many tools for tracking visitors and page views to web sites, but in certain circumstances this might be something that you would want to implement on your own. It is simple enough to get the IP address of the visitor of the web site, but usually in order to get the country we need to call some sort of API. There are various API provides that can give you this information, and in the case of this blog post, I selected a provide called ipgeolocation (ipgeolocation.io).

The first thing that we need to do is go to the ipgeolocation web site and get an api key. You can get an free api key which will give you up to 1000 calls per day (non-commercial). Once you login and create your account, you can find the key on their dashboard (as shown in the image below).

IPGeoLocation

Now, we can start creating our flow that will trigger on the creation of a new visitor records. The visitor records are created from the web site when a new page is rendered. We will launch Microsoft flow and navigate to a solution that we want to use the flow, and create a new flow.

The first step in the new flow will be to use a When a record is created trigger using the Common Data Service (Current Environment Connector), and be able to retrieve the IP address of the created record.

Common Data Service Current Environment Connector

Although the API key can be hard coded in the HTTP request, I used the initialize variable to store the Api Key so that I can use it in the Http Request. You can also store it in a CDS environmental variable and retrieve it later or elsewhere.

Flow Initialize variable

Next we make a call to the IPGeolocation API using the GET method. You can view the documentation of ipgeolocation at https://ipgeolocation.io/documentation/ip-geolocation-api.html.

Notice that we pass the API key variable that we previously initialized and the IP address that we got from the Create record trigger from CDS. We also append the names of the fields that we want to retrieve or we can leave it empty and that will retrieve all fields.

Flow HTTP Request

Next we will parse the results that we retrieved from the api using a Parse JSON action. We can use a Generate from Sample (which you can copy from the documentation web page) to create the schema. We pass the Body of the HTTP GET response to the Parse JSON action.

Flow Parse JSON

Finally, we call the Update a record action of the Common Data Service Current Environment connector to update the Country back to the entity record that was recently created.

Common Data Service Current Environment Update record

We can now run the process by navigating to a new page on our web site, which will trigger the creation of the record and then trigger the flow. When we look at the Output of the Parse JSON action, we can see that the results are showing the Country for the IP address.

Flow Parse JSON Results

Simple enough. You can now use similar logic in your own web apps (whether using SQL as your backend, CDS or another data system).