Edits history of script submission #11320 for ' Get targeting analytics for an ad account (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Get targeting analytics for an ad account
     * Get targeting analytics for an ad account.
     */
    export async function main(
      auth: Pinterest,
      ad_account_id: string,
      start_date: string | undefined,
      end_date: string | undefined,
      targeting_types: 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,
      attribution_types: "INDIVIDUAL" | "HOUSEHOLD" | undefined,
    ) {
      const url = new URL(
        `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/targeting_analytics`,
      );
      for (const [k, v] of [
        ["start_date", start_date],
        ["end_date", end_date],
        ["targeting_types", targeting_types],
        ["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],
        ["attribution_types", attribution_types],
      ]) {
        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 536 days ago