Edits history of script submission #11309 for ' Get multiple Pin analytics (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Get multiple Pin analytics
     * This endpoint is currently in beta and not available to all apps.
     */
    export async function main(
      auth: Pinterest,
      pin_ids: string | undefined,
      start_date: string | undefined,
      end_date: string | undefined,
      app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined,
      metric_types: string | undefined,
      ad_account_id: string | undefined,
    ) {
      const url = new URL(`https://api.pinterest.com/v5/pins/analytics`);
      for (const [k, v] of [
        ["pin_ids", pin_ids],
        ["start_date", start_date],
        ["end_date", end_date],
        ["app_types", app_types],
        ["metric_types", metric_types],
        ["ad_account_id", ad_account_id],
      ]) {
        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