我创建了一个自定义的GEOIP提供程序,但我总是在Experience Analytics中返回未知国家/地区的代码。
以下是我的代码:
public class GeoLiteProvider : LookupProviderBase
{
public override WhoIsInformation GetInformationByIp(string ip)
{
const string ModuleName = "Utilities.GeoIP";
string vLogging = Settings.GetSetting(ModuleName + ".VerboseLogging", "false");
bool vLoggingResult = false;
bool verboseLogging = bool.TryParse(vLogging, out vLoggingResult);
if (!vLoggingResult)
{
verboseLogging = false;
}
WebRequest rssReq = WebRequest.Create("http://www.telize.com/geoip/" + ip);
WebProxy px = new WebProxy("http://www.telize.com/geoip/" + ip, true);
rssReq.Proxy = px;
rssReq.Timeout = 2000;
WhoIsInformation information = new WhoIsInformation();
try
{
WebResponse rep = rssReq.GetResponse();
StreamReader content = new StreamReader(rep.GetResponseStream());
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
System.Collections.Generic.Dictionary<string, object> jsonObject = (System.Collections.Generic.Dictionary<string, object>)serializer.DeserializeObject(content.ReadToEnd());
if (jsonObject != null)
{
information.Country = jsonObject["country"] != null ? jsonObject["country"].ToString() : string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.Country = " + information.Country, this);
}
information.Region = jsonObject["region"] != null ? jsonObject["region"].ToString() : string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.Region = " + information.Region, this);
}
information.City = jsonObject["city"] != null ? jsonObject["city"].ToString() : string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.City = " + information.City, this);
}
information.PostalCode = string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.PostalCode = " + information.PostalCode, this);
}
information.Latitude = double.Parse(jsonObject["latitude"].ToString());
if (verboseLogging)
{
Log.Info(ModuleName + ": information.Latitude = " + information.Latitude, this);
}
information.Longitude = double.Parse(jsonObject["longitude"].ToString());
if (verboseLogging)
{
Log.Info(ModuleName + ": information.Longitude = " + ((information.Longitude == 0) ? string.Empty : information.Longitude.ToString()), this);
}
information.MetroCode = string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.MetroCode = " + information.MetroCode, this);
}
information.AreaCode = jsonObject["area_code"] != null ? jsonObject["area_code"].ToString() : string.Empty;
if (verboseLogging)
{
Log.Info(ModuleName + ": information.AreaCode = " + information.AreaCode, this);
}
}
else
{
Log.Info(ModuleName + ": IP location cannot be determined. IP Address: " + ip, this);
information.BusinessName = Settings.GetSetting(ModuleName + ".NoLocationText", "Not Available");
}
if (verboseLogging)
{
Log.Info(ModuleName + ": IP Lookup Complete", this);
}
}
catch(Exception ex)
{
Log.Error(ModuleName + ": Exception occurred. Exception: " + ex.Message, this);
}
return information;
}}
配置补丁:
<lookupManager>
<patch:attribute name="defaultProvider">geoLite</patch:attribute>
<providers>
<add patch:after="*[@type='Sitecore.Analytics.Lookups.MaxMindProvider,Sitecore.Analytics']" name="geoLite" type="Core.Utilities.LookupProvider.GeoLiteProvider, Core.Utilities" />
</providers>
</lookupManager>
我还创建了一个覆盖IP地址的管道,以便我可以在本地计算机上验证它但仍然失败。
public class OverrideIPAddress
{
public void Process(StartTrackingArgs args)
{
Tracker.Current.Interaction.Ip = System.Net.IPAddress.Parse("1.68.0.0").GetAddressBytes();
}
}
Sitecore.Analytics.Tracking.Config:
<startTracking>
<processor type="Sitecore.Analytics.Pipelines.StartTracking.RaiseStartTracking, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.InitializeTracker, Sitecore.Analytics" />
<processor type="Utilities.GEOIP.OverrideIPAddress, Core.Utilities" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.TrackerInitialized, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.UpdateGeoIpData, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.ProcessQueryStringCampaign, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.ProcessQueryStringPageEvent, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartTracking.ProcessQueryStringTriggers, Sitecore.Analytics">
<triggers hint="raw:AddTrigger">
<trigger querystring="sc_rss" eventname="RSS"/>
</triggers>
</processor>
<processor type="Sitecore.Analytics.Pipelines.StartTracking.ProcessItem, Sitecore.Analytics"/>
</startTracking>
这是我在本地机器上调试时得到的结果:
此外,我在我的BaseLayouts中添加了这个。
@Html.Sitecore().VisitorIdentification()
让我知道我错过了什么。