-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
82 lines (69 loc) · 2.77 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'dotenv/config';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import client from '@mailchimp/mailchimp_marketing';
import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
const rl = readline.createInterface({ input, output });
const { MAILCHIMP_API, MAILCHIMP_SERVER } = process.env;
if (!MAILCHIMP_API || !MAILCHIMP_SERVER) {
throw Error('Make sure API and server are set in .env, check example.env for info');
}
client.setConfig({
apiKey: MAILCHIMP_API,
server: MAILCHIMP_SERVER,
});
const getDesiredListId = async (allLists) => {
const listId = await rl.question('What list to get campaign data for? Use the id from one of the lists output above, without quotation marks.\n');
if (!allLists.find(l => l.id === listId)) {
console.log(`The id you input doesn't match any in the list. Try again.`);
return getDesiredListId(allLists);
} else {
return listId;
}
};
const askAboutUsingList = async () => {
const answer = await rl.question('Do you want to get all campaigns, regardless of list? (y/n)\n');
if (answer === 'y' || answer === 'n') {
return answer;
} else {
console.log(`Looks like your answer wasn't y or n. Please try again.`);
return askAboutUsingList();
}
};
(async() => {
try {
let allCampaigns = { campaigns: [] };
const useList = await askAboutUsingList();
if (useList === 'y') {
allCampaigns = await await client.campaigns.list({ count: 1000 });
} else {
console.log('Getting all lists.');
const allLists = await client.lists.getAllLists();
console.log('Retrieved these lists: ', allLists.lists);
const desiredListId = await getDesiredListId(allLists.lists);
console.log(`Getting all campaigns for list id ${desiredListId} (limited to 1000)`);
allCampaigns = await client.campaigns.list({list_id: desiredListId, count: 1000});
}
console.log(`Fetched ${allCampaigns.campaigns.length} campaigns.`);
console.log('Fetching URL clicks for each campaign.');
let count = 0;
for (const campaign of allCampaigns.campaigns) {
const clickReport = await client.reports.getCampaignClickDetails(campaign.id);
campaign.urlClicks = clickReport.urls_clicked;
count++;
process.stdout.write(`\u001b[2K\u001b[0EFetched ${count}/${allCampaigns.campaigns.length} URL clicks.`)
}
console.log('\nDone fetching URL clicks!');
console.log('Writing all campaigns + clicks to file.');
fs.writeFileSync(path.join(__dirname, `campaigns-urls-${Date.now()}.json`), JSON.stringify(allCampaigns.campaigns));
console.log(`Data written to campaigns-urls-${Date.now()}.json in src directory.`);
} catch (e) {
throw e;
} finally {
rl.close();
}
})();