Create An Event and push to MailChimp using MailChimp Net V3

196 Views Asked by At

Here's an (php) example of creating an Event and pushing it to MailChimp. Anybody knows how this can be accomplished using C# and MailChimp Net V3?

PHP-example: https://mailchimp.com/developer/marketing/guides/track-outside-activity-events/#create-an-event

1

There are 1 best solutions below

0
AnteSim On

I've found a way, though you'll need to create a new Logic class.

public class MemberEventResponse
{
}

public class MailChimpMemberEventLogic : BaseLogic
{
    public MailChimpMemberEventLogic( MailChimpOptions options )
        : base( options )
    {
    }

    public async Task<MemberEventResponse> CreateMemberEventAsync( string listId, string email, string eventName )
    {
        var baseUrl = $"/lists/{listId}/members/{Hash(email)}/events";
        using ( var client = CreateMailClient( baseUrl ) )
        {
            var response = await client.PostAsJsonAsync( "", new { name = eventName } ).ConfigureAwait( false );
            await response.EnsureSuccessMailChimpAsync().ConfigureAwait( false );

            var listActivityResponse = await response.Content.ReadAsAsync<MemberEventResponse>().ConfigureAwait( false );
            return listActivityResponse;
        }
    }
}

Then you just need to instantiate it, and create the event.

var logic = new MailChimpMemberEventLogic( _mailChimpOptions );
await logic.CreateMemberEventAsync( _mailChimpListId, email, eventName );

NB, I haven't looked into the response contents...