2

I had a method which worked fine in 8.0 but now is throwing an error in 8.2 Update 2.

The method is:

public bool AddPageEvent(Guid DefinitionId, Guid PageEventItemId, string PageEventName, string PageEventData, string PageEventText) {
    try
    {
        Assert.IsNotNull(Tracker.Current, "Tracker.Current");
        Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session");
        var interaction = Tracker.Current.Session.Interaction;
        Assert.IsNotNull(interaction, "Tracker.Current.Session.Interaction");
        Assert.IsNotNull(interaction.CurrentPage, "Tracker.Current.Session.Interaction.CurrentPage");
        var pageEventData = new PageEventData(PageEventName, DefinitionId)
        {
            ItemId = PageEventItemId,
            Data = PageEventData,
            Text = PageEventText
        };
        interaction.CurrentPage.Register(pageEventData);
    }
    catch (Exception ex)
    {
        Log.Error("MySite.Analytics.PageEvents.AddPageEvent", ex, this);
        return false;
    }
    return true;
}   

The following error is being thrown on the first Assert.IsNotNull :

Exception: System.InvalidOperationException Message: Tracker.Current Source: MySite.Analytics at MySite.Analytics.PageEvents.AddPageEvent(Guid DefinitionId, Guid PageEventItemId, String PageEventName, String PageEventData, String PageEventText) in C:\SRC\Main\Sitecore\MySite.Analytics\PageEvents.cs:line 27

Any ideas why this would throw this error in 8.2?

5
  • Do you have Analytics enabled?
    – Diego
    May 10 '17 at 22:57
  • Yeah, but only on one node so far (just working out some config issues). I'm wondering if it is a session issue? May 10 '17 at 23:48
  • @Diego, when you are using xDB Cloud 2.0 the Analytics.Enabled setting should be false, and the Xdb.Enabled setting should be true, correct? May 11 '17 at 0:35
  • Have you solved your issue? May 22 '17 at 14:09
  • @Artsem Working through a couple other issues with Sitecore Support now, so can't be sure. May 22 '17 at 19:55
2

It looks like I'va got the same issue. I've fixed that with the following code. Thy this:

public bool AddPageEvent(Guid DefinitionId, Guid PageEventItemId, string PageEventName, string PageEventData, string PageEventText)
    {
        try
        {
            //check id tracker is active. start it if it is not.
            if (!Sitecore.Analytics.Tracker.IsActive)
            {
                Sitecore.Analytics.Tracker.StartTracking();
            }

            //ensure that tracker has a CurrentPage and already activated
            if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.Current.CurrentPage != null)
            {                    
                if (PageEventItemId != Guid.Empty) {

                    var pageEventData = new Sitecore.Analytics.Data.PageEventData(PageEventName)
                    {
                        ItemId = PageEventItemId,
                        Data = PageEventData,
                        Text = PageEventText
                    };

                    var pageEventsRow = Tracker.Current.CurrentPage.Register(pageEventData);                         
                    Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications(); 
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error("MySite.Analytics.PageEvents.AddPageEvent", ex, this);
            return false;
        }
        return true;
        }
1

It can happen when the Tracker is not active. So you need the following check and start the Tracking if it is not active.

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

Also a good practice the check the Tracker is enabled or not. Before start tracking you can check it:

if (!Tracker.Enabled)
{
    return false;
}
2
  • I have never found the tracker to not be on. Wouldn't that be a sign of a deeper issue?
    – Chris Auer
    May 11 '17 at 17:02
  • 1
    @dnstommy I experienced this, when a page does not use any Sitecore Tracking feature like a Goal or Profiling, then Tracker.IsActive is false. So it only happens when you want to use Tracking from custom code first time in the session. May 12 '17 at 15:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.