Public Beta
Not set

Experience

Changes how pages explain things, never what you can do.

More

Experience

Changes how pages explain things, never what you can do.

Sign inGet started

Marketing Analytics v0.2.1 · generic

Archive SHA-256 8e33c2b8ecf72e82b9edc3bf24453b7c7f30311d425df18c285750c66a34b408 · 48.8 KB · built Nov 27, 2026

src/index.js

// AgentRove native agent: marketing-analytics (MIT).
// Tools: funnel, attribution, cohort_retention, campaign_comparison, build_report, channel_performance. All are offline,
// side-effect free and compute only from the CSV rows the user supplies; nothing is estimated.

import { readFileSync } from "node:fs";
import { defineAgent, makeContext, handleMcpMessage, runCli, runMcpStdio, validateManifest } from "../sdk/index.js";
import { loadData } from "./data.js";
import { channelPerformance } from "./spend.js";
import { funnel as runFunnel, attribution as runAttribution, cohortRetention, campaignComparison } from "./analytics.js";
import { dataSection, funnelSection, attributionSection, campaignSection, campaignTable, retentionSection, cell, prose } from "./report.js";

export { makeContext, handleMcpMessage, runCli, runMcpStdio, validateManifest };

export const manifest = JSON.parse(readFileSync(new URL("../agent.json", import.meta.url), "utf8"));

const hasSessions = (dq) => dq.datasets.some((d) => d.columns.session_id !== undefined);
const isoNow = (ctx) => ctx.now().toISOString().replace(".000Z", "Z");

function missingEvents(events, names) {
  const present = new Set(events.map((e) => e.e));
  return names.filter((n) => !present.has(n));
}

async function funnel(input) {
  const { events, dq, warnings } = loadData(input);
  const r = runFunnel(events, input.steps, input.window_days);
  const missing = missingEvents(events, input.steps);
  if (missing.length) warnings.push(`No rows have these step events: ${missing.join(", ")}.`);
  return { ...r, data_quality: dq, warnings };
}

async function attribution(input) {
  const { events, dq, warnings } = loadData(input);
  const r = runAttribution(events, { conversionEvents: input.conversion_events, lookbackDays: input.lookback_days, groupBy: input.group_by, count: input.count });
  if (!events.some((e) => e.touch)) warnings.push("No UTM-tagged rows were found (utm_source/utm_medium/utm_campaign are empty), so every conversion is unattributed.");
  return { ...r, data_quality: dq, warnings };
}

async function cohort_retention(input) {
  const { events, dq, warnings } = loadData(input);
  const r = cohortRetention(events, { cohortEvent: input.cohort_event, activityEvents: input.activity_events, weeks: input.weeks, smallCohort: input.small_cohort_size });
  return { ...r, data_quality: dq, warnings };
}

async function campaign_comparison(input) {
  const { events, dq, warnings } = loadData(input);
  const r = campaignComparison(events, { conversionEvents: input.conversion_events, groupBy: input.group_by, lookbackDays: input.lookback_days, minSample: input.min_sample, hasSessions: hasSessions(dq) });
  const missing = missingEvents(events, input.conversion_events);
  if (missing.length) warnings.push(`No rows have these conversion events: ${missing.join(", ")}.`);
  return { ...r, markdown_table: campaignTable(r, input.conversion_events), data_quality: dq, warnings };
}

async function build_report(input, ctx) {
  const { events, dq, warnings } = loadData(input);
  const title = input.title ?? "Marketing analytics report";
  const sections = {};
  const md = [`# ${cell(title)}`, `_Generated ${isoNow(ctx)} from ${dq.rows_used} rows in ${dq.datasets.length} file(s). Every number below is computed from your files; nothing is estimated or filled in._`, dataSection(dq)];
  const notes = [...warnings];
  if (input.funnel_steps) {
    sections.funnel = runFunnel(events, input.funnel_steps, input.window_days);
    md.push(funnelSection(sections.funnel));
    notes.push(...sections.funnel.notes);
  }
  if (input.conversion_events) {
    sections.attribution = runAttribution(events, { conversionEvents: input.conversion_events, lookbackDays: input.lookback_days, groupBy: input.group_by ?? "source_medium_campaign" });
    sections.campaigns = campaignComparison(events, { conversionEvents: input.conversion_events, groupBy: input.group_by ?? "campaign", lookbackDays: input.lookback_days, minSample: input.min_sample, hasSessions: hasSessions(dq) });
    md.push(attributionSection(sections.attribution), campaignSection(sections.campaigns, input.conversion_events));
    notes.push(...sections.attribution.notes, ...sections.campaigns.notes);
  }
  if (input.cohorts !== false && events.length) {
    sections.retention = cohortRetention(events, { weeks: input.weeks ?? 8 });
    md.push(retentionSection(sections.retention));
    notes.push(...sections.retention.notes);
  }
  if (!input.funnel_steps && !input.conversion_events) notes.push("Pass funnel_steps for a funnel and conversion_events for attribution and the campaign table.");
  if (notes.length) md.push(`## Notes\n\n${notes.map((n) => `- ${prose(n)}`).join("\n")}`);
  return { report: { title, generated_at: isoNow(ctx), data_quality: dq, sections, notes }, markdown: md.join("\n\n") + "\n" };
}

async function channel_performance(input) {
  return channelPerformance(input);
}

const agent = defineAgent(manifest, { funnel, attribution, cohort_retention, campaign_comparison, build_report, channel_performance });
export default agent;

Find an agent

Do not include passwords, keys, customer data or other confidential details.
Rule-based matching is free during the public beta (fair use).