0

Get audience insights

by
Published Dec 20, 2024

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.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Get audience insights
7
 * Get Audience Insights for an ad account. The response will return insights for 3 types of audiences: the
8
ad account's engaged audience on Pinterest, the ad account's total audience on Pinterest and Pinterest's
9
total audience.
10
Learn more about Audience Insights.
11
 */
12
export async function main(
13
  auth: Pinterest,
14
  ad_account_id: string,
15
  audience_insight_type:
16
    | "YOUR_TOTAL_AUDIENCE"
17
    | "YOUR_ENGAGED_AUDIENCE"
18
    | "PINTEREST_TOTAL_AUDIENCE"
19
    | undefined,
20
) {
21
  const url = new URL(
22
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audience_insights`,
23
  );
24
  for (const [k, v] of [["audience_insight_type", audience_insight_type]]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42