최근 웹 서비스 내에서 분석 이벤트를 추적해야하는 것과 비슷한 시나리오가 발생했습니다. 당신이 지적한대로, 문제는 AnalyticsTracker.Current
이 웹 서비스 컨텍스트 내에서 null이라는 것입니다.
그 이유는 AnalytisTracker.Current
차례로 상황에 맞는 항목이 null가 아니고, 상황에 맞는 항목을 정의 프리젠 테이션 설정이있는 경우에만 호출됩니다 renderLayout
파이프 라인, 동안 호출되는 trackAnalytics
파이프 라인, 동안 채워져 있다는 것입니다.
if (!AnalyticsTracker.IsActive)
{
AnalyticsTracker.StartTracking();
}
당신은 다음과 같이 AnalyticsTracker
인스턴스를 검색 할 수 있습니다 :
AnalyticsTracker tracker = AnalyticsTracker.Current;
if (tracker == null)
return;
그와
는 해결 방법 :
당신과 같이 수동으로 AnalyticsTracker
를 시작할 수가 말했다
그리고 마지막으로 이벤트, 프로필 등을 만들고 트리거 할 수 있습니다. 아래 예제는 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
좋은 대답 아담 :) –