With all that in mind, the following code should work for you. Let me know if you run into any errors with it, but I think it covers everything.
output = { inlineLinkClicks: [], costPerInlineLinkClick: [], cpm: [], impressions: [], conversions: [] };
const accessToken = "INSERT_ACCESS_TOKEN_HERE";
const actNum = "INSERT_ACT_NUM_HERE";
const queryParams = new URLSearchParams({
'access_token': accessToken,
'breakdowns': 'hourly_stats_aggregated_by_advertiser_time_zone',
'fields': 'inline_link_clicks,cost_per_inline_link_click,cpm,impressions,conversions',
'date_preset': 'today'
});
let appData;
try {
const appResp = await fetch(
`https:
method: 'GET',
}
);
if (appResp.ok) {
appData = await appResp.json();
const insights = appData.data || [];
for (let i = 0; i < insights.length; i++) {
output.inlineLinkClicks[i] = insights[i].inline_link_clicks;
output.costPerInlineLinkClick[i] = insights[i].cost_per_inline_link_click;
output.cpm[i] = insights[i].cpm;
output.impressions[i] = insights[i].impressions;
if (!insights[i].conversions) {
output.conversions.push("0");
} else {
output.conversions.push(insights[i].conversions[0].value);
}
}
console.log(output);
} else {
console.log(`Received status code: ${appResp.status}`);
}
} catch (e) {
console.log(e.message);
}
Caveat: conversions are returned as lists of objects that each include an action type and a value. This code returns only the value of the first item in the list. So for example, using your previous screenshot, it currently includes 1 conversion object. If two existed, as in
conversions
1
action_type schedule_total
value 1
2
action_type schedule_total
value 8
only “1” (the value of the first conversion object - bolded above) will be returned. Instances of “schedule_total” and the value of 8 are ignored. If you need to handle additional data like this, it would be beyond the scope of my free assistance, and I’d recommend hiring a Zapier Expert to refine the code.