Sitecore Analytics - Tracker.Current is null when invoked through a pipeline

1.5k Views Asked by At

I have the following method

GetValue()
{
    var value = Tracker.Current.Contact;
}

That always executes.

However when I invoke the above method from the ItemResolver pipeline, Tracker.Current is always null.

public class CustomItemResolver : HttpRequestProcessor
{
     public override void Process(HttpRequestArgs args)
     {
        GetValue();
     } 
}

What am I missing here?

1

There are 1 best solutions below

1
Marek Musielak On BEST ANSWER

Tracker is initialized during startAnalytics pipeline, which is started by the renderLayout pipeline Sitecore.Analytics.Pipelines.HttpRequest.StartAnalytics processor.

If you really need Tracker to be initialized earlier, you can use the following method:

Sitecore.Analytics.Tracker.StartTracking();

Remember that Sitecore starts Tracker much later and I'm quite sure there is a reason they don't do this before ItemResolver, so you're doing it on your own responsibility.