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;
Hope this helps!
Adam weber
source share