//native
type Pinterest = {
token: string;
};
/**
* Get campaign analytics
* Get analytics for the specified campaigns in the specified ad_account_id, filtered by the specified options.
*/
export async function main(
auth: Pinterest,
ad_account_id: string,
start_date: string | undefined,
end_date: string | undefined,
campaign_ids: string | undefined,
columns: string | undefined,
granularity: "TOTAL" | "DAY" | "HOUR" | "WEEK" | "MONTH" | undefined,
click_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined,
engagement_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined,
view_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined,
conversion_report_time:
| "TIME_OF_AD_ACTION"
| "TIME_OF_CONVERSION"
| undefined,
) {
const url = new URL(
`https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/campaigns/analytics`,
);
for (const [k, v] of [
["start_date", start_date],
["end_date", end_date],
["campaign_ids", campaign_ids],
["columns", columns],
["granularity", granularity],
["click_window_days", click_window_days],
["engagement_window_days", engagement_window_days],
["view_window_days", view_window_days],
["conversion_report_time", conversion_report_time],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 537 days ago