Last active
August 29, 2015 14:19
-
-
Save andrewdelprete/b4963ca1c9bc33a0491f to your computer and use it in GitHub Desktop.
Retrieve A/B e-mails from Mailchimp API sent-to.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var fs = require('fs'); | |
var apikey = null | |
var cid = null | |
var emailFinalArray = []; | |
var total = 0; | |
var start = 0; | |
var limit = 100; | |
var loopTimes = 0; | |
var counter = 0; | |
(function makeCall() { | |
request.post( | |
'https://us2.api.mailchimp.com/2.0/reports/sent-to.json', | |
{ | |
json: { | |
"apikey": apikey, | |
"cid": cid, | |
"opts": { | |
"status": "example status", | |
"start": start, | |
"limit": limit | |
} | |
} | |
}, | |
function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
loopTimes++; | |
console.log(loopTimes); | |
total = body.total; | |
emailArray = body.data.filter(function(entry) { | |
if (entry.absplit_group == 'a' || entry.absplit_group == 'b') { | |
return true | |
} | |
return false | |
}).map(function(entry) { | |
counter++ | |
return entry.member.email; | |
}); | |
emailFinalArray = emailFinalArray.concat(emailArray); | |
if (start <= total / limit) { | |
makeCall(); | |
start++ | |
return | |
} | |
emailFinalString = emailFinalArray.join(",\n"); | |
fs.writeFile("/tmp/test.txt", emailFinalString, function(err) { | |
if (err) { | |
return console.log(err); | |
} | |
console.log("The file was saved - " + counter); | |
}); | |
} | |
} | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment