|
1 | | -# Implement an IP to location provider |
| 1 | +--- |
| 2 | +description: >- |
| 3 | + Learn how to implement an IP to location provider in uMarketingSuite to enhance your analytics |
| 4 | + with geographical data from incoming traffic. |
| 5 | +--- |
2 | 6 |
|
3 | | -The uMarketingSuite Analytics provides native support for storing and reporting localization information for your incoming traffic. When we speak of localization, we refer to the ability to identify the (physical) origin of an incoming request. By design, requests sent to your website from a visitor's browser will never contain a location of origin. As the uMarketingSuite does not contain any localization information, you will have to provide your own implementation that provides this. |
| 7 | +# Implement an IP to Location Provider |
4 | 8 |
|
5 | | -Most localization services, such as provided by Maxmind, use IP addresses to perform a (rough) lookup. The information is compiled into a database where lookups can be performed. However IP addresses do not contain any information regarding their (physical) origin, rather they only identify a device on the internet. Because of this the localization information for any given IP address is tracked manually and can change wildly (and sometimes regularly). We recommend either using an external service or acquiring a copy of a GeoIP database for localization lookup purposes. |
| 9 | +The uMarketingSuite Analytics natively supports storing and reporting localization information for incoming traffic. Localization refers to identifying the (physical) origin of an incoming request. Web requests from a visitor's browser do not contain location information, so you must provide an implementation for this. |
| 10 | + |
| 11 | +Most localization services, such as Maxmind, use IP addresses to perform a (rough) lookup. The information is compiled into a database where lookups can be performed. However IP addresses do not contain any information regarding their (physical) origin, rather they only identify a device on the internet. Localization information for any given IP address is tracked manually and can change overtime. We recommend either using an external service or acquiring a copy of a GeoIP database for localization lookup purposes. |
6 | 12 |
|
7 | 13 | ## Implementation |
8 | 14 |
|
9 | | -Once you have a service that can provide localization information, it is very easy to integrate this into the uMarketingSuite. For this purpose we will be implementing the interface **uMarketingSuite.Business.Analytics.Processing.Extractors.IRawPageviewLocationExtractor**. As the name suggests, this interface defines a service that allows the provision of localization information given a pageview, defined as a single visit from a single visitor on a single page at one specific point in time. The pageview will contain the property **IpAddress** which you can use for the Geo IP lookup. |
| 15 | +Once you have a service that can provide localization information, integrating it into uMarketingSuite is straightforward. |
10 | 16 |
|
11 | | -First we define a class implementing **ILocation**, which will hold the localization information that will be returned through the interface in our implementation: |
| 17 | +For this purpose, implement the interface **uMarketingSuite.Business.Analytics.Processing.Extractors.IRawPageviewLocationExtractor**. This interface allows the localization information for a pageview, defined as a single visitor's visit to a specific point in time. The pageview contains the property **IpAddress** which can be used for Geo IP lookup. |
12 | 18 |
|
13 | | - using uMarketingSuite.Business.Analytics.Processed;public class GeoIpLocation : ILocation{ |
14 | | - public string Country { get; set; } |
15 | | - public string County { get; set; } |
16 | | - public string Province { get; set; } |
17 | | - public string City { get; set; } |
18 | | - } |
| 19 | +First, define a class that implements **ILocation**, to hold the localization information that will be returned through the interface in our implementation: |
19 | 20 |
|
20 | | -We are now ready to implement the location extractor. First, we will read and validate the incoming IP address. We also filter out local IP addresses with the native **IsLoopback** method. After this you can call your own Geo IP localization implementation: |
| 21 | +```cs |
| 22 | +using uMarketingSuite.Business.Analytics.Processed;public class GeoIpLocation : ILocation{ |
| 23 | + public string Country { get; set; } |
| 24 | + public string County { get; set; } |
| 25 | + public string Province { get; set; } |
| 26 | + public string City { get; set; } |
| 27 | +} |
| 28 | +``` |
21 | 29 |
|
22 | | - using uMarketingSuite.Business.Analytics.Processing.Extractors;public class MyCustomLocationExtractor : IRawPageviewLocationExtractor |
| 30 | +Next, implement the location extractor to read and validate the incoming IP address and filter out local IP addresses with the native **IsLoopback** method. Then, call your Geo IP localization implementation: |
| 31 | + |
| 32 | +```cs |
| 33 | +using uMarketingSuite.Business.Analytics.Processing.Extractors;public class MyCustomLocationExtractor : IRawPageviewLocationExtractor |
| 34 | +{ |
| 35 | + public ILocation Extract(IRawPageview rawPageview) |
23 | 36 | { |
24 | | - public ILocation Extract(IRawPageview rawPageview) |
25 | | - { |
26 | | - if (!IPAddress.TryParse(rawPageview?.IpAddress, out var ipAddress) || IPAddress.IsLoopback(ipAddress)) return null; |
| 37 | + if (!IPAddress.TryParse(rawPageview?.IpAddress, out var ipAddress) || IPAddress.IsLoopback(ipAddress)) return null; |
27 | 38 |
|
28 | | - string country, county, province, city; |
| 39 | + string country, county, province, city; |
29 | 40 |
|
30 | | - //... |
31 | | - // Perform your own GEO IP lookup here |
32 | | - // ... |
| 41 | + //... |
| 42 | + // Perform your own GEO IP lookup here |
| 43 | + // ... |
33 | 44 | |
34 | | - var location = new GeoIpLocation |
35 | | - { |
36 | | - Country = country, |
37 | | - County = county, |
38 | | - Province = province, |
39 | | - City = city |
40 | | - }; |
| 45 | + var location = new GeoIpLocation |
| 46 | + { |
| 47 | + Country = country, |
| 48 | + County = county, |
| 49 | + Province = province, |
| 50 | + City = city |
| 51 | + }; |
41 | 52 |
|
42 | | - return location; |
43 | | - } |
| 53 | + return location; |
44 | 54 | } |
| 55 | +} |
| 56 | +``` |
45 | 57 |
|
46 | | -Lastly we will need to let the IoC container know to use your implementation for the **IRawPageviewLocationExtractor**. The uMarketingSuite has a default implementation of this service, which only returns null. This default service is registered using Umbraco's **RegisterUnique** method. To override this service we will need to call RegisterUnique **after** the uMarketingSuite dependencies have been initialised, which is **after** the uMarketingSuite's **UMarketingSuiteApplicationComposer**: |
| 58 | +Lastly, let the IoC container know to use your implementation for the **IRawPageviewLocationExtractor**. The uMarketingSuite has a default implementation of this service, which only returns null. This default service is registered using Umbraco's **RegisterUnique** method. To override this service, call RegisterUnique **after** the uMarketingSuite dependencies have been initialized, which is **after** the uMarketingSuite's **UMarketingSuiteApplicationComposer**: |
47 | 59 |
|
48 | | - using uMarketingSuite.Business.Analytics.Processing.Extractors; |
49 | | - using uMarketingSuite.Common.Composing; |
50 | | - using Umbraco.Core; |
51 | | - using Umbraco.Core.Composing; |
| 60 | +```cs |
| 61 | +using uMarketingSuite.Business.Analytics.Processing.Extractors; |
| 62 | +using uMarketingSuite.Common.Composing; |
| 63 | +using Umbraco.Core; |
| 64 | +using Umbraco.Core.Composing; |
52 | 65 |
|
53 | | - [ComposeAfter(typeof(UMarketingSuiteApplicationComposer))] |
54 | | - public class UMarketingSuiteComposer : IComposer |
| 66 | +[ComposeAfter(typeof(UMarketingSuiteApplicationComposer))] |
| 67 | +public class UMarketingSuiteComposer : IComposer |
| 68 | +{ |
| 69 | + public void Compose(IUmbracoBuilder builder) |
55 | 70 | { |
56 | | - public void Compose(IUmbracoBuilder builder) |
57 | | - { |
58 | | - builder.services.AddUnique<IRawPageviewLocationExtractor, MyCustomLocationExtractor>(); |
59 | | - } |
| 71 | + builder.services.AddUnique<IRawPageviewLocationExtractor, MyCustomLocationExtractor>(); |
60 | 72 | } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +After implementing this, the uMarketingSuite will begin collecting and displaying localization information for pageviews. This can be viewed in the Analytics section of the uMarketingSuite dashboard. |
61 | 77 |
|
62 | | -After the service has been implemented correctly, the uMarketingSuite should start collecting and displaying localization information for pageviews. This can be viewed in the Analytics section of the uMarketingSuite dashboard in Umbraco. Note that if your custom implementation returns **null** as **ILocation** this will be displayed in uMarketingSuite as "Unknown". |
| 78 | +{% hint style="info" %} |
| 79 | +If the custom implementation returns **null**, **ILocation** will display as "Unknown". |
| 80 | +{% endhint %} |
63 | 81 |
|
64 | | -**Keep in mind that the LocationExtractor is only executed for incoming pageviews and will not retroactively apply to historical data**. |
| 82 | +{% hint style="info" %} |
| 83 | +The LocationExtractor only processes new pageviews and will not apply retroactively to historical data. |
| 84 | +{% endhint %} |
65 | 85 |
|
66 | | -## Analytics location report |
| 86 | +## Analytics Location Report |
67 | 87 |
|
68 | | -The localization information is displayed under the tab Location in the Analytics section of the uMarketingSuite dashboard: |
| 88 | +The localization information is displayed under the **Location** tab in the **Analytics** section of the uMarketingSuite dashboard: |
69 | 89 |
|
70 | 90 | ![Location tab, located under the Analytics section]() |
71 | 91 |
|
72 | 92 | The graph contains all sessions that were started for the given time period, similar to the tab "**New and returning visitors**". As this information is not location bound this graph is always displayed, even if no localization information is present. |
73 | 93 |
|
74 | | -Underneath the graph you may find the table containing session and pageview information per country. If you have not implemented the LocationExtractor service, or if the pageviews for the given date range do not contain location information, the following error is displayed instead of the table: |
| 94 | +Underneath the graph you may find the table containing session and pageview information per country. If the LocationExtractor service is not implemented or the pageviews for the given date range do not contain location information, the following error is displayed: |
75 | 95 |
|
76 | 96 | ![Location table - missing data error]() |
77 | 97 |
|
78 | 98 | If the pageviews contain location information, the table with countries is displayed: |
79 | 99 |
|
80 | 100 | ![Location table with data]() |
81 | 101 |
|
82 | | -From country, you can drilldown to city. This will then filter the displayed graph and table data to only display session and pageview information for the selected country. Even though the uMarketingSuite does support the storage for county and province as well, currently the UI only supports displaying data by country and city. |
| 102 | +From country, you can drilldown to city. This will then filter the displayed graph and table data to only display session and pageview information for the selected country. Even though the uMarketingSuite does support the storage for county and province as well, currently the UI only supports displaying data by country and city. |
0 commit comments