2

我创建了一个自定义 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()

让我知道我错过了什么。

4

1 回答 1

0

我相当确定国家需要是与路径/sitecore/system/Settings/Analytics/Lookups/Countries中的项目一致的国家代码

您会注意到这些项目都有一个“国家代码”字段,这是您通常在分析数据中找到的值。

从您上面发布的内容来看,我猜您的服务返回全名?我建议在 Init 管道中设置一个静态字典,该字典读取以 Country 名称为键的所有 CountryItem 值,您可以使用它来对服务中的值进行编码。Sitecore 中的某个地方可能已经有一本这样的字典,但我只是粗略地一瞥就找不到。

于 2015-11-23T13:05:24.527 回答