1

I am trying to enroll an anonymous user to a marketing automation plan but it didn't work.

Basically the user will open campaign from Facebook, this campaign already created with the Sitecore campaign creator, then the user will land on the registration page (it will be lead page), SC campaign ID is already in the query string.

I need to enroll the user in marketing automation and I can send him a welcome email and add him to the recipients list.

3

You can not add anonymous contacts to Automation Plans.

You will need to identify them first. Additionally, if you are planning on sending this user an email, you have to know information about the contact, thereby making it a known contact.

It sounds like you need to add a Custom Submit Action to your Sitecore Form to identify the contact, set the email address and personal facets, and then trigger a goal, to then enroll them into a Marketing Automation campaign.

Example Code to Identify a Contact using a Custom Submit Action

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Analytics;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Client.Configuration;
using Sitecore.XConnect.Collection.Model;

namespace SitecoreHacker.Feature.ExperienceForms.SubmitActions
{
    public class TriggerIdentifyContact : SubmitActionBase
    {
        ///
<summary>
        ///     Initializes a new instance of the  class.
        /// </summary>

        /// The submit action data.
        public TriggerIdentifyContact(ISubmitActionData submitActionData) : base(submitActionData)
        {
        }

        ///
<summary>
        ///     Gets the current tracker.
        /// </summary>

        protected virtual ITracker CurrentTracker =&gt; Tracker.Current;

        public override void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)
        {
            Assert.ArgumentNotNull(formSubmitContext, "formSubmitContext");
            UpdateContactData target;
            if (TryParse(parameters, out target))
                try
                {
                    if (Execute(target, formSubmitContext))
                        return;
                }
                catch (ArgumentNullException)
                {
                }
            formSubmitContext.Errors.Add(new FormActionError
            {
                ErrorMessage = SubmitActionData.ErrorMessage
            });
            formSubmitContext.Abort();
        }

        ///
<summary>
        ///     Executes the action with the specified .
        /// </summary>

        /// The data.
        /// The form submit context.
        /// true if the action is executed correctly; otherwise false
        protected override bool Execute(UpdateContactData data, FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(data, nameof(data));
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            var firstNameField = GetFieldById(data.FirstNameFieldId, formSubmitContext.Fields);
            var lastNameField = GetFieldById(data.LastNameFieldId, formSubmitContext.Fields);
            var emailField = GetFieldById(data.EmailFieldId, formSubmitContext.Fields);

            if (firstNameField == null &amp;&amp; lastNameField == null &amp;&amp; emailField == null)
                return false;

            using (var client = CreateClient())
            {
                try
                {
                    var emailIdent = new IdentifiedContactReference("IdentifiedEmail", GetValue(emailField));

                    //Identify with Email
                    CurrentTracker.Session.IdentifyAs(emailIdent.Source, emailIdent.Identifier);

                    var expandOptions = new ContactExpandOptions(
                        CollectionModel.FacetKeys.PersonalInformation,
                        CollectionModel.FacetKeys.EmailAddressList);
                    var contact = client.Get(emailIdent, expandOptions);

                    //Set Fields
                    SetPersonalInformation(GetValue(firstNameField), GetValue(lastNameField), contact, client);
                    SetEmail(GetValue(emailField), contact, client);

                    client.Submit();

                    return true;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.Message, ex);
                    return false;
                }
            }
        }

        ///
<summary>
        ///     Creates the client.
        /// </summary>

        /// The  instance.
        protected virtual IXdbContext CreateClient()
        {
            return SitecoreXConnectClientConfiguration.GetClient();
        }

        ///
<summary>
        ///     Gets the field by .
        /// </summary>

        /// The identifier.
        /// The fields.
        /// The field with the specified .
        private static IViewModel GetFieldById(Guid id, IList fields)
        {
            return fields.FirstOrDefault(f =&gt; Guid.Parse(f.ItemId) == id);
        }

        ///
<summary>
        ///     Gets the  value.
        /// </summary>

        /// The field.
        /// The field value.
        private static string GetValue(object field)
        {
            return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
        }

        ///
<summary>
        ///     Sets the  facet of the specified .
        /// </summary>

        /// The first name.
        /// The last name.
        /// The contact.
        /// The client.
        private static void SetPersonalInformation(string firstName, string lastName, Contact contact,
            IXdbContext client)
        {
            if (string.IsNullOrEmpty(firstName) &amp;&amp; string.IsNullOrEmpty(lastName))
                return;
            var personalInfoFacet = contact.Personal() ?? new PersonalInformation();
            if (personalInfoFacet.FirstName == firstName &amp;&amp; personalInfoFacet.LastName == lastName)
                return;
            personalInfoFacet.FirstName = firstName;
            personalInfoFacet.LastName = lastName;
            client.SetPersonal(contact, personalInfoFacet);
        }

        ///
<summary>
        ///     Sets the  facet of the specified .
        /// </summary>

        /// The email address.
        /// The contact.
        /// The client.
        private static void SetEmail(string email, Contact contact, IXdbContext client)
        {
            if (string.IsNullOrEmpty(email))
                return;
            var emailFacet = contact.Emails();
            if (emailFacet == null)
            {
                emailFacet = new EmailAddressList(new EmailAddress(email, false), "Preferred");
            }
            else
            {
                if (emailFacet.PreferredEmail?.SmtpAddress == email)
                    return;
                emailFacet.PreferredEmail = new EmailAddress(email, false);
            }
            client.SetEmails(contact, emailFacet);
        }
    }

    public class UpdateContactData
    {
        public Guid FirstNameFieldId { get; set; }
        public Guid LastNameFieldId { get; set; }
        public Guid EmailFieldId { get; set; }
    }
}

This came from the blog post: https://sitecorehacker.com/2018/02/22/svdd-using-forms-in-sitecore-9-and-xdb-together/

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.