我最近遇到类似的情况,需要跟踪web服务中的分析事件。如您所述,问题是AnalyticsTracker.Current
在web服务的上下文中为空。
这样做的原因是,AnalytisTracker.Current
是trackAnalytics
管道,这又将renderLayout
管道,这如果上下文项不为空,并从上下文项目具有定义演示设置时才会激活过程中被调用时填充。
随着中说,有一种变通方法:)
您可以手动启动AnalyticsTracker
像这样:
if (!AnalyticsTracker.IsActive)
{
AnalyticsTracker.StartTracking();
}
然后你可以检索AnalyticsTracker
情况下,像这样:
AnalyticsTracker tracker = AnalyticsTracker.Current;
if (tracker == null)
return;
最后,您可以创建并触发您的事件,配置文件等等。下面的示例触发PageEvent
。注意:为了获得Timestamp
财产,需要对PageEvent
(以及最有可能的其他事件)进行特殊考虑。请参阅下面代码中的注释。
if (!AnalyticsTracker.IsActive)
{
AnalyticsTracker.StartTracking();
}
AnalyticsTracker tracker = AnalyticsTracker.Current;
if (tracker == null)
return;
string data = HttpContext.Current.Request.UrlReferrer != null
? HttpContext.Current.Request.UrlReferrer.PathAndQuery
: string.Empty;
//Need to set a context item in order for the AnalyticsPageEvent.Timestamp property to
//be set. As a hack, just set the context item to a known item before declaring the event,
//then set the context item to null afterwards.
Sitecore.Context.Item = Sitecore.Context.Database.GetItem("/sitecore");
AnalyticsPageEvent pageEvent = new AnalyticsPageEvent();
pageEvent.Name = "Download Registration Form Submitted";
pageEvent.Key = HttpContext.Current.Request.RawUrl;
pageEvent.Text = HttpContext.Current.Request.RawUrl;
pageEvent.Data = data;
//Set the AnalyticsPageEvent.Item property to null and the context item to null.
//This way the PageEvent isn't tied to the item you specified as the context item.
pageEvent.Item = null;
Sitecore.Context.Item = null;
tracker.CurrentPage.TriggerEvent(pageEvent);
tracker.Submit();
希望这会有所帮助!
谢谢你。你是救世主:-) – gorhal 2011-03-10 14:35:40
好的回答亚当:) – 2011-12-05 17:47:13