Skip to content

Instantly share code, notes, and snippets.

@imCorfitz
Created July 7, 2022 13:08
Show Gist options
  • Save imCorfitz/53bf8501b97e67fc2be5714c8e91f32e to your computer and use it in GitHub Desktop.
Save imCorfitz/53bf8501b97e67fc2be5714c8e91f32e to your computer and use it in GitHub Desktop.
MailChimp combined click report
// Simply add API key and MC region - and set what condition to look for as url - and run `node index.js` in terminal.
const client = require("mailchimp-marketing");
client.setConfig({
apiKey: "MC_API_KEY",
server: "MC_REGION",
});
let total_clicks = {};
const run = async (offset = 0) => {
const { reports } = await client.reports.getAllCampaignReports({
count: 10,
offset,
});
await Promise.all(
reports.map(async (report) => {
const listId = report.list_id;
const { urls_clicked } = await client.reports.getCampaignClickDetails(
report.id,
{
count: 1000,
}
);
const links = urls_clicked.filter((e) => {
return e.url.indexOf("www.example.tld") !== -1;
});
links.map((a) => {
total_clicks[listId]
? (total_clicks[listId] += a.total_clicks)
: (total_clicks[listId] = a.total_clicks);
});
})
);
console.log("Total clicks:", total_clicks);
};
async function init() {
let offset = 0;
// 200 is the amount of campaigns to loop through.
while (offset < 200) {
await run(offset);
offset += 10;
}
}
init();
// Example output:
// Total clicks: { '85b3afbcae': 9, '5adf9ddbbc': 12, '1542a86e5a': 3 }
// Count is separated per list basis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment