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