Sitecore Analytics: trigger profiles and events from webservice

I have a problem with Sitecore.Analytics

From my xslt, I am making an ajax call to a web service using jQuery.

In my web service I need to register / save some Sitecore.Analytics data. The problem is that I cannot use Sitecore.Analytics.AnalyticsTracker.Current .

So how do I make TriggerProfile and TriggerEvent ? I wonder if Sitecore.Analytics.AnalyticsManager be of any help.

+7
source share
4 answers

I recently came across a similar scenario in tracking analytics events in a web service. As you noticed, the problem is that AnalyticsTracker.Current is null in the context of the web service.

The reason for this is that AnalytisTracker.Current populated during the trackAnalytics pipeline, which, in turn, is called during the renderLayout pipeline, which is called only if the context element is not null and the context element has a representation of certain parameters.

With that said, there is a workaround :)

You can run AnalyticsTracker manually like this:

 if (!AnalyticsTracker.IsActive) { AnalyticsTracker.StartTracking(); } 

Then you can get an instance of AnalyticsTracker , for example:

 AnalyticsTracker tracker = AnalyticsTracker.Current; if (tracker == null) return; 

And finally, you can create and run your event, profile, etc. The following example starts a PageEvent . Note. For PageEvent (and most likely other events), special care must be taken to get the Timestamp property. See comments in the code below.

 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(); 

Hope this helps!

+9
source

I just want to add an additional comment to share my experience. If you have a POST RESTful service call in which you implement the page event logging method, make sure to use the Simple REST client extension for Chrome instead of Fiddler to send a POST request . I came across a situation where I used Fiddler to call the POST RESTful service, the code ran fine, but I did not see any record in the page event table (the problem, I think, could be due to the browser session, maybe).

For DMS 2.0, I performed a similar article on StackOverflow and wrote the following to record an event.

 void TriggerPageEvent(string eventName) { if (!Tracker.IsActive) { Tracker.StartTracking(); } Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("web"); Sitecore.Context.Item = db.GetItem("/sitecore"); PageEventData pageEventData = new PageEventData(eventName); pageEventData.Text = "Value is here"; pageEventData.DataKey = String.Empty; pageEventData.Data = " Data is here"; pageEventData.ItemId = new Guid(Cms.Context.Item.ID.ToString()); Tracker.CurrentPage.Register(pageEventData); Tracker.Submit(); } 
+3
source

If you want to register the event / target of the page and use your custom handler to get the data from the ajax / jquery request, make sure that your location for the handler is analytical. This means that if your handler is located, for example, in /mylocation/myhandlers/myhandler.ashx, you need to implement the following, add this line to your configuration file:

 <site name="your_name" virtualFolder="/mylocation/myhandlers" physicalFolder="/mylocation/myhandlers" enableAnalytics="true" domain="sitecore" database="master" patch:before="site[@name='website']" /> 

This should help. As you can see, in this case your analytics is turned on.

+1
source

Be sure to add the attribute / value [WebMethod(EnableSession = true)] to your WebMethod and this will work correctly.

+1
source

All Articles