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

tests/channel-performance.test.ts

import { describe, it, expect } from "vitest";
// @ts-expect-error -- plain JavaScript agent module without type declarations
import agentModule from "../src/index.js";
// @ts-expect-error -- plain JavaScript helper module without type declarations
import { lexNumber, inferNumberFormat, toNumber } from "../src/spend.js";

const agent: any = agentModule;

// Synthetic weekly channel export (original numbers) with the usual export quirks: currency symbols,
// quoted thousands separators, a blank line, an all-empty row, "--" cells, zero spend, mixed label
// case, an unreadable cell and a platform "Total" row.
const WEEKLY = [
  "Week,Channel,Campaign,Spend,Impressions,Clicks,Conversions,Revenue",
  '2026-03-02,Google Ads,spring-search,"$1,240.50","48,200",1310,62,"$5,890.00"',
  '2026-03-02,Meta,Spring-Video,$980.00,"120,450",870,21,$1610.25',
  '2026-03-02,Newsletter,march-digest,$0.00,"14,000",640,38,$2210.00',
  "2026-03-02,LinkedIn,b2b-test,$450.00,9800,95,2,$0.00",
  "",
  '2026-03-09,Google Ads,spring-search,"$1,310.75","51,030",1402,70,"$6,420.10"',
  '2026-03-09,meta,spring-video,"$1,020.00","131,900",910,19,$1490.00',
  '2026-03-09,Newsletter,march-digest,--,"13,750",602,35,$2050.00',
  "2026-03-09,LinkedIn,b2b-test,$460.00,10100,88,--,--",
  ",,,,,,,",
  '2026-03-16,TikTok,creator-pilot,"$300.00",abc,120,3,$190.00',
  'Total,,,"$5,761.25","389,230",5917,247,"$19,670.35"',
].join("\n");

const call = (input: Record<string, unknown>) => agent.call("channel_performance", input);

describe("channel_performance: number parsing", () => {
  it("reads currency symbols, codes, separators, parentheses and blanks", () => {
    expect(lexNumber("$1,240.50")).toMatchObject({ sign: 1, digits: "1,240.50", currency: "$" });
    expect(lexNumber("1.240,50 €")).toMatchObject({ digits: "1.240,50", currency: "EUR" });
    expect(lexNumber("CA$12.00")).toMatchObject({ currency: "CAD" });
    expect(lexNumber("USD 99")).toMatchObject({ digits: "99", currency: "USD" });
    expect(lexNumber("(25.00)")).toMatchObject({ sign: -1, digits: "25.00" });
    expect(lexNumber("--")).toEqual({ blank: true });
    expect(lexNumber("")).toEqual({ blank: true });
    expect(lexNumber("abc")).toBeNull();
    expect(lexNumber("12%")).toBeNull();
    expect(inferNumberFormat(["1,240.50", "48,200"]).format).toBe("1,234.56");
    expect(inferNumberFormat(["1.240,50", "48.200"]).format).toBe("1.234,56");
    expect(inferNumberFormat(["12,5"]).format).toBe("1.234,56");
    expect(inferNumberFormat(["1,240.50", "1.240,50"]).format).toBeNull();
    // Repeated separators are thousands groups.
    expect(inferNumberFormat(["48.200.000"]).format).toBe("1.234,56");
    expect(inferNumberFormat(["48,200,000"]).format).toBe("1,234.56");
    // A lone "1,234" is ambiguous; the default is 1,234.56 and the result says it was not evidenced.
    expect(inferNumberFormat(["1,234"])).toEqual({ format: "1,234.56", inferred: true });
    expect(toNumber(lexNumber("1.310"), "1.234,56")).toBe(1310);
    expect(toNumber(lexNumber("1,310"), "1,234.56")).toBe(1310);
    expect(toNumber(lexNumber("12,34,5"), "1,234.56")).toBeNull();
  });
});

describe("channel_performance: aggregation", () => {
  it("computes per-channel efficiency, skips Total rows and never divides by zero spend", async () => {
    const out = await call({ data: [{ name: "channels-march.csv", csv: WEEKLY }], currency: "USD" });
    expect(out.currency).toBe("USD");
    const g = out.rows.find((r: any) => r.group === "Google Ads");
    expect(g).toMatchObject({ rows: 2, spend: 2551.25, impressions: 99230, clicks: 2712, conversions: 132, revenue: 12310.1, confidence: "ok" });
    expect(g.cpa).toBeCloseTo(2551.25 / 132, 4);
    expect(g.roas).toBeCloseTo(12310.1 / 2551.25, 4);
    expect(g.ctr).toBeCloseTo(2712 / 99230, 4);
    // "Meta" and "meta" are one channel; the first spelling is shown.
    expect(out.rows.filter((r: any) => /meta/i.test(r.group))).toHaveLength(1);
    // Zero recorded spend: cost ratios are n/a, not 0.00, and the channel is not ranked as cheapest.
    const nl = out.rows.find((r: any) => r.group === "Newsletter");
    expect(nl).toMatchObject({ spend: 0, cpc: null, cpm: null, cpa: null, roas: null, conversions: 73 });
    expect(nl.confidence_notes.join(" ")).toMatch(/No spend recorded/);
    expect(out.insights[0]).toMatch(/^Lowest cost per conversion: Google Ads/);
    // Few conversions are flagged, not ranked.
    expect(out.rows.find((r: any) => r.group === "LinkedIn")).toMatchObject({ conversions: 2, confidence: "low" });
    // The Total row is skipped, the TikTok row with "abc" impressions is skipped, both are reported.
    expect(out.totals.spend).toBe(5461.25);
    expect(out.rows.some((r: any) => r.group === "TikTok" || r.group === "Total")).toBe(false);
    const dq = out.data_quality.datasets[0];
    expect(dq.skipped).toMatchObject({ blank: 1, total_row: 1, bad_number: 1 });
    expect(dq.blank_cells).toMatchObject({ spend: 1, conversions: 1, revenue: 1 });
    expect(out.warnings.join(" ")).toMatch(/counted as 0/);
    expect(out.insights.join(" ")).toMatch(/Meta took .*% of spend but delivered .*% of conversions/);
    expect(out.markdown_table.split("\n")[0]).toBe("| channel | Spend | Impr. | Clicks | CTR | CPC | Conv. | Conv. rate | CPA | Revenue | ROAS | Share of spend | Share of conv. |");
    expect(out.markdown_table).toContain("| Newsletter | 0.00 | 27750 | 1242 | 4.5% | n/a |");
  });

  it("groups by ISO week and by channel / campaign", async () => {
    const weeks = await call({ data: [{ name: "w.csv", csv: WEEKLY }], group_by: "week" });
    expect(weeks.rows.map((r: any) => r.group)).toEqual(["2026-W10", "2026-W11"]);
    expect(weeks.warnings.join(" ")).toMatch(/"\$" without a currency code/);
    const cc = await call({ data: [{ name: "w.csv", csv: WEEKLY }], group_by: "channel_campaign" });
    expect(cc.rows.find((r: any) => r.channel === "Google Ads")).toMatchObject({ group: "Google Ads / spring-search", campaign: "spring-search" });
  });

  it("reads decimal-comma exports and labels files without a channel column by name", async () => {
    const eu = "Datum;Kampagne;Kosten;Klicks;Conversions\n2026-03-02;fruehling;1.240,50 €;1.310;62\n2026-03-09;fruehling;1.310,75 €;1.402;70\n";
    const out = await call({ data: [{ name: "google-ads.csv", csv: eu }], columns: { date: "Datum", campaign: "Kampagne", spend: "Kosten", clicks: "Klicks" } });
    expect(out.currency).toBe("EUR");
    expect(out.rows).toHaveLength(1);
    expect(out.rows[0]).toMatchObject({ group: "google-ads", spend: 2551.25, clicks: 2712, conversions: 132, impressions: null, ctr: null });
    expect(out.data_quality.datasets[0].number_format).toBe("1.234,56");
    expect(out.warnings.join(" ")).toMatch(/labelled with the file name "google-ads"/);
    expect(out.markdown_table.split("\n")[0]).not.toMatch(/Impr\.|Revenue|ROAS/);
  });

  it("refuses mixed currencies, ambiguous number styles and missing columns instead of guessing", async () => {
    await expect(call({ data: [{ name: "m.csv", csv: "date,channel,spend\n2026-03-02,A,$100.00\n2026-03-02,B,€80.00\n" }] })).rejects.toMatchObject({ code: "invalid_input", message: expect.stringMatching(/Mixed currencies/) });
    await expect(call({ data: [{ name: "m.csv", csv: "channel,spend\nA,100.00 EUR\n" }], currency: "USD" })).rejects.toMatchObject({ message: expect.stringMatching(/Mixed currencies/) });
    await expect(call({ data: [{ name: "a.csv", csv: "channel,spend\nA,\"1,240.50\"\nB,\"1.240,50\"\n" }] })).rejects.toMatchObject({ message: expect.stringMatching(/set number_format/) });
    await expect(call({ data: [{ name: "n.csv", csv: "channel,note\nA,hello\n" }] })).rejects.toMatchObject({ message: expect.stringMatching(/no metric columns/) });
    await expect(call({ data: [{ name: "n.csv", csv: "channel,spend\nA,1\n" }], group_by: "week" })).rejects.toMatchObject({ message: expect.stringMatching(/needs a date column/) });
    await expect(call({ data: [{ name: "n.csv", csv: "channel,spend\nA,1\n" }], columns: { spend: "Cost" } })).rejects.toMatchObject({ message: expect.stringMatching(/column "Cost" not found/) });
  });

  it("can skip rows with empty cells, rejects negative spend and escapes labels in the table", async () => {
    const csv = "channel,spend,conversions\nA,10.00,--\nB,-5.00,1\n\"<img src=x onerror=alert(1)>|x\",3.00,1\n";
    const out = await call({ data: [{ name: "x.csv", csv }], blank_numbers: "skip_row" });
    expect(out.data_quality.datasets[0].skipped).toMatchObject({ bad_number: 1, negative: 1 });
    expect(out.rows).toHaveLength(1);
    expect(out.markdown_table).not.toContain("<img");
    expect(out.markdown_table).toContain("&lt;img src=x onerror=alert(1)&gt;\\|x");
  });
});

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).