Level: 300
 

Use the Segment Builder for creating Target Audience in ECM

In this post I will describe how you can tweak the Sitecore Segment Builder to generate Target Audience for the Sitecore ECM based on Visitor information stored in the DMS database.
Written by: Jens Mikkelsen
Wed, Jul 31 2013

Introduction


I have been working quite a bit with Sitecore ECM and really like the product. Most installations I have worked with generates Target Audiences using external data from a CRM or similar. This is quite nice and you generate dynamic lists and detailed segments. However I was thinking about how nice it would be, to be able to generate lists based on tracked interactions on the website captured by DMS. 


Of course you need the e-mail on the DMS visitor, but if your setup is as I think it should be, you have hooked up DMS with your CRM using a membership provider. When sending out campaigns, on registration forms etc. you make sure to connect the visitor to the CRM user, thus your information collection just got a lot better. If you now have this in place, wouldn’t it be cool, to be able to send out an e-mail to everyone in your CRM, who has triggered a goal, visited a certain page (e.g product page) an amount of times or something third?


Well you can do that using the following implementation.



The solution


I built this solution using the standard Sitecore Segment Builder which is available for enrolling visitors in Engagement Plans. The dialog had to be customized, so that instead of enrolling visitors in engagement plan it would add the associated user to a given role. When this was done, I just needed to add it in Sitecore ECM, which was quite easy, as Sitecore ECM has been built very extendable using SPEAK.


So when creating your newsletter and gets to the recipient creation, you will then see this:


ECM Build Target Audience


And then you can select to build it from the segment builder:






Now the Segment Builder will be triggered:

Sitecore Segment Builder


When the OK button is clicked, the visitors who have an associated user with an email, will be added to the default list for the email. 

Clever right? 


Implementation

So actually not very much coding is required for this. First we have to customize the Segment Builder. The dialog is built using good old Sitecore XAML. This means we can copy the dialog from its original location and customize the new dialog. The XAML file can be found at: \sitecore\shell\Applications\Analytics\SegmentBuilder\SegmentBuilder.xml

We have to copy this file and place it somewhere (inside /sitecore/shell if you don’t want to add something to ControlSources in web.config). For the sake of this proof-of-concept I just copied it to the /sitecore/shell/override folder and renamed it to ECMSegmentBuilder.xml.

Now we need to edit the file. First of all we need to rename the control, which is denoted by the first element

Segment builder XAML

I change this to the name I want as well as the CodeBeside, which denotes what class should be used as code behind file. I use my own namespacing:

ECM Segment builder XAML

The CodeBeside class I then create in Visual Studio and let it inherit from the normal Segment Builder:

namespace Demo.Code.Components.Test

{

  public class EmailCampaignSegmentBuilderForm : SegmentBuilderForm

  {


The SegmentBuilderForm is placed in the Sitecore.SegmentBuilder.dll.

Now we can override the functionality, so that it will create the role and add users to it instead of what the SegmentBuilder is doing per default. To know which role we should add the user to, we will add a query string that will be appended, when the dialog is triggered. We just need to override the OnOk button as this is where we want to trigger our code:

namespace Demo.Code.Components.Test

{

  public class EmailCampaignSegmentBuilderForm : SegmentBuilderForm

  {

    protected override void OnOK(object sender, EventArgs args)

    {

      Assert.ArgumentNotNull(sender, "sender");

      Assert.ArgumentNotNull(args, "args");

 

      //Get the role from the query string that is provided when the dialog is triggered

      string roleName = HttpContext.Current.Request.QueryString["targetAudience"] as string;

      if (string.IsNullOrEmpty(roleName))

        throw new InvalidOperationException("You need to specify a role name");

 

      //Find the role

      Role role = Role.FromName(roleName);

      if(role == null)

        System.Web.Security.Roles.CreateRole(roleName);

 

      //Iterate over all visitors selected by the filters specified by the user

      IEnumerable<Guid> visitors = this.Filter.GetVisitorIDs(this.FilterSet.ToString());

      foreach (Guid visitorId in visitors)

      {

        Visitor visitor = Sitecore.Analytics.Data.DataAccess.VisitorFactory.GetVisitor(visitorId);

        if (visitor != null && !String.IsNullOrEmpty(visitor.ExternalUser))

        {

          //We need to find the associated user and check if they have a valid e-mail

          User user = Sitecore.Security.Accounts.User.FromName(visitor.ExternalUser, false);

          if (String.IsNullOrEmpty(user.Profile.Email))

            continue;

 

          //Add the user to the role which denotes our Target Audience

          System.Web.Security.Roles.AddUserToRole(user.Name, roleName);

        }

      }

      base.OnOK(sender, args);

    }

 

  }


That is more or less it from the Segment Builder form. Quite simple and small changes. Don’t you just love the extendibility of Sitecore? 

Now we need to add the functionality to Sitecore ECM. This is built on SPEAK, and to be honest I haven’t dug deep into SPEAK, but was able to quite easily extend the functionality. It seems that the dropdown where you create your lists (which is shown above), is controlled here: /sitecore/system/Modules/SPEAK/EmailCampaign/Controls/Action panels/ImportOptIn.

Here we can just add a new Action. Let us call it GenerateFromSegment. We can then enter a command like you are used to from the old days:

ECM Action


Now we need to add the command to /App_Config/commands.config. Note that you might want to use another command name than emailcampaign:test, but I’ll let it be for now.


Sitecore command action

Now all we have to do is implement the command:


namespace Demo.Code.Components.Test

{

  public class LaunchSegmentBuilderCommand : Command, ISupportsContinuation

  {

    public override void Execute(CommandContext context)

    {

      //Get the default target audience holding the recipients

      string itemID = context.Parameters["id"];

      MessageItem item = (itemID != null) ? Factory.GetMessage(itemID) : UIFactory.Instance.GetSpeakContext().Message;

      string role = item.TargetAudience.OptInList.Roles.FirstOrDefault().Name;

 

      //Trigger our dialog with the right target audience query string

      SheerResponse.ShowModalDialog("/sitecore/shell/default.aspx?xmlcontrol=Demo.Code.Test.SegmentBuilder&targetAudience=" + role + "&hdl=");

    }

  }

}


And that is actually it and the only thing taking a long time, was getting my head around SPEAK. Hopefully Sitecore will provide documentation, when the product is released officially.

 

Please rate this article


1 rates / 5 avg.

  • About the author:

    Jens Mikkelsen

    Jens Mikkelsen is a partner at Inmento Solutions a Sitecore consulting firm. He works as a Sitecore specialist and consulting helping clients architect and build quality Sitecore solutions using the newest modules and tools. 

    Further he has been deeply envolved in various complex solutions and has built up a strong knowledge of Sitecore architecture and best practices. He has especially focused on and is specialized in debugging and analyzing Sitecore solutions.

     

    Jens is very interested in the technical mechanisms in the new marketing products such as Sitecore DMS and Sitecore ECM.

    My Sitecore Freelance CV

3 responses to "Use the Segment Builder for creating Target Audience in ECM"

Hi

Thanks for your blog post. its interesting.
I tried developing this feature in our sitecore 7, ECM 2.1 following the instructions here but the code for 'LaunchSegmentBuilderCommand' had an issue which was lacks the urlhandle query string parameter. So I replaced this line of code:
SheerResponse.ShowModalDialog("/sitecore/shell/default.aspx?xmlcontrol=Demo.Code.Test.SegmentBuilder&targetAudience=" + role + "&hdl=");

with:
var handle = new UrlHandle();
var str = "/sitecore/shell/default.aspx?xmlcontrol=Demo.Code.Test.SegmentBuilder&targetAudience=" + role;
var url = new UrlString(str);
handle.Add(url, "hdl");
SheerResponse.ShowModalDialog(url.ToString());

This fixed the issue
Posted: Tuesday, January 28, 2014 5:32 PM
I have a question here please if you can help.

After adding the recipients list using 'GenerateFromSegment', the list doesn't get populated in the 'Recipients View list' even though there are list members available. So it gives impression that there is no recipients selected, but they only appear if I refresh the page manually.

Is there anyway the list get populated in the 'Recipients View list' immediately?

Your help appreciated.
Bahareh
Posted: Tuesday, January 28, 2014 5:39 PM
Hi,
In Sitecore content editor my email messages are coming from noreply@vicsuper.com.au How can I add a name next to the email account so the email client displays VicSuper instead of noreply?
Thank you,
Esteban
Posted: Friday, February 07, 2014 4:20 AM

Leave a reply


Notify me of follow-up comments via email.
 
 
#nbsp;