Edits history of script submission #11377 for ' Send conversions (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Send conversions
     * The Pinterest API offers advertisers a way to send Pinterest their conversion information (including web conversions, in-app conversions, or even offline conversions) based on their ad_account_id.
     */
    export async function main(
      auth: Pinterest,
      ad_account_id: string,
      test: string | undefined,
      body: {
        data: {
          event_name: string;
          action_source: string;
          event_time: number;
          event_id: string;
          event_source_url?: string;
          opt_out?: false | true;
          partner_name?: string;
          user_data:
            | {
                em: string[];
                hashed_maids?: string[];
                client_ip_address?: string;
                client_user_agent?: string;
              }
            | {
                em?: string[];
                hashed_maids: string[];
                client_ip_address?: string;
                client_user_agent?: string;
              }
            | {
                em?: string[];
                hashed_maids?: string[];
                client_ip_address: string;
                client_user_agent: string;
              };
          custom_data?: {
            currency?: string;
            value?: string;
            content_ids?: string[];
            content_name?: string;
            content_category?: string;
            content_brand?: string;
            contents?: {
              id?: string;
              item_price?: string;
              quantity?: number;
              item_name?: string;
              item_category?: string;
              item_brand?: string;
            }[];
            num_items?: number;
            order_id?: string;
            search_string?: string;
            opt_out_type?: string;
            np?: string;
          };
          app_id?: string;
          app_name?: string;
          app_version?: string;
          device_brand?: string;
          device_carrier?: string;
          device_model?: string;
          device_type?: string;
          os_version?: string;
          wifi?: false | true;
          language?: string;
        }[];
      },
    ) {
      const url = new URL(
        `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/events`,
      );
      for (const [k, v] of [["test", test]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago