Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.7 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { ReachinboxAuth } from '../..';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { reachinboxCommon } from '../common/index';

// Define the structure of the summary response
interface SummaryAnalyticsResponse {
  status: number;
  message: string;
  data: {
    totalSent: number;
    open: number;
    openRate: string;
    click: number;
    clickRate: string;
    reply: number;
    replyRate: string;
    result: Array<{
      date: string;
      sent: number;
      totalOpens: string;
      uniqueOpens: string;
      linksClicked: string;
      totalReplies: string;
    }>;
  };
}

export const getSummary = createAction({
  auth: ReachinboxAuth,
  name: 'getSummary',
  classification: 'READ',
  displayName: 'Get Summary',
  description: 'Get a summary of campaign analytics within a date range.',
  audience: 'both',
  aiMetadata: {
    description:
      'Retrieves account-wide outreach analytics (totals and rates for sent, opens, clicks, and replies) aggregated across all campaigns for a date range. Use for an overall performance summary rather than a single campaign. Requires start/end dates in YYYY-MM-DD format; read-only and idempotent.',
    idempotent: true,
  },
  props: {
    startDate: Property.ShortText({
      displayName: 'Start Date',
      description: 'Enter the start date (YYYY-MM-DD). E.g. 2023-10-11',
      required: true,
    }),
    endDate: Property.ShortText({
      displayName: 'End Date',
      description: 'Enter the end date (YYYY-MM-DD). E.g. 2023-12-20',
      required: true,
    }),
  },
  async run(context) {
    const { startDate, endDate } = context.propsValue;

    // Build the URL with the startDate and endDate query parameters
    const url = `https://api.reachinbox.ai/api/v1/analytics/summary?startDate=${startDate}&endDate=${endDate}`;

    try {
      const response = await httpClient.sendRequest<SummaryAnalyticsResponse>({
        method: HttpMethod.GET,
        url: url,
        headers: {
          Authorization: `Bearer ${context.auth.secret_text}`,
        },
      });

      if (response.body.status === 200) {
        return {
          success: true,
          message: response.body.message,
          data: response.body.data,
        };
      } else {
        throw new Error(`Failed to fetch summary: ${response.body.message}`);
      }
    } catch (error: any) {
      if (error.response?.status === 404) {
        return {
          success: false,
          message: 'Summary data not found for the provided date range.',
        };
      } else {
        throw new Error(`Failed to fetch summary: ${error.message}`);
      }
    }
  },
});