我有一个问题Sitecore.Analytics
从我的 xslt 中,我使用 jQuery 对 Web 服务进行 ajax 调用。
在我的网络服务中,我需要注册/保存一些Sitecore.Analytics
数据。
问题是我无法使用Sitecore.Analytics.AnalyticsTracker.Current
.
那我该怎么办TriggerProfile
and TriggerEvent
?
我怀疑是否Sitecore.Analytics.AnalyticsManager
可能有任何帮助。
我最近遇到了类似的场景,必须跟踪 Web 服务中的分析事件。正如你所指出的,问题是AnalyticsTracker.Current
在 Web 服务上下文中为 null。
这样做的原因是AnalytisTracker.Current
期间有人居住trackAnalytics
管道,依次在renderLayout
管道,仅当上下文项不为空并且上下文项定义了呈现设置时才调用该管道。
话虽如此,有一个解决方法:)
您可以启动AnalyticsTracker
手动像这样:
if (!AnalyticsTracker.IsActive)
{
AnalyticsTracker.StartTracking();
}
然后您可以检索AnalyticsTracker
像这样的实例:
AnalyticsTracker tracker = AnalyticsTracker.Current;
if (tracker == null)
return;
最后,您可以创建并触发您的事件、个人资料等...下面的示例触发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();
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)