Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 860
Steven Rogers Avatar asked Jun 04 '15 21:06

Steven Rogers


3 Answers

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.

like image 86
3 revs Avatar answered Nov 23 '22 05:11

3 revs


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)

like image 44
Jan Uhlar Avatar answered Nov 23 '22 05:11

Jan Uhlar


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

like image 37
David Adams Avatar answered Nov 23 '22 04:11

David Adams