Problem: Mailchimp limits you to 1000 entrys at a time, but provides an offset to get the rest.
Solution: In a loop, handle changing the offset so we can collect everything into an array and get the details we need.
- Requires gochimp3
fmt.Println("[Pulling Data From Mailchimp]")
mailchimpListTotalItems, err := mailchimpConnection.NewListResponse("XXXXXXXX").GetMembers(
&gochimp3.InterestCategoriesQueryParams{
ExtendedQueryParams: gochimp3.ExtendedQueryParams{
BasicQueryParams: gochimp3.BasicQueryParams{
Fields: []string{"total_items"}, // Return less data
},
Count: 1,
},
},
)
mailchimpListTotalItemsCount := mailchimpListTotalItems.TotalItems
fmt.Printf("Total members: %d\n", mailchimpListTotalItemsCount)
lastTotal := mailchimpListTotalItemsCount
var mailchimpUSCountry []string
var mailchimpOtherCountry []string
count := 1000
offset := mailchimpListTotalItemsCount - 1000
for offset >= 0 && lastTotal > 0 {
fmt.Printf("Calling entries between %d and %d\n", offset, lastTotal)
mailchimpList, err := mailchimpConnection.NewListResponse("XXXXXXX").GetMembers(
&gochimp3.InterestCategoriesQueryParams{
ExtendedQueryParams: gochimp3.ExtendedQueryParams{
BasicQueryParams: gochimp3.BasicQueryParams{
Fields: []string{"members.email_address,members.merge_fields"}, // Return less data
},
Count: count,
Offset: offset,
},
},
)
if err != nil {
fmt.Println("Failed to get list...")
os.Exit(1)
}
for _, member := range mailchimpList.Members {
// fmt.Printf("%#v %#v\n", member.EmailAddress, member.MergeFields["COUNTRY"])
if member.MergeFields["COUNTRY"] == "United States" {
mailchimpUSCountry = append(mailchimpUSCountry, member.EmailAddress)
}
if member.MergeFields["COUNTRY"] != "United States" {
mailchimpOtherCountry = append(mailchimpOtherCountry, member.EmailAddress)
}
}
fmt.Printf("Mailchimp with US Country: %v\n", len(mailchimpUSCountry))
fmt.Printf("Mailchimp results with non-US Country: %v\n", len(mailchimpOtherCountry))
if count > offset { // Handle offset < 1000
count = offset
}
lastTotal = offset
offset = offset - count
fmt.Println("------------")
}
fmt.Printf("Total: %v", len(mailchimpUSCountry)+len(mailchimpOtherCountry))