//native
type Pinterest = {
token: string;
};
/**
* Get audience insights
* Get Audience Insights for an ad account. The response will return insights for 3 types of audiences: the
ad account's engaged audience on Pinterest, the ad account's total audience on Pinterest and Pinterest's
total audience.
Learn more about Audience Insights.
*/
export async function main(
auth: Pinterest,
ad_account_id: string,
audience_insight_type:
| "YOUR_TOTAL_AUDIENCE"
| "YOUR_ENGAGED_AUDIENCE"
| "PINTEREST_TOTAL_AUDIENCE"
| undefined,
) {
const url = new URL(
`https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audience_insights`,
);
for (const [k, v] of [["audience_insight_type", audience_insight_type]]) {
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