Edits history of script submission #20077 for ' List Delivery Metrics Summary from Destination (segment)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Segment = {
      token: string;
      baseUrl: string;
    };
    /**
     * List Delivery Metrics Summary from Destination
     * Get an event delivery metrics summary from a Destination.
    
    Based on the granularity chosen, there are restrictions on the time range you can query:
    
    **Minute**:
    - Max time range: 4 hours
    - Oldest possible start time: 48 hours in the past
    
    **Hour**:
    - Max Time range: 7 days
    - Oldest possible start time: 7 days in the past
    
    **Day**:
    - Max time range: 14 days
    - Oldest possible start time: 14 days in the past
     */
    export async function main(
      auth: Segment,
      destinationId: string,
      sourceId: string | undefined,
      startTime: string | undefined,
      endTime: string | undefined,
      granularity: "DAY" | "HOUR" | "MINUTE" | undefined,
    ) {
      const url = new URL(
        `${auth.baseUrl}/destinations/${destinationId}/delivery-metrics`,
      );
      for (const [k, v] of [
        ["sourceId", sourceId],
        ["startTime", startTime],
        ["endTime", endTime],
        ["granularity", granularity],
      ]) {
        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.text();
    }
    

    Submitted by hugo697 235 days ago