Monthly Archives: October 2016

Modifying Code to Add a Visitor to an Engagement Plan when upgrading from Sitecore 6.6 to 8.1

In my recent efforts during an upgrade from 6.6 to 8.1 I had to refactor some code to work with Sitecore 8.1 to add a visitor to an engagement plan. Below are my findings:

These were the .dlls that were referenced in the previous solution in 6.6:

* Sitecore.Analytics
* Sitecore.Automation.MarketingAutomation

I added in the Sitecore.Analytics.dll and that wasn’t all I needed apparently as I got some errors upon building the project.

using Sitecore.Analytics;
using Sitecore.Analytics.Automation.Data;

Looked like the Automation in Sitecore.Analytics was no longer there and has been moved. After a quick look through Reflecting on the Analytics libraries I had to add the Sitecore.Analytics.Automation.dll as a reference which would give me the Automation and MarketingAutomation that I needed.

Sitecore 8.1 Analytics

Sitecore 8.1 Analytics Libraries

Once that was in place I was able to refactor the code to work with the new Analytics code libraries for Sitecore 8.1.

Before:

using System;
using System.Collections.Generic;
using Sitecore.Analytics;
using Sitecore.Analytics.Automation.Data;
using Sitecore.Analytics.Data.DataAccess;
using Sitecore.Data;
using Sitecore.Rules;
using Sitecore.Rules.Actions;
using Sitecore.Form.Core.Configuration;
 
namespace Sitecore.Sandbox.Actions
{
    public class AddVisitorToEngagementPlan<T> : RuleAction<T> where T : RuleContext
    {
        public string ItemId { get; set; }
 
        public override void Apply(T ruleContext)
        {
            if (!Tracker.IsActive)
                Tracker.StartTracking();
 
            if (!Settings.IsAnalyticsEnabled || string.IsNullOrEmpty(ItemId)) return;
 
            var engagementPlanState = Sitecore.Context.Database.GetItem(ID.Parse(ItemId));
            if (engagementPlanState.TemplateID != ID.Parse("8CE2707A-3742-4A89-933B-065E5BE02BC9")) return;
 
            var engagementPlan = engagementPlanState.Parent;
 
            var visitor = Tracker.Visitor;
            var visitorLoadOption = new VisitorLoadOptions { Options = VisitorOptions.AutomationStates };
            visitor.Load(visitorLoadOption);
 
            if (Tracker.Visitor.DataSet.AutomationStates.Count > 0) return;
 
            AutomationManager.Provider.CreateAutomationStatesFromBulk(new List<Guid> { Tracker.CurrentVisit.VisitorId }, engagementPlan.ID.Guid, engagementPlanState.ID.Guid);
        }
    }
}

After:

You will notice that Sitecore.Automation.MarketingAutomation changed to Sitecore.Analytics.Automation.MarketingAutomation.

using System.Linq;
using Sitecore;
using Sitecore.Analytics;
using Sitecore.Analytics.Automation.MarketingAutomation;
using Sitecore.Data;
using Sitecore.Rules;
using Sitecore.Rules.Actions;
 
namespace Sitecore.Sandbox.Actions
{
    public class AddVisitorToEngagementPlan<T> : RuleAction<T> where T : RuleContext
    {
        public string ItemId { get; set; }
 
        public override void Apply(T ruleContext)
        {
            if (!Tracker.IsActive)
                Tracker.StartTracking();
 
            if (!Tracker.Enabled || string.IsNullOrEmpty(ItemId)) return;
 
            var engagementPlanState = Context.Database.GetItem(ID.Parse(ItemId));
            if (engagementPlanState.TemplateID != ID.Parse("8CE2707A-3742-4A89-933B-065E5BE02BC9")) return;
 
            var engagementPlan = engagementPlanState.Parent;
 
            Tracker.Current.Session.Identify(Context.User.Profile.UserName);
            var manager = Tracker.Current.Session.CreateAutomationStateManager();
 
            if (!manager.GetAutomationStates().Any()) return;
            manager.EnrollInEngagementPlan(ID.Parse(engagementPlan.ID.Guid), ID.Parse(engagementPlanState.ID.Guid));
        }
    }
}

Thanks goes to Brian Pederson for his blog post, which helped me on this issue: https://briancaos.wordpress.com/2015/01/26/sitecore-8-and-engagement-plans/