I need to do a simple newsletter form. This form should work like this:
This form should be work with help EXM
I've created Triggered message in the EXM with link for subscription. And I wrote this code for the Submit button for trigger the Newsletter Goal
[HttpPost]
public ActionResult NewsletterSubscribe(NewsletterViewBag model)
{
var goal = Context.Database.GetItem(newsletterGoal);
if (goal == null)
{
continue;
}
var registerGoal = new Sitecore.Analytics.Data.Items.PageEventItem(goal);
var eventData = Tracker.Current.CurrentPage.Register(registerGoal);
eventData.Data = goal[DateTime.Now.ToString(CultureInfo.InvariantCulture)];
Tracker.Submit();
}
How I can assign my triggered message to the newsletterGoal? Also I try manually send message this way:
MessageItem message = Sitecore.Modules.EmailCampaign.Factory.GetMessage(new ID(messageId));
Sitecore.Modules.EmailCampaign.AsyncSendingManager manager = new AsyncSendingManager(message);
var contactId = ClientApi.GetAnonymousIdFromEmail(email);
var recipientId = (RecipientId) new XdbContactId(contactId);
manager.SendStandardMessage(recipientId);
And I see error in the log: The recipient 'xdb:857bbea1-1f18-4621-a798-178399cd0b54' does not exist. But Triggered Message haven't any recipient list
Goals are not assigned directly to messages. You can, however, assign engagement plans and campaigns. Each message has its own engagement plan to handle tracking the contacts actions with the message. If you create a campaign that triggers a goal, you can assign that to the message and it will be associated with the contact when they receive the message. You can also leverage the message engagement plan to trigger events as the contact proceeds through those states.
Also, you're missing some details while recording the contact data. Look at the newsletter signup control that is included in the EXM module. The important part in there is this:
protected virtual RecipientId RecipientId
{
get
{
RecipientId recipientId = null;
var contactId = ContactId;
if (contactId != (ID)null)
{
recipientId = new XdbContactId(contactId);
}
return recipientId;
}
}
protected virtual ID ContactId
{
get
{
if (!Email.Visible || string.IsNullOrEmpty(Email.Text))
{
return new ID(Tracker.Current.Contact.ContactId);
}
var anonymousId = ClientApi.GetAnonymousIdFromEmail(Email.Text);
return anonymousId.HasValue ? new ID(anonymousId.Value) : new ID(Tracker.Current.Contact.ContactId);
}
}
protected virtual void UpdateEmailInXdb()
{
_recipientRepository.UpdateRecipientEmail(RecipientId, Email.Text);
}
It will write the email address directly to Mongo, rather than waiting for the session to end. Include this and the related RecipientId and ContactId properties in your signup code.
Once they are signed up you can register the goal programmatically or send them to a Thank You page where the goal can be registered (Advanced - Tracking), or send the message and let that register the goal. Or create an engagement plan with states for each step of the process (this is the best way).
You'll also want to add the recipient to a list that the newsletter message can use later. Actually, it looks to me like the example Subscription Form does everything you need.