How do I get more than 10 entries from Users or Interests using Mailchimp API 3.0?
Asked Answered
N

3

6

I have a few lists in mailchimp, some which have thousands of users and one of the representatives recommended I merge some of my lists and use 'groups' (A.K.A. Interests) to target certain audiences.

I have one 'interest-category' that contains over 40 separate interests, and I want to get their IDs along side the names so I can subscribe users through the API and add them to the right 'groups/interests'.

I am so close to getting the interests along with their names, but the documentation does not say anything about increasing the amount of entries or going to the next 10. http://kb.mailchimp.com/api/resources/lists/interest-categories/interests/lists-interests-collection

If this helps at all, this is the code I used to pull up the interests. (Written in Python and uses the 'requests' library)

import requests
print "Name\tID"
r = requests.auth.HTTPBasicAuth('x', '00000000000000000000000000000-us4')
interest_categories_raw = requests.get('http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/', auth=r)
interest_categories = json.loads(interest_categories_raw.content)
for category in interest_categories['categories']:
    url = 'http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/{0}/interests'.format(str(category['id']))
    interests = json.loads(requests.get(url, auth=r).content)
    for interest in interests['interests']:
        print "{0}\t{1}".format(interest['name'],interest['id'])

Is there any way I can get the rest of the 'interests/groups', both the Name and the ID so I can assign them correctly? (Any way other than uploading test users and patching their interest data and checking the website to see what groups they are added into)

Newsdealer answered 4/6, 2015 at 21:42 Comment(5)
I'm not sure Mailchimp API 3.0 is ready for use.Dubbin
Does Mailchimp API 2.0 have 'interest' functionality?Newsdealer
Yeah, you can do various things with interest groups in 2.0.Dubbin
API v3.0 is live and being used in production in various places. The getting started document covers pagination.Meninges
Yeah, I have developped a package for Mailchimp API v3 in python, check it out github.com/charlesthk/python-mailchimpGreer
N
11

In TooMuchPete's comment, he mentioned that the getting started document mentions pagination. Mailchimp's API uses 'count' and 'offset' for their pagination. They are parameters added to the end of the URL. Count is the amount of items per page and Offset is how far off from the page will start (offset=0 will start on the first entry). So the url

https://us4.api.mailchimp.com/3.0/lists/XXXXXXXXXX/members/?count=5&offset=10

will get 5 items starting on the 11th item ("3rd page", items 11 - 16)

Before TooMuchPete's comment, I answered my own question with this:

I found what I was looking for in terms of the Interest IDs matched with their name. Rather than looking for the IDs by category, the user object already has all of the interests IDs in one array, I just used the IDs to find their names going backwards. For anyone who might have the same problem in the future, this is the code snippet I wrote to find the Names with the IDs:

import requests
print "ID\tName"
r = requests.auth.HTTPBasicAuth('x', '00000000000000000000000000000000-us4')
interest_categories_raw = requests.get('http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/', auth=r)
interest_categories = json.loads(interest_categories_raw.content)

# List of all the interest IDs. Can be found from any user
# https://us4.api.mailchimp.com/3.0/lists/0000000000/members/000000000000000000000000
interest_ids = ["00000000000", "0000000000"]

for i_id in interest_ids:
    for category in interest_categories['categories']:
        url = 'http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/{0}/interests/{1}'.format(str(category['id']), str(i_id))
        raw_response = requests.get(url, auth=r)
        if raw_response.status_code != 404:
            interest = json.loads(raw_response.content)
            print interest['id'] + "\t" + interest['name']

This code will find all the Names and match them to all the IDs of the list. If any one else finds a better way to match names to IDs, please add that to this answer.

Newsdealer answered 4/6, 2015 at 21:42 Comment(0)
C
2

You should use /?count=&offset= parameters, but in order to paginate, the offset must increment not by +1, but +count, because its the offset not the page.

So ?count=50&offset=50 gives you 51-100 (2nd page)

Capitoline answered 14/8, 2015 at 9:32 Comment(3)
The answer here is correct, but the previous answer already contains this information. This answer is basically just a simplified version.Newsdealer
I think that previous answer is not correct, since there is written: "?count=5&offset=1 .. will get 5 items starting on the 2nd page ... (items 6 - 10)". Because it would get items (2-6) skipping just the first item.Capitoline
Ah yes, you are correct. I will correct the previous answer.Newsdealer
D
2

I was having a similar problem with only 10 interests being loaded per interest-category, but buried in the documentation (under pagination, api version 3.0) is that by default the "count" is 10. I simply changed the count parameter to a larger number and all interests were returned without having to use a looping mechanism to pull all records.

https://usX.api.mailchimp.com/3.0/campaigns?offset=0&count=37

Diplomatics answered 14/12, 2016 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.