0

I'm using Sitecore 9.1.1, and I've wired an event up to media:request that forces a track of the download page event, as well as increments a counter of number of downloads that a user has performed in a certain day. The counter part is working fine, but I've been getting repeated errors on the code to track the download event. It was a little perplexing because the logging code I wrote was reporting an error on a line that made no sense, so I deconstructed the code and found the problem was related to Tracker.Current being null.

At first I thought it might be that I hadn't configured the Analytics.ClusterName correctly; I hadn't set anything in there, but I hadn't noticed any issues tracking. I have one CM and one CD, so I've gone ahead and put in a hosts name entry to the CD on the CM's hosts file, and set the Analytics.ClusterName to that on CM, and to "localhost" on CD. However, I'm still getting the Tracker.Current is null error. I've confirmed the download event is found correctly, and I'm passing in the guid retrieved from the MediaRequest MediaUri.MediaPath value.

Here's my download tracking method. I put a lot of null logging checks in to find at what point in the analytics call I might be having an issue:

     private void TriggerDownloadEvent(Guid mediaPath) {
        try {
            IPageEventDefinition downloadDefinition = Tracker.MarketingDefinitions.PageEvents[AnalyticsIds.DownloadEvent.Guid];
            _logRepository.Info($"Download event info - alias: {downloadDefinition.Alias}, ID: {downloadDefinition.Id}");

            if (Tracker.Current == null) {
                _logRepository.Warn("Tracker.Current is null");
            } else if (Tracker.Current.Session == null) {
                _logRepository.Warn("Tracker.Current.Session is null");
            } else if (Tracker.Current.Session.Interaction == null) {
                _logRepository.Warn("Tracker.Current.Session.Interaction is null");
            } else if (Tracker.Current.Session.Interaction.PreviousPage == null) {
                _logRepository.Warn("Tracker.Current.Session.Interaction.PreviousPage is null");
            }

            Tracker.Current.Session.Interaction.PreviousPage.Register(new PageEventData(downloadDefinition.Alias, downloadDefinition.Id) {
                ItemId = mediaPath
            });
        } catch (Exception ex) {
            _logRepository.Error("TriggerDownloadEvent error", ex);
        }
    }
  • I faced a Tracker. Current is a null error in a diff scenario and was able to resolve it. In my case, I was using multiple custom token resolver on the search listing page to get data and custom facet from the xConnect facet, and one of the tokens was always getting Tracker. Current is null to solve that. I started the Tracker if it's null and meant for a run like if (Tracker.Current == null && Tracker.Enabled) Tracker.StartTracking(); and that solves my problem. I am just sharing my scenario and fix details if that help. – Jitendra soni May 8 at 22:43
  • @Jitendrasoni I tried that and got to Tracker.Current is not initialized. In looking at a related question, I saw this info: "Reason 3: Sitecore.Context.Page is not available. Sitecore's Analytic Tracker requires that the Sitecore.Context.Page object be available in order for Tracker to be initialized. In certain cases where this object is not available, and Tracker tries to be started, it will throw this error. Tracker cannot be utilized on ASHX's, internal pipelines or thread processes where Page is not set." So maybe I can't use it because it's an internal pipeline? – Ken McAndrew May 9 at 4:58
  • Tracker.Current required webvisit. Please try creating fake webvisit as mentioned in sitecore.stackexchange.com/a/20066 – Amit Kumar May 10 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.