Edits history of script submission #12463 for ' Query Metric Aggregates (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Query Metric Aggregates
     * Query and aggregate event data associated with a metric, including native Klaviyo metrics, integration-specific metrics, and custom events.
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "metric-aggregate";
          attributes: {
            metric_id: string;
            page_cursor?: string;
            measurements: "count" | "sum_value" | "unique"[];
            interval?: "day" | "hour" | "month" | "week";
            page_size?: number;
            by?:
              | "$attributed_channel"
              | "$attributed_flow"
              | "$attributed_message"
              | "$attributed_variation"
              | "$campaign_channel"
              | "$flow"
              | "$flow_channel"
              | "$message"
              | "$message_send_cohort"
              | "$variation"
              | "$variation_send_cohort"
              | "Bot Click"
              | "Bounce Type"
              | "Campaign Name"
              | "Client Canonical"
              | "Client Name"
              | "Client Type"
              | "Email Domain"
              | "Failure Source"
              | "Failure Type"
              | "From Number"
              | "From Phone Region"
              | "Inbox Provider"
              | "List"
              | "Message Name"
              | "Message Type"
              | "Method"
              | "Subject"
              | "To Number"
              | "To Phone Region"
              | "URL"
              | "form_id"[];
            return_fields?: string[];
            filter: string[];
            timezone?: string;
            sort?:
              | "count"
              | "sum_value"
              | "unique"
              | "$attributed_channel"
              | "$attributed_flow"
              | "$attributed_message"
              | "$attributed_variation"
              | "$campaign_channel"
              | "$flow"
              | "$flow_channel"
              | "$message"
              | "$message_send_cohort"
              | "$variation"
              | "$variation_send_cohort"
              | "Bot Click"
              | "Bounce Type"
              | "Campaign Name"
              | "Client Canonical"
              | "Client Name"
              | "Client Type"
              | "Email Domain"
              | "Failure Source"
              | "Failure Type"
              | "From Number"
              | "From Phone Region"
              | "Inbox Provider"
              | "List"
              | "Message Name"
              | "Message Type"
              | "Method"
              | "Subject"
              | "To Number"
              | "To Phone Region"
              | "URL"
              | "form_id"
              | "-$attributed_channel"
              | "-$attributed_flow"
              | "-$attributed_message"
              | "-$attributed_variation"
              | "-$campaign_channel"
              | "-$flow"
              | "-$flow_channel"
              | "-$message"
              | "-$message_send_cohort"
              | "-$variation"
              | "-$variation_send_cohort"
              | "-Bot Click"
              | "-Bounce Type"
              | "-Campaign Name"
              | "-Client Canonical"
              | "-Client Name"
              | "-Client Type"
              | "-Email Domain"
              | "-Failure Source"
              | "-Failure Type"
              | "-From Number"
              | "-From Phone Region"
              | "-Inbox Provider"
              | "-List"
              | "-Message Name"
              | "-Message Type"
              | "-Method"
              | "-Subject"
              | "-To Number"
              | "-To Phone Region"
              | "-URL"
              | "-count"
              | "-form_id"
              | "-sum_value"
              | "-unique";
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/metric-aggregates`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          revision: revision,
          "Accept": "application/vnd.api+json",
          "Content-Type": "application/vnd.api+json",
          Authorization: "Klaviyo-API-Key " + auth.apiKey,
        },
        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 428 days ago