我目前正在尝试跟踪 ASHX 处理程序中的 PageEvent。我的代码基本上是这样的:
public class GetProductPdf : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (!Tracker.IsActive)
{
Tracker.Initialize();
Tracker.StartTracking();
}
//Track PageEvent here...
}
public bool IsReusable
{
get
{
return false;
}
}
}
Tracker 始终处于非事件状态,并且 Tracker.Current == null。在方法调用“Tracker.StartTracking();”抛出以下异常:
[InvalidOperationException: Tracker.Current is not initialized]
Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking.Process(PipelineArgs args) +317
(Object , Object[] ) +83
Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +445
Project.Web.Handler.PdfCreation.GetProductPdf.ProcessRequest(HttpContext context) in d:\Project\Website\Handler\PdfCreation\GetProductPdf.ashx.cs:69
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +913
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165
我尝试了所有建议的可能解决方案 here .
在 mvc Controller 中执行相同操作时,Tracker 处于事件状态并且 Tracker.Current != null。
有谁知道什么可能导致此问题,或者有其他解决方案建议吗?
提前致谢。
最佳答案
我不确定您的 Ashx Handler
是否可以在必要的 Sitecore 上下文中执行,因此 Tacker.Current
将无效,也无法通过 启动Tracker.StartTracking()
。也许有人可以确认,但我有另一个适合我的解决方案,您可以尝试一下。
尽管 Ashx Handler
为您注册 Event
会很好,但您可以在文件链接上触发 JavaScript 函数。这样,当单击链接时,JavaScript 会向 MVC Controller
发出 Web 请求,并且 Controller 会为您注册事件。
我自己使用 WebApi Controller
实现了这一点。 数据属性
位于a标签
上,JavaScript将这些属性发布到 Controller , Controller 使用这些属性来确定要注册哪个事件
以及用于事件的描述
。
<asp:HyperLink runat="server" data-goalid="{08030449-A811-428B-95F0-59FCD42B8DEB}" data-goaldescription="Product 0112 brochure">
[System.Web.Mvc.HttpPost]
public JsonResult RegisterGoal(string goalId, string goalDescription)
{
Item eventItem = Sitecore.Context.Database.GetItem(goalId);
var goal = new PageEventItem(eventItem);
var eventData = Tracker.Current.PreviousPage.Register(goal);
eventData.Data = goal["Description"] + " " + goalDescription;
Tracker.Current.Interaction.AcceptModifications();
return Json(new PageEventRequestResult()
{
Success = true,
Message = "Successfully registered goal",
});
}
效果非常好。唯一的缺点是必须将其添加到指向您要跟踪的文件的各种链接中。
我写了一篇关于 tracking various interactions on a site and registering Sitecore Events / Goals 的博客您可能想查看,向下滚动到“在 xDB 中存储自定义数据”部分。
关于sitecore - 从 ASHX 处理程序跟踪页面事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597472/