Sitecore Analytics:来自WebService的触发配置文件和事件 [英] Sitecore Analytics: Trigger profiles and events from webservice

查看:20
本文介绍了Sitecore Analytics:来自WebService的触发配置文件和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Sitecore.Analytics

有问题

从我的XSLT中,我使用jQuery对Web服务进行Ajax调用。

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

那么我该如何做TriggerProfileTriggerEvent? 我想知道Sitecore.Analytics.AnalyticsManager是否可以提供任何帮助。

推荐答案

我最近遇到了一个类似的场景,必须跟踪Web服务中的分析事件。正如您所提到的,问题在于AnalyticsTracker.Current在Web服务的上下文中为空。

原因是AnalytisTracker.Current是在trackAnalytics管道期间填充的,而renderLayout管道又是在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();

希望这能有所帮助!

这篇关于Sitecore Analytics:来自WebService的触发配置文件和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆