private static void BatchSubscribe() { MailChimpManager mc = new MailChimpManager("e20533551f21cbc6b07c76e570dc33c4-us11"); ListResult lists = mc.GetLists(); //1fe090facf List<BatchEmailParameter> emails = new List<BatchEmailParameter>(); BatchEmailParameter email1 = new BatchEmailParameter() { Email = new EmailParameter() { Email = "*****@*****.**" } }; MergeVar mVar1 = new MergeVar(); mVar1.Add("FNAME", "first1" + DateTime.Now); mVar1.Add("LNAME", "last1" + DateTime.Now); email1.MergeVars = mVar1; emails.Add(email1); }
public static void RegisterMergeVarFieldValuesJsonConversion() { if (_registered) { return; } _registered = true; JsConfig <MergeVar> .RawSerializeFn = x => { var dict = x.ToDictionary(a => a.Key, b => b.Value); foreach (System.Reflection.PropertyInfo item in x.GetType().GetSerializableProperties()) { switch (item.Name) { case "Keys": case "Count": case "Values": case "IsReadOnly": continue; default: break; } object val = item.GetValue(x, null); if (val == null) { continue; } string name = item.Name; DataMemberAttribute dataMember = item.GetDataMember(); if (dataMember != null) { name = dataMember.Name; } dict[name] = val; } return(dict.ToJson()); }; JsConfig <MergeVar> .RawDeserializeFn = x => { return(JsonObject.Parse(x).ConvertTo <MergeVar>(y => { MergeVar result = new MergeVar(); foreach (KeyValuePair <string, string> item in y) { switch (item.Key.ToLowerInvariant()) { case "groupings": result.Groupings = y.Get <List <Grouping> >(item.Key); break; case "mc_language": result.Language = y.Get(item.Key); break; case "mc_location": result.LocationData = y.Get <MCLocation>(item.Key); break; case "new-email": result.NewEmail = y.Get(item.Key); break; case "mc_notes-email": result.Notes = y.Get <List <MCNote> >(item.Key); break; case "optin_ip": result.OptInIP = y.Get(item.Key); break; case "optin_time": result.OptInTime = y.Get <DateTime?>(item.Key); break; case "email": // skip break; default: result.Add(item.Key, y.Get <object>(item.Key)); break; } } return result; })); }; }
/// <summary> /// Batches the subscribe. /// </summary> /// <param name="recordList">The records</param> public virtual BatchSubscribeResult BatchSubscribe(IEnumerable<MailChimpEventQueueRecord> recordList) { if (string.IsNullOrEmpty(_mailChimpSettings.DefaultListId)) throw new ArgumentException("MailChimp list is not specified"); var mc = new MailChimpManager(_mailChimpSettings.ApiKey); var batchEmailParam = new List<BatchEmailParameter>(); foreach (var sub in recordList) { try { var emailParam = new EmailParameter { Email = sub.Email }; var mergeVars = new MergeVar(); // TODO Customize your merge vars // get customer and attributes var customer = _customerService.GetCustomerByEmail(sub.Email); if (customer != null) { AddAttribute(mergeVars, customer, SystemCustomerAttributeNames.FirstName, "FNAME"); AddAttribute(mergeVars, customer, SystemCustomerAttributeNames.LastName, "LNAME"); AddAttribute(mergeVars, customer, SystemCustomerAttributeNames.Phone, "PHONE"); var gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender); switch (gender) { case "F": mergeVars.Add("GENDER", "Mujer"); break; case "M": mergeVars.Add("GENDER", "Hombre"); break; default: mergeVars.Add("GENDER", "No especificado"); break; } } //add to group mergeVars.Groupings = new List<Grouping>() {new Grouping()}; mergeVars.Groupings[0].Name = "Yo soy"; mergeVars.Groupings[0].GroupNames = new List<string> { "Deportista" }; batchEmailParam.Add(new BatchEmailParameter() { Email = emailParam, MergeVars = mergeVars }); } catch (Exception ex) { _log.Warning(string.Format("Could not register email {0} to Mailchimp", sub.Email), ex); } } BatchSubscribeResult results = mc.BatchSubscribe(_mailChimpSettings.DefaultListId, batchEmailParam, true, true, false); return results; }
private static void AddAttribute(MergeVar myMergeVars, Customer customer, string attribute, string mailchimpField) { var value = customer.GetAttribute<string>(attribute); myMergeVars.Add(mailchimpField, value); }
public void BatchSubscribe_Successful() { // Arrange MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey); ListResult lists = mc.GetLists(); List<BatchEmailParameter> emails = new List<BatchEmailParameter>(); BatchEmailParameter email1 = new BatchEmailParameter() { Email = new EmailParameter() { Email = "*****@*****.**" } }; MergeVar mVar1 = new MergeVar(); mVar1.Add("FNAME", "first1" + DateTime.Now); mVar1.Add("LNAME", "last1" + DateTime.Now); email1.MergeVars = mVar1; emails.Add(email1); BatchEmailParameter email2 = new BatchEmailParameter() { Email = new EmailParameter() { Email = "*****@*****.**" } }; MergeVar mVar2 = new MergeVar(); mVar2.Add("FNAME", "first2" + DateTime.Now); mVar2.Add("LNAME", "last2" + DateTime.Now); email2.MergeVars = mVar2; emails.Add(email2); // Act BatchSubscribeResult results = mc.BatchSubscribe(lists.Data[0].Id, emails); // Assert Assert.IsNotNull(results); Assert.IsTrue(results.AddCount == 2); // load List<EmailParameter> emailsP = new List<EmailParameter>(); emailsP.Add(email1.Email); MemberInfoResult memberInfo = mc.GetMemberInfo(lists.Data[0].Id, emailsP); Assert.AreEqual(mVar1["FNAME"], memberInfo.Data[0].MemberMergeInfo["FNAME"]); Assert.AreEqual(mVar1["LNAME"], memberInfo.Data[0].MemberMergeInfo["LNAME"]); }
public void SubscribeWithGroupSelectionUsingDictonary_Successful() { // Arrange MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey); ListResult lists = mc.GetLists(); EmailParameter email = new EmailParameter() { Email = "*****@*****.**" }; // find a list with interest groups... string strListID = null; int nGroupingID = 0; string strGroupName = null; foreach (ListInfo li in lists.Data) { List<InterestGrouping> interests = mc.GetListInterestGroupings(li.Id); if (interests != null) { if (interests.Count > 0) { if (interests[0].GroupNames.Count > 0) { strListID = li.Id; nGroupingID = interests[0].Id; strGroupName = interests[0].GroupNames[0].Name; break; } } } } Assert.IsNotNull(strListID, "no lists found in this account with groupings / group names"); Assert.AreNotEqual(0, nGroupingID); Assert.IsNotNull(strGroupName); MergeVar mvso = new MergeVar(); mvso.Groupings = new List<Grouping>(); mvso.Groupings.Add(new Grouping()); mvso.Groupings[0].Id = nGroupingID; mvso.Groupings[0].GroupNames = new List<string>(); mvso.Groupings[0].GroupNames.Add(strGroupName); mvso.Add("FNAME","Testy" + DateTime.Now); mvso.Add("LNAME", "Testerson" + DateTime.Now); // Act EmailParameter results = mc.Subscribe(strListID, email, mvso); // Assert Assert.IsNotNull(results); Assert.IsTrue(!string.IsNullOrEmpty(results.LEId)); // load List<EmailParameter> emails = new List<EmailParameter>(); emails.Add(results); MemberInfoResult memberInfos = mc.GetMemberInfo(strListID, emails); // Assert Assert.AreEqual(1, memberInfos.SuccessCount); Assert.AreEqual(2, memberInfos.Data[0].MemberMergeInfo.Count); Assert.AreEqual(mvso["FNAME"], memberInfos.Data[0].MemberMergeInfo["FNAME"]); Assert.AreEqual(mvso["LNAME"], memberInfos.Data[0].MemberMergeInfo["LNAME"]); }