1. Home
  2. Analytics
  3. Extending Analytics
  4. Getting the correct IP address

Getting the correct IP address

By default the uMarketingSuite extracts the IP address for the given request by inspecting the request's UserHostAddress. For websites that run on servers directly facing the internet, the IP Address is resolved correctly.

If you find that the IP addresses are not resolved correctly, your website might be running behind a load balancing server or another protected environment. As Load Balancers forward the request to the destination server on behalf of the original requester, this default IP resolving strategy will cause the IP of the loadbalancer to be resolved instead of the client's IP address.

In most of these cases the client's IP Address is included as a request header as it is forwarded to the webserver. As there is no industry standard as to what the name of this request header should be, each vendor will have their own strategy for forwarding this IP Address. If you wish to correct the IP Address, you will have to override the uMarketingSuite's default IP address extractor.

The default extractor looks like this:

using System.Web;
using uMarketingSuite.Business.Analytics.Collection.Extractors;

public class HttpContextIpAddressExtractor : IHttpContextIpAddressExtractor { public string ExtractIpAddress(HttpContextBase context) => context?.Request?.UserHostAddress; }

First you will have to implement your own IHttpContextIpAddressExtractor, and extracting the correct IP Address from the provided context.

Secondly, you will have to tell Umbraco to use your extractor instead of the default extractor. This can be done by using Umbraco's IUserComposer. Create another class in your project that looks like this:

using uMarketingSuite.Business.Analytics.Collection.Extractors;
using Umbraco.Core.Composing;
using Umbraco.Core; [ComposeAfter(typeof(uMarketingSuite.Business.Analytics.Collection.Extractors.AnalyticsExtractorsComposer))] public class CustomIpExtractorUserComposer : IUserComposer { public void Compose(Composition composition) { composition.RegisterUnique<IHttpContextIpAddressExtractor, MyIpAddressExtractor>(); } }

It is important to note that your UserComposer makes the adjustments to the service registration AFTER the uMarketingSuite has initialized. This can be enforce this by using the ComposeAfterAttribute. Forgetting to add this attribute could cause Umbraco to run your IUserComposer before the uMarketingSuite's composer, causing your changes to be overwritten.

Also make sure you are using .RegisterUnique<...>() instead of .Register<...>(). Under normal circumstances you could use register when you have multiple implementations of a single service. However as we don't want Umbraco to hold more than one implementation for the extractor service (you want your own extractor to be resolved instead), we will be using RegisterUnique to overwrite the uMarketingSuite's extractor.

After implementing both classes and running your project, you should see the that your extractor gets called to resolved IP addresses. It is also possible to verify the output of your extractor by inspecting the database table uMarketingSuiteAnalyticsIpAddress, but keep in mind that the last portion of your IP address might get anonymized (read: set to 0) if you have this option enabled in your uMarketingSuite's configuration file.