Sitecore Analytics:从Web服务触发配置文件和事件

gor*_*hal 7 sitecore

我有问题 Sitecore.Analytics

从我的xslt,我使用jQuery对web服务进行ajax调用.

在我的web服务中,我需要注册/保存一些Sitecore.Analytics数据.问题是我无法使用Sitecore.Analytics.AnalyticsTracker.Current.

所以,我该怎么做TriggerProfileTriggerEvent?我想知道是否Sitecore.Analytics.AnalyticsManager可以提供任何帮助.

Ada*_*ber 9

我最近遇到了类似的情况,必须跟踪Web服务中的分析事件.如您所述,问题是AnalyticsTracker.Current在Web服务的上下文中为null.

其原因AnalytisTracker.Current是在trackAnalytics管道期间填充,而管道在管道期间调用renderLayout,仅在上下文项不为空且上下文项具有已定义的表示设置时才调用.

话虽如此,有一个解决方法:)

您可以AnalyticsTracker像这样手动启动:

if (!AnalyticsTracker.IsActive)
{
    AnalyticsTracker.StartTracking();
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以检索如下所示的AnalyticsTracker实例:

AnalyticsTracker tracker = AnalyticsTracker.Current;
if (tracker == null)
    return;
Run Code Online (Sandbox Code Playgroud)

最后,您可以创建并触发您的活动,个人资料等...下面的示例触发了一个PageEvent.注意:PageEvent为了获得Timestamp填充的属性,需要特别考虑(并且很可能是其他事件).请参阅以下代码中的注释.

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();
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!