danesparza / MailChimp.NET

:envelope: .NET Wrapper for the MailChimp v2.0 API
MIT License
179 stars 119 forks source link

Unsubscribed by date range #147

Closed teamofsteve closed 8 years ago

teamofsteve commented 9 years ago

Hello. I'm using Mail Chimp and the API wrapper of the first time so this is probably very basic. I can return a list of all members who have unsubscribed by using MembersResult results = mc.GetAllMembersForList(list.Id, "unsubscribed", 0, 100);

Can something similar be done by date range? I cannot see a param in this method or a similar method to do this. Can anyone advise please?

utillity commented 8 years ago

yes, use a segment to describe the date range:

    private CampaignSegmentOptions CreateChangedSinceCriteria(DateTime? lastSync, DateTime maxDate)
    {
        var result = new CampaignSegmentOptions
        {
            Match = "all",
            Conditions = new List<CampaignSegmentCriteria>()
        };
        var crit = new CampaignSegmentCriteria
        {
            Field = "last_changed",
            Operator = "gt",
            Value = MailChimpManager.ConvertDateTimeToMailChimpAPI(lastSync.Value)
        };
        result.Conditions.Add(crit);
        {
            var crit = new CampaignSegmentCriteria
            {
                Field = "last_changed",
                Operator = "lt",
                Value = MailChimpManager.ConvertDateTimeToMailChimpAPI(maxDate)
            };
            result.Conditions.Add(crit);
        }
        return result;
    }