Sitecore Moosend - Part II: Mailing Lists, Custom Fields and API Integration
Back to home

Sitecore Moosend - Part II: Mailing Lists, Custom Fields and API Integration

Miguel Minoldo's picture
Miguel Minoldo

In my previous post I've shared a quick overview on Moosend and some of the main features. I advise you to take a look at the previous one before proceeding with this reading.

Today we will explore a bit more in depth the mailing lists, custom fields and also the API implementation approach.

Mailing Lists and Segments

In the previous post we created a Mailing List and added subscribers using the front end approach, applying Segmentation to it, we can improve the efficency of our marketing campaign by targeting the audience based on the data gathered from the users (custom fields) and the events recorded by Moosend.

Blog post image
Click to expand

Custom Fields

We can define in this section custom fields that we then can use for gathering data from the user, on top of the default ones (Name, Email and Mobile). We can use those afterwards for automation, segmentation, etc.

Let's create a new custom field (Date of Birth) and make it optional:

Blog post image
Click to expand

Our custom field is now created and we can use it for our example. Check the generated tag: "recipient:Date of Birth": you can make use of this token for pesonalize your campaigns.

Blog post image
Click to expand

Segments

Let's for example take our previously created list "My Testing List" and create a new segmentation based on the Subscription Method = API Integration AND Date of Birth field < 01-01-2010".

Blog post image
Click to expand
Blog post image
Click to expand

We give a name and then add a criteria, so here we're creating a segmentation where we fetch "all contacts that subscribed through the API integration method and provided a Date of Birth before 01-01-2010".

API Integration

As mentioned, this time we'll be doing the integration it with the API approach. Moosend provides an API wrapper (Javascript or C# .NET) that makes working with it really straightforward, you can find the Nuget package here.

First of all, go to the setting section and then click in API key, copy it and save for later:

Blog post image
Click to expand

Now we can create our service class on .NET Core that will interact with the Moosend API:

MoosendService.cs

csharp
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Microsoft.Extensions.Options;
5using Moosend.Api.Client.Common.Models;
6using MyApp.WebApi.Configuration;
7
8namespace MyApp.WebApi.Services
9{
10 public class MoosendService : IMoosendService
11 {
12 public MoosendService(IOptions<MoosendSettings> settings)
13 {
14 MoosendSettings = settings.Value;
15 }
16
17 private MoosendSettings MoosendSettings { get; }
18
19 public async Task<Moosend.Api.Client.Common.Models.Subscriber> AddSubscriberAsync(string name, string email,
20 DateTime dob)
21 {
22 var mailingListId = new Guid(MoosendSettings.MailingListID);
23 var apiKey = new Guid(MoosendSettings.ApiKey);
24 var apiClient = new Moosend.Api.Client.MoosendApiClient(apiKey);
25 var customFields = new Dictionary<string, string> {{"Date of Birth", dob.ToLongDateString()}};
26 var member = new SubscriberParams()
27 {
28 Email = email,
29 Name = name,
30 CustomFields = customFields
31 };
32
33 return await apiClient.SubscribeMemberAsync(mailingListId, member);
34 }
35 }
36}

MoosendController.cs

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyApp.WebApi.Services;

namespace MyApp.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class MoosendController : ControllerBase
{
public MoosendController(IMoosendService moosendService)
{
MoosendService = moosendService;
}

private IMoosendService MoosendService { get; }

[HttpPost("AddSubscriber/{name}/{email}/{dob}")]
public async Task AddSubscriber(string name, string email,
string dob)
{
return await MoosendService.AddSubscriberAsync(name, email, Convert.ToDateTime(dob));
}
}
}

Let's now test it on Swaggwer, I'll create 2 users with a birthdate before 01-01-2010 and one after this date, so we can test the segmentation properly:

Blog post image
Click to expand
Blog post image
Click to expand

Check now the Mailing List:

Blog post image
Click to expand

We can see our 3 members being added through the API, let's take a look now at the segmentation:

Blog post image
Click to expand

We can see the two subscribers matching the segmentation criteria. In the next post I'll be showing how to make use of the previously created mailing list, custom fields and segments with the campaigns and automation, we will also have a quick look at the template designer.

I hope you find it useful and keep tuned for more Moosend posts!