Skip to content

Instantly share code, notes, and snippets.

@geroembser
Last active April 29, 2020 02:06
Show Gist options
  • Save geroembser/796652aec124d9201a5439a213bedbcf to your computer and use it in GitHub Desktop.
Save geroembser/796652aec124d9201a5439a213bedbcf to your computer and use it in GitHub Desktop.
Download clicked-links data of campaigns from Mailjet's website
async function fetchClickList(pageN, token, apiPathName, referrer) {
const url = `https://app.mailjet.com${apiPathName}`
const options = {
"headers": {
"accept": "*/*",
"accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-csrf-token": token,
"x-requested-with": "XMLHttpRequest"
},
"referrer": referrer,
"referrerPolicy": "no-referrer-when-downgrade",
"body": `limit=50&page=${pageN}`,
"method": "POST",
"mode": "cors",
"credentials": "include"
}
return await fetch(url, options);
}
async function fetchClickListJSON(pageN, token, apiPathName, referrer) {
const response = await fetchClickList(pageN, token, apiPathName, referrer)
if (response.status !== 200) {
throw new Error(`Failed to get data from mailjet, got response: ${JSON.stringify(response)}`)
}
return await response.json()
}
async function getPaginationInfo(token, apiPathName, referrer) {
return (await fetchClickListJSON(0, token, apiPathName, referrer)).pagination
}
async function getPage(pageN, token, apiPathName, referrer) {
return (await fetchClickListJSON(pageN, token, apiPathName, referrer)).clickList
}
async function getClickList(token, apiPathName, referrer) {
//get page information and query all pages...
const paginationInfo = await getPaginationInfo(token, apiPathName, referrer)
const clickList = []
for (let i = paginationInfo.first; i <= paginationInfo.last; i++) {
console.log(`Requesting page ${i}`)
clickList.push(...(await getPage(i, token, apiPathName, referrer)))
}
return clickList
}
async function main(token, apiPathName, referrer) {
console.log(JSON.stringify(await getClickList(token, apiPathName, referrer)))
}
function getCurrentTokenFromMailjet() {
return document.querySelector('meta[name="csrf_token"]').content
}
function getCurrentReferrerURL() { //get the request URL for the current campaign
return location.protocol + '//' + location.host + location.pathname
}
function getCurrentCampaignPathname() { //returns the pathname to get the clickList data of the current campaign (that is visible on screen)
return Object.values(__MJ_GLOBAL__.components).filter( c => c.name === "PageStats")[0].context.data.urls.statsCampaignLinksClickList
}
let token = getCurrentTokenFromMailjet() //gets the current token from mailjet, if you're on the appropriate site, otherwise enter it manually here
let apiPathName = getCurrentCampaignPathname()
let referrer = getCurrentReferrerURL()
main(token, apiPathName, referrer)
@geroembser
Copy link
Author

Because it looks like the usual download function on Mailjet's website does not work, we needed to find another way to get the exact data of those people that clicked a link in a campaign together with the user specific clicked links.
How to use:
➔ Open Mailjet's website
➔ Login
➔ Open the campaign which links the statistics you're interested in
➔ go to Dev Tools in Chrome
➔ add this script as a snippet or just copy it in the console
➔ see the results in the console's output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment