fix: use backlinks history for default trends (#69)

* fix: use backlinks history for default trends

* simplify backlinks: remove filters, always use history endpoint

Remove the filter UI (status, subdomains, indirect links, exclude internal)
and hardcode defaults across the stack. Replace the conditional
timeseries_summary + timeseries_new_lost_summary fallback with a single
backlinks/history/live call for trend data. This reduces the overview from
5 parallel API calls to 3 and removes all conditional branching.

* improve charts

* fix: refresh backlinks cost docs
This commit is contained in:
Ben Senescu 2026-04-04 18:21:42 -04:00 committed by Ben Senescu
parent fdb2d453e4
commit d773105e8d
21 changed files with 135 additions and 399 deletions

View File

@ -267,7 +267,7 @@ That means you can try OpenSEO for free with the starter credit, then decide if/
### 4) Backlinks search ### 4) Backlinks search
- Backlinks search costs about `$0.08` for a domain or `$0.04` for a page. - Backlinks search costs about `$0.06` for a domain or `$0.04` for a page.
- Opening extra tabs like `Referring Domains` or `Top Pages` adds about `+$0.02` each. - Opening extra tabs like `Referring Domains` or `Top Pages` adds about `+$0.02` each.
- Exact cost can vary slightly based on returned rows and DataForSEO pricing. - Exact cost can vary slightly based on returned rows and DataForSEO pricing.
@ -276,7 +276,7 @@ That means you can try OpenSEO for free with the starter credit, then decide if/
- 100 keyword research requests at the default 150 results: `$3.50` - 100 keyword research requests at the default 150 results: `$3.50`
- 100 keyword research requests at 500 results each: `$7.00` - 100 keyword research requests at 500 results each: `$7.00`
- 100 domain overviews (200 ranked keywords each): `$4.01` - 100 domain overviews (200 ranked keywords each): `$4.01`
- 100 backlinks domain searches at current defaults before opening extra tabs: about `$8.38` - 100 backlinks domain searches at current defaults before opening extra tabs: about `$6.34`
- 100 backlinks page searches at current defaults before opening extra tabs: about `$4.30` - 100 backlinks page searches at current defaults before opening extra tabs: about `$4.30`
- 100 fully explored backlinks domain searches: about `$12.98` - 100 fully explored backlinks domain searches: about `$10.94`
- 100 fully explored backlinks page searches: about `$8.61` - 100 fully explored backlinks page searches: about `$8.61`

View File

@ -2,7 +2,10 @@ import { existsSync, readFileSync } from "node:fs";
import process from "node:process"; import process from "node:process";
import { createBacklinksService } from "@/server/features/backlinks/services/BacklinksService"; import { createBacklinksService } from "@/server/features/backlinks/services/BacklinksService";
import type { BillingCustomerContext } from "@/server/billing/subscription"; import type { BillingCustomerContext } from "@/server/billing/subscription";
import type { BacklinksLookupInput } from "@/types/schemas/backlinks"; import type {
BacklinksLookupInput,
BacklinksTargetScope,
} from "@/types/schemas/backlinks";
loadLocalEnv(); loadLocalEnv();
@ -84,6 +87,7 @@ async function main() {
function buildInput(cliArgs: Record<string, string>): BacklinksLookupInput { function buildInput(cliArgs: Record<string, string>): BacklinksLookupInput {
const target = cliArgs.target; const target = cliArgs.target;
const scope = parseScope(cliArgs.scope);
if (!target) { if (!target) {
printUsageAndExit("Missing target."); printUsageAndExit("Missing target.");
} }
@ -93,10 +97,7 @@ function buildInput(cliArgs: Record<string, string>): BacklinksLookupInput {
return { return {
target, target,
includeSubdomains: parseBoolean(cliArgs.subdomains, true), scope,
includeIndirectLinks: parseBoolean(cliArgs.indirect, true),
excludeInternalBacklinks: parseBoolean(cliArgs.excludeInternal, true),
status: parseStatus(cliArgs.status),
}; };
} }
@ -147,13 +148,12 @@ function parseBoolean(value: string | undefined, fallback: boolean) {
return value === "true"; return value === "true";
} }
function parseStatus( function parseScope(
value: string | undefined, value: string | undefined,
): BacklinksLookupInput["status"] { ): BacklinksTargetScope | undefined {
if (value === "live" || value === "lost" || value === "all") { if (!value) return undefined;
return value; if (value === "domain" || value === "page") return value;
} printUsageAndExit(`Invalid scope: ${value}. Expected domain or page.`);
return "live";
} }
function parsePositiveInteger(value: string | undefined, fallback: number) { function parsePositiveInteger(value: string | undefined, fallback: number) {
@ -185,7 +185,7 @@ function loadLocalEnv() {
function printUsageAndExit(message: string): never { function printUsageAndExit(message: string): never {
console.error(message); console.error(message);
console.error( console.error(
"Usage: pnpm billing:backlinks --target=example.com --confirmLive=true [--status=live|lost|all] [--subdomains=true|false] [--indirect=true|false] [--excludeInternal=true|false] [--repeat=1] [--includeTabs=true|false] [--allowCi=true]", "Usage: pnpm billing:backlinks --target=example.com --confirmLive=true [--scope=domain|page] [--repeat=1] [--includeTabs=true|false] [--allowCi=true]",
); );
process.exit(1); process.exit(1);
} }

View File

@ -49,13 +49,20 @@ export function BacklinksTrendChart({
tickFormatter={formatChartTick} tickFormatter={formatChartTick}
minTickGap={24} minTickGap={24}
/> />
<YAxis /> <YAxis yAxisId="left" tickFormatter={formatAxisValue} width={60} />
<YAxis
yAxisId="right"
orientation="right"
tickFormatter={formatAxisValue}
width={60}
/>
<Tooltip <Tooltip
formatter={formatTooltipValue} formatter={formatTooltipValue}
labelFormatter={formatChartLabel} labelFormatter={formatChartLabel}
/> />
<Legend /> <Legend />
<Line <Line
yAxisId="left"
type="monotone" type="monotone"
dataKey="backlinks" dataKey="backlinks"
stroke="#2563eb" stroke="#2563eb"
@ -64,6 +71,7 @@ export function BacklinksTrendChart({
name="Backlinks" name="Backlinks"
/> />
<Line <Line
yAxisId="right"
type="monotone" type="monotone"
dataKey="referringDomains" dataKey="referringDomains"
stroke="#14b8a6" stroke="#14b8a6"
@ -111,12 +119,20 @@ export function BacklinksNewLostChart({
tickFormatter={formatChartTick} tickFormatter={formatChartTick}
minTickGap={24} minTickGap={24}
/> />
<YAxis /> <YAxis tickFormatter={formatAxisValue} width={60} />
<Tooltip <Tooltip
formatter={formatTooltipValue} formatter={formatTooltipValue}
labelFormatter={formatChartLabel} labelFormatter={formatChartLabel}
/> />
<Legend /> <Legend />
<Line
type="monotone"
dataKey="lostBacklinks"
stroke="#ef4444"
strokeWidth={2}
dot={false}
name="Lost backlinks"
/>
<Line <Line
type="monotone" type="monotone"
dataKey="newBacklinks" dataKey="newBacklinks"
@ -125,14 +141,6 @@ export function BacklinksNewLostChart({
dot={false} dot={false}
name="New backlinks" name="New backlinks"
/> />
<Line
type="monotone"
dataKey="lostBacklinks"
stroke="#dc2626"
strokeWidth={2}
dot={false}
name="Lost backlinks"
/>
</LineChart> </LineChart>
) : null} ) : null}
</div> </div>
@ -174,6 +182,13 @@ function EmptyChartState() {
); );
} }
function formatAxisValue(value: unknown) {
if (typeof value !== "number") return "";
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
if (value >= 1_000) return `${(value / 1_000).toFixed(0)}K`;
return String(value);
}
function formatChartTick(value: unknown) { function formatChartTick(value: unknown) {
return typeof value === "string" ? formatMonthLabel(value) : ""; return typeof value === "string" ? formatMonthLabel(value) : "";
} }

View File

@ -129,7 +129,7 @@ function BacklinksContent({
useEffect(() => { useEffect(() => {
setFilterText(""); setFilterText("");
}, [searchState.target, searchState.status, searchState.tab]); }, [searchState.target, searchState.tab]);
const mergedData = useMemo( const mergedData = useMemo(
() => mergeTabData(data, referringDomains, topPages), () => mergeTabData(data, referringDomains, topPages),

View File

@ -9,10 +9,7 @@ import {
import type { BacklinksSearchState } from "./backlinksPageTypes"; import type { BacklinksSearchState } from "./backlinksPageTypes";
import { resolveBacklinksSearchScope } from "./backlinksSearchScope"; import { resolveBacklinksSearchScope } from "./backlinksSearchScope";
type SearchDraft = Pick< type SearchDraft = Pick<BacklinksSearchState, "target" | "scope">;
BacklinksSearchState,
"target" | "scope" | "subdomains" | "indirect" | "excludeInternal" | "status"
>;
function getBacklinksValidationErrors( function getBacklinksValidationErrors(
value: SearchDraft, value: SearchDraft,
@ -44,7 +41,6 @@ export function BacklinksSearchCard({
isFetching: boolean; isFetching: boolean;
onSubmit: (values: SearchDraft) => void; onSubmit: (values: SearchDraft) => void;
}) { }) {
const [showAdvanced, setShowAdvanced] = useState(false);
const [userSelectedScope, setUserSelectedScope] = useState(false); const [userSelectedScope, setUserSelectedScope] = useState(false);
const form = useForm({ const form = useForm({
defaultValues: initialValues, defaultValues: initialValues,
@ -93,7 +89,7 @@ export function BacklinksSearchCard({
return ( return (
<label <label
className={`input input-bordered lg:col-span-8 flex items-center gap-2 ${targetError ? "input-error" : ""}`} className={`input input-bordered lg:col-span-10 flex items-center gap-2 ${targetError ? "input-error" : ""}`}
> >
<Search className="size-4 text-base-content/60" /> <Search className="size-4 text-base-content/60" />
<input <input
@ -119,28 +115,6 @@ export function BacklinksSearchCard({
}} }}
</form.Field> </form.Field>
<form.Field name="status">
{(field) => (
<select
className="select select-bordered lg:col-span-2"
value={field.state.value}
onChange={(event) =>
field.handleChange(
event.target.value === "lost"
? "lost"
: event.target.value === "all"
? "all"
: "live",
)
}
>
<option value="live">Live links</option>
<option value="lost">Lost links</option>
<option value="all">All links</option>
</select>
)}
</form.Field>
<form.Subscribe selector={(state) => state.isSubmitting}> <form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => ( {(isSubmitting) => (
<button <button
@ -193,70 +167,6 @@ export function BacklinksSearchCard({
</form.Field> </form.Field>
</div> </div>
</div> </div>
<div className="flex flex-wrap items-center gap-3">
<form.Field name="subdomains">
{(field) => (
<label className="label cursor-pointer gap-2 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">Include subdomains</span>
</label>
)}
</form.Field>
<button
type="button"
className="btn btn-ghost btn-sm"
onClick={() => setShowAdvanced((current) => !current)}
>
{showAdvanced ? "Hide advanced" : "Show advanced"}
</button>
</div>
{showAdvanced ? (
<div className="grid grid-cols-1 gap-3 rounded-xl border border-base-300 bg-base-200/40 p-4 text-sm md:grid-cols-2">
<form.Field name="indirect">
{(field) => (
<label className="label cursor-pointer justify-start gap-3 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">Include indirect links</span>
</label>
)}
</form.Field>
<form.Field name="excludeInternal">
{(field) => (
<label className="label cursor-pointer justify-start gap-3 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">
Exclude internal backlinks
</span>
</label>
)}
</form.Field>
</div>
) : null}
</form> </form>
{errorMessage ? ( {errorMessage ? (

View File

@ -1,5 +1,4 @@
import type { import type {
BacklinksStatus,
BacklinksTab, BacklinksTab,
BacklinksTargetScope, BacklinksTargetScope,
} from "@/types/schemas/backlinks"; } from "@/types/schemas/backlinks";
@ -26,10 +25,6 @@ export type BacklinksTopPagesData = Awaited<
export type BacklinksSearchState = { export type BacklinksSearchState = {
target: string; target: string;
scope: BacklinksTargetScope; scope: BacklinksTargetScope;
subdomains: boolean;
indirect: boolean;
excludeInternal: boolean;
status: BacklinksStatus;
tab: BacklinksTab; tab: BacklinksTab;
}; };

View File

@ -60,19 +60,8 @@ export function useBacklinksPageData({
() => ({ () => ({
target: searchState.target, target: searchState.target,
scope: searchState.scope, scope: searchState.scope,
subdomains: searchState.subdomains,
indirect: searchState.indirect,
excludeInternal: searchState.excludeInternal,
status: searchState.status,
}), }),
[ [searchState.scope, searchState.target],
searchState.excludeInternal,
searchState.indirect,
searchState.scope,
searchState.status,
searchState.subdomains,
searchState.target,
],
); );
const testAccessMutation = useMutation({ const testAccessMutation = useMutation({
@ -86,10 +75,6 @@ export function useBacklinksPageData({
projectId, projectId,
searchState.scope, searchState.scope,
searchState.target, searchState.target,
searchState.subdomains,
searchState.indirect,
searchState.excludeInternal,
searchState.status,
] as const; ] as const;
const overviewQuery = useQuery({ const overviewQuery = useQuery({
@ -166,25 +151,13 @@ export function useBacklinksPageData({
export function navigateToBacklinksSearch( export function navigateToBacklinksSearch(
navigate: BacklinksPageProps["navigate"], navigate: BacklinksPageProps["navigate"],
values: Pick< values: Pick<BacklinksSearchState, "target" | "scope">,
BacklinksSearchState,
| "target"
| "scope"
| "subdomains"
| "indirect"
| "excludeInternal"
| "status"
>,
) { ) {
navigate({ navigate({
search: (prev) => ({ search: (prev) => ({
...prev, ...prev,
target: values.target, target: values.target,
scope: getPersistedBacklinksSearchScope(values.target, values.scope), scope: getPersistedBacklinksSearchScope(values.target, values.scope),
subdomains: values.subdomains ? undefined : false,
indirect: values.indirect ? undefined : false,
excludeInternal: values.excludeInternal ? undefined : false,
status: values.status === "live" ? undefined : values.status,
tab: undefined, tab: undefined,
}), }),
replace: true, replace: true,
@ -212,10 +185,6 @@ function buildBacklinksRequestInput(
projectId, projectId,
target: searchState.target, target: searchState.target,
scope: searchState.scope, scope: searchState.scope,
includeSubdomains: searchState.subdomains,
includeIndirectLinks: searchState.indirect,
excludeInternalBacklinks: searchState.excludeInternal,
status: searchState.status,
}; };
} }

View File

@ -11,15 +11,7 @@ export const Route = createFileRoute("/_project/p/$projectId/backlinks")({
function BacklinksRoute() { function BacklinksRoute() {
const { projectId } = Route.useParams(); const { projectId } = Route.useParams();
const navigate = useNavigate({ from: Route.fullPath }); const navigate = useNavigate({ from: Route.fullPath });
const { const { target = "", scope: rawScope, tab = "backlinks" } = Route.useSearch();
target = "",
scope: rawScope,
subdomains = true,
indirect = true,
excludeInternal = true,
status = "live",
tab = "backlinks",
} = Route.useSearch();
const scope = rawScope ?? inferBacklinksSearchScopeFromTarget(target); const scope = rawScope ?? inferBacklinksSearchScopeFromTarget(target);
return ( return (
@ -29,10 +21,6 @@ function BacklinksRoute() {
searchState={{ searchState={{
target, target,
scope, scope,
subdomains,
indirect,
excludeInternal,
status,
tab, tab,
}} }}
/> />

View File

@ -4,8 +4,7 @@ const backlinksSummaryMock = vi.fn();
const backlinksRowsMock = vi.fn(); const backlinksRowsMock = vi.fn();
const referringDomainsMock = vi.fn(); const referringDomainsMock = vi.fn();
const domainPagesMock = vi.fn(); const domainPagesMock = vi.fn();
const timeseriesSummaryMock = vi.fn(); const backlinksHistoryMock = vi.fn();
const newLostTimeseriesMock = vi.fn();
vi.mock("@/server/lib/r2-cache", () => ({ vi.mock("@/server/lib/r2-cache", () => ({
buildCacheKey: vi.fn( buildCacheKey: vi.fn(
@ -27,8 +26,7 @@ vi.mock("@/server/lib/dataforseoClient", () => ({
rows: backlinksRowsMock, rows: backlinksRowsMock,
referringDomains: referringDomainsMock, referringDomains: referringDomainsMock,
domainPages: domainPagesMock, domainPages: domainPagesMock,
timeseriesSummary: timeseriesSummaryMock, history: backlinksHistoryMock,
newLostTimeseries: newLostTimeseriesMock,
}, },
})), })),
})); }));
@ -98,17 +96,12 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
rel_attributes: ["noopener"], rel_attributes: ["noopener"],
}, },
]); ]);
timeseriesSummaryMock.mockResolvedValue([ backlinksHistoryMock.mockResolvedValue([
{ {
date: "2026-02-01", date: "2026-02-01",
backlinks: 1100, backlinks: 1100,
referring_domains: 300, referring_domains: 300,
rank: 40, rank: 40,
},
]);
newLostTimeseriesMock.mockResolvedValue([
{
date: "2026-02-01",
new_backlinks: 20, new_backlinks: 20,
lost_backlinks: 5, lost_backlinks: 5,
new_referring_domains: 3, new_referring_domains: 3,
@ -117,23 +110,11 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
]); ]);
const first = await service.profileOverview( const first = await service.profileOverview(
{ { target: "example.com" },
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
billingCustomer, billingCustomer,
); );
const second = await service.profileOverview( const second = await service.profileOverview(
{ { target: "example.com" },
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
billingCustomer, billingCustomer,
); );
@ -142,6 +123,7 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
expect(referringDomainsMock).not.toHaveBeenCalled(); expect(referringDomainsMock).not.toHaveBeenCalled();
expect(domainPagesMock).not.toHaveBeenCalled(); expect(domainPagesMock).not.toHaveBeenCalled();
expect(backlinksSummaryMock).toHaveBeenCalledOnce(); expect(backlinksSummaryMock).toHaveBeenCalledOnce();
expect(backlinksHistoryMock).toHaveBeenCalledOnce();
expect(second).toEqual(first); expect(second).toEqual(first);
}); });
@ -175,23 +157,11 @@ it("profiles referring domains and top pages separately", async () => {
]); ]);
const domains = await service.profileReferringDomains( const domains = await service.profileReferringDomains(
{ { target: "https://example.com/foo" },
target: "https://example.com/foo",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
billingCustomer, billingCustomer,
); );
const pages = await service.profileTopPages( const pages = await service.profileTopPages(
{ { target: "https://example.com/foo" },
target: "https://example.com/foo",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
billingCustomer, billingCustomer,
); );
@ -221,13 +191,7 @@ it("does not fall back to target spam score for referring domains", async () =>
]); ]);
const domains = await service.profileReferringDomains( const domains = await service.profileReferringDomains(
{ { target: "example.com" },
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
billingCustomer, billingCustomer,
); );
@ -256,16 +220,9 @@ it("keeps cache entries isolated per organization", async () => {
lost_referring_domains: 2, lost_referring_domains: 2,
}); });
backlinksRowsMock.mockResolvedValue([]); backlinksRowsMock.mockResolvedValue([]);
timeseriesSummaryMock.mockResolvedValue([]); backlinksHistoryMock.mockResolvedValue([]);
newLostTimeseriesMock.mockResolvedValue([]);
const input = { const input = { target: "example.com" };
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live" as const,
};
await service.profileOverview(input, billingCustomer); await service.profileOverview(input, billingCustomer);
await service.profileOverview(input, { await service.profileOverview(input, {

View File

@ -67,10 +67,6 @@ async function buildOverviewCacheKey(
organizationId: billingCustomer.organizationId, organizationId: billingCustomer.organizationId,
target: normalizedTarget.apiTarget, target: normalizedTarget.apiTarget,
scope: normalizedTarget.scope, scope: normalizedTarget.scope,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
}); });
} }
@ -86,10 +82,6 @@ async function buildTabCacheKey(
organizationId: billingCustomer.organizationId, organizationId: billingCustomer.organizationId,
target: normalizedTarget.apiTarget, target: normalizedTarget.apiTarget,
scope: normalizedTarget.scope, scope: normalizedTarget.scope,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
}); });
} }

View File

@ -57,10 +57,6 @@ export const backlinksOverviewSchema = z.object({
target: z.string(), target: z.string(),
displayTarget: z.string(), displayTarget: z.string(),
scope: z.enum(["domain", "page"]), scope: z.enum(["domain", "page"]),
includeSubdomains: z.boolean(),
includeIndirectLinks: z.boolean(),
excludeInternalBacklinks: z.boolean(),
status: z.enum(["live", "lost", "all"]),
summary: z.object({ summary: z.object({
rank: z.number().nullable(), rank: z.number().nullable(),
backlinks: z.number().nullable(), backlinks: z.number().nullable(),

View File

@ -2,12 +2,11 @@ import { z } from "zod";
import type { BillingCustomerContext } from "@/server/billing/subscription"; import type { BillingCustomerContext } from "@/server/billing/subscription";
import { import {
type BacklinksRequest, type BacklinksRequest,
type fetchBacklinksHistoryRaw,
type fetchBacklinksRowsRaw, type fetchBacklinksRowsRaw,
type fetchBacklinksSummaryRaw, type fetchBacklinksSummaryRaw,
type fetchDomainPagesSummaryRaw, type fetchDomainPagesSummaryRaw,
type fetchNewLostTimeseriesRaw,
type fetchReferringDomainsRaw, type fetchReferringDomainsRaw,
type fetchTimeseriesSummaryRaw,
normalizeBacklinksTarget, normalizeBacklinksTarget,
} from "@/server/lib/dataforseoBacklinks"; } from "@/server/lib/dataforseoBacklinks";
import { createDataforseoClient } from "@/server/lib/dataforseoClient"; import { createDataforseoClient } from "@/server/lib/dataforseoClient";
@ -74,28 +73,26 @@ export async function profileBacklinksOverview(
const normalizedTarget = normalizeBacklinksTarget(input.target, { const normalizedTarget = normalizeBacklinksTarget(input.target, {
scope: input.scope, scope: input.scope,
}); });
const request = buildBacklinksRequest(input, normalizedTarget.apiTarget); const request = buildBacklinksRequest(normalizedTarget.apiTarget);
const dateRange = buildBacklinksDateRange(now); const dateRange = buildBacklinksDateRange(now);
const [summary, backlinks, trends, newLostTrends] = await Promise.all([ const [summary, backlinks, history] = await Promise.all([
dataforseo.backlinks.summary(request), dataforseo.backlinks.summary(request),
dataforseo.backlinks.rows({ ...request, limit: 100 }), dataforseo.backlinks.rows({ ...request, limit: 100 }),
normalizedTarget.scope === "domain" normalizedTarget.scope === "domain"
? dataforseo.backlinks.timeseriesSummary({ ...request, ...dateRange }) ? dataforseo.backlinks.history({
: Promise.resolve([]), target: normalizedTarget.apiTarget,
normalizedTarget.scope === "domain" ...dateRange,
? dataforseo.backlinks.newLostTimeseries({ ...request, ...dateRange }) })
: Promise.resolve([]), : Promise.resolve([]),
]); ]);
const overview = buildOverviewResult({ const overview = buildOverviewResult({
input,
normalizedTarget, normalizedTarget,
now, now,
summary, summary,
backlinks, backlinks,
trends, history,
newLostTrends,
}); });
await cacheValue( await cacheValue(
cache, cache,
@ -124,7 +121,6 @@ export async function profileReferringDomainsRows(
const dataforseo = createDataforseoClient(billingCustomer); const dataforseo = createDataforseoClient(billingCustomer);
const request = buildBacklinksRequest( const request = buildBacklinksRequest(
input,
normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget, normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget,
); );
const response = await dataforseo.backlinks.referringDomains({ const response = await dataforseo.backlinks.referringDomains({
@ -155,7 +151,6 @@ export async function profileTopPagesRows(
const dataforseo = createDataforseoClient(billingCustomer); const dataforseo = createDataforseoClient(billingCustomer);
const request = buildBacklinksRequest( const request = buildBacklinksRequest(
input,
normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget, normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget,
); );
const response = await dataforseo.backlinks.domainPages({ const response = await dataforseo.backlinks.domainPages({
@ -169,17 +164,8 @@ export async function profileTopPagesRows(
return { rows }; return { rows };
} }
function buildBacklinksRequest( function buildBacklinksRequest(target: string): BacklinksRequest {
input: BacklinksLookupInput, return { target };
target: string,
): BacklinksRequest {
return {
target,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
};
} }
function buildBacklinksDateRange(now: Date): BacklinksDateRange { function buildBacklinksDateRange(now: Date): BacklinksDateRange {
@ -199,22 +185,37 @@ function buildBacklinksDateRange(now: Date): BacklinksDateRange {
} }
function buildOverviewResult(args: { function buildOverviewResult(args: {
input: BacklinksLookupInput;
normalizedTarget: ReturnType<typeof normalizeBacklinksTarget>; normalizedTarget: ReturnType<typeof normalizeBacklinksTarget>;
now: Date; now: Date;
summary: Awaited<ReturnType<typeof fetchBacklinksSummaryRaw>>["data"]; summary: Awaited<ReturnType<typeof fetchBacklinksSummaryRaw>>["data"];
backlinks: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"]; backlinks: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"];
trends: Awaited<ReturnType<typeof fetchTimeseriesSummaryRaw>>["data"]; history: Awaited<ReturnType<typeof fetchBacklinksHistoryRaw>>["data"];
newLostTrends: Awaited<ReturnType<typeof fetchNewLostTimeseriesRaw>>["data"];
}): BacklinksOverviewResult { }): BacklinksOverviewResult {
const historyRows = args.history
.map((item) => ({
date: normalizeHistoryDate(item.date),
backlinks: item.backlinks ?? null,
referringDomains: item.referring_domains ?? null,
rank: item.rank ?? null,
newBacklinks: item.new_backlinks ?? null,
lostBacklinks: item.lost_backlinks ?? null,
newReferringDomains:
item.new_referring_domains ?? item.new_reffering_domains ?? null,
lostReferringDomains:
item.lost_referring_domains ?? item.lost_reffering_domains ?? null,
}))
.filter(
(
item,
): item is typeof item & {
date: string;
} => item.date !== null,
);
return { return {
target: args.normalizedTarget.apiTarget, target: args.normalizedTarget.apiTarget,
displayTarget: args.normalizedTarget.displayTarget, displayTarget: args.normalizedTarget.displayTarget,
scope: args.normalizedTarget.scope, scope: args.normalizedTarget.scope,
includeSubdomains: args.input.includeSubdomains,
includeIndirectLinks: args.input.includeIndirectLinks,
excludeInternalBacklinks: args.input.excludeInternalBacklinks,
status: args.input.status,
summary: { summary: {
rank: args.summary.rank ?? null, rank: args.summary.rank ?? null,
backlinks: args.summary.backlinks ?? null, backlinks: args.summary.backlinks ?? null,
@ -238,29 +239,27 @@ function buildOverviewResult(args: {
backlinks: mapBacklinksRows(args.backlinks), backlinks: mapBacklinksRows(args.backlinks),
referringDomains: [], referringDomains: [],
topPages: [], topPages: [],
trends: args.trends trends: historyRows.map((item) => ({
.filter((item) => Boolean(item.date)) date: item.date,
.map((item) => ({ backlinks: item.backlinks,
date: item.date ?? "", referringDomains: item.referringDomains,
backlinks: item.backlinks ?? null, rank: item.rank,
referringDomains: item.referring_domains ?? null,
rank: item.rank ?? null,
})), })),
newLostTrends: args.newLostTrends newLostTrends: historyRows.map((item) => ({
.filter((item) => Boolean(item.date)) date: item.date,
.map((item) => ({ newBacklinks: item.newBacklinks,
date: item.date ?? "", lostBacklinks: item.lostBacklinks,
newBacklinks: item.new_backlinks ?? null, newReferringDomains: item.newReferringDomains,
lostBacklinks: item.lost_backlinks ?? null, lostReferringDomains: item.lostReferringDomains,
newReferringDomains:
item.new_referring_domains ?? item.new_reffering_domains ?? null,
lostReferringDomains:
item.lost_referring_domains ?? item.lost_reffering_domains ?? null,
})), })),
fetchedAt: args.now.toISOString(), fetchedAt: args.now.toISOString(),
}; };
} }
function normalizeHistoryDate(value: string | null | undefined) {
return value ? value.slice(0, 10) : null;
}
function mapBacklinksRows( function mapBacklinksRows(
rows: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"], rows: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"],
) { ) {

View File

@ -135,10 +135,6 @@ describe("fetchBacklinksSummaryRaw", () => {
await expect( await expect(
fetchBacklinksSummaryRaw({ fetchBacklinksSummaryRaw({
target: "example.com", target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
}), }),
).rejects.toMatchObject({ code: "BACKLINKS_NOT_ENABLED" }); ).rejects.toMatchObject({ code: "BACKLINKS_NOT_ENABLED" });
@ -171,10 +167,6 @@ describe("fetchBacklinksSummaryRaw", () => {
await expect( await expect(
fetchBacklinksSummaryRaw({ fetchBacklinksSummaryRaw({
target: "not-a-real-input.example", target: "not-a-real-input.example",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
}), }),
).rejects.toMatchObject({ code: "VALIDATION_ERROR" }); ).rejects.toMatchObject({ code: "VALIDATION_ERROR" });
}); });

View File

@ -4,24 +4,22 @@ import type {
DataforseoApiResponse, DataforseoApiResponse,
} from "@/server/lib/dataforseoCost"; } from "@/server/lib/dataforseoCost";
import { getRequiredEnvValue } from "@/server/lib/runtime-env"; import { getRequiredEnvValue } from "@/server/lib/runtime-env";
import type { BacklinksLookupInput } from "@/types/schemas/backlinks";
import { import {
backlinksHistoryItemSchema,
backlinksItemSchema, backlinksItemSchema,
backlinksSummaryItemSchema, backlinksSummaryItemSchema,
domainPageSummaryItemSchema, domainPageSummaryItemSchema,
newLostTimeseriesItemSchema,
parseFirstResult, parseFirstResult,
parseItems, parseItems,
referringDomainItemSchema, referringDomainItemSchema,
responseSchema, responseSchema,
timeseriesSummaryItemSchema,
} from "@/server/lib/dataforseoBacklinksSupport"; } from "@/server/lib/dataforseoBacklinksSupport";
import { classifyBacklinksErrorWithAccountState } from "@/server/lib/dataforseoBacklinksAccount"; import { classifyBacklinksErrorWithAccountState } from "@/server/lib/dataforseoBacklinksAccount";
export { normalizeBacklinksTarget } from "@/server/lib/dataforseoBacklinksTarget"; export { normalizeBacklinksTarget } from "@/server/lib/dataforseoBacklinksTarget";
const API_BASE = "https://api.dataforseo.com"; const API_BASE = "https://api.dataforseo.com";
export type BacklinksRequest = BacklinksLookupInput & { export type BacklinksRequest = {
target: string; target: string;
}; };
@ -29,7 +27,8 @@ export type BacklinksListRequest = BacklinksRequest & {
limit?: number; limit?: number;
}; };
export type BacklinksTimeseriesRequest = BacklinksRequest & { export type BacklinksTimeseriesRequest = {
target: string;
dateFrom: string; dateFrom: string;
dateTo: string; dateTo: string;
}; };
@ -160,10 +159,10 @@ async function postBacklinks(path: string, payload: unknown) {
function buildCommonPayload(input: BacklinksRequest) { function buildCommonPayload(input: BacklinksRequest) {
return { return {
target: input.target, target: input.target,
include_subdomains: input.includeSubdomains, include_subdomains: true,
include_indirect_links: input.includeIndirectLinks, include_indirect_links: true,
exclude_internal_backlinks: input.excludeInternalBacklinks, exclude_internal_backlinks: true,
backlinks_status_type: input.status, backlinks_status_type: "live",
rank_scale: "one_hundred", rank_scale: "one_hundred",
}; };
} }
@ -243,49 +242,21 @@ export async function fetchDomainPagesSummaryRaw(input: BacklinksListRequest) {
} satisfies DataforseoApiResponse<typeof data>; } satisfies DataforseoApiResponse<typeof data>;
} }
export async function fetchTimeseriesSummaryRaw( export async function fetchBacklinksHistoryRaw(
input: BacklinksTimeseriesRequest, input: BacklinksTimeseriesRequest,
) { ) {
const response = await postBacklinks( const response = await postBacklinks("/v3/backlinks/history/live", [
"/v3/backlinks/timeseries_summary/live",
[
{ {
...buildCommonPayload(input), target: input.target,
date_from: input.dateFrom, date_from: input.dateFrom,
date_to: input.dateTo, date_to: input.dateTo,
group_range: "month", rank_scale: "one_hundred",
}, },
], ]);
);
const data = parseItems( const data = parseItems(
"timeseries-summary-live", "backlinks-history-live",
response.results, response.results,
timeseriesSummaryItemSchema, backlinksHistoryItemSchema,
);
return {
data,
billing: response.billing,
} satisfies DataforseoApiResponse<typeof data>;
}
export async function fetchNewLostTimeseriesRaw(
input: BacklinksTimeseriesRequest,
) {
const response = await postBacklinks(
"/v3/backlinks/timeseries_new_lost_summary/live",
[
{
...buildCommonPayload(input),
date_from: input.dateFrom,
date_to: input.dateTo,
group_range: "month",
},
],
);
const data = parseItems(
"timeseries-new-lost-summary-live",
response.results,
newLostTimeseriesItemSchema,
); );
return { return {
data, data,

View File

@ -97,18 +97,12 @@ export const domainPageSummaryItemSchema = z
}) })
.passthrough(); .passthrough();
export const timeseriesSummaryItemSchema = z export const backlinksHistoryItemSchema = z
.object({ .object({
date: z.string().nullable().optional(), date: z.string().nullable().optional(),
rank: z.number().nullable().optional(), rank: z.number().nullable().optional(),
backlinks: z.number().nullable().optional(), backlinks: z.number().nullable().optional(),
referring_domains: z.number().nullable().optional(), referring_domains: z.number().nullable().optional(),
})
.passthrough();
export const newLostTimeseriesItemSchema = z
.object({
date: z.string().nullable().optional(),
new_backlinks: z.number().nullable().optional(), new_backlinks: z.number().nullable().optional(),
lost_backlinks: z.number().nullable().optional(), lost_backlinks: z.number().nullable().optional(),
new_reffering_domains: z.number().nullable().optional(), new_reffering_domains: z.number().nullable().optional(),

View File

@ -48,12 +48,11 @@ vi.mock("@/server/lib/dataforseoLighthouse", () => ({
})); }));
vi.mock("@/server/lib/dataforseoBacklinks", () => ({ vi.mock("@/server/lib/dataforseoBacklinks", () => ({
fetchBacklinksHistoryRaw: vi.fn(),
fetchBacklinksRowsRaw: vi.fn(), fetchBacklinksRowsRaw: vi.fn(),
fetchBacklinksSummaryRaw: vi.fn(), fetchBacklinksSummaryRaw: vi.fn(),
fetchDomainPagesSummaryRaw: vi.fn(), fetchDomainPagesSummaryRaw: vi.fn(),
fetchNewLostTimeseriesRaw: vi.fn(),
fetchReferringDomainsRaw: vi.fn(), fetchReferringDomainsRaw: vi.fn(),
fetchTimeseriesSummaryRaw: vi.fn(),
})); }));
import { createDataforseoClient } from "./dataforseoClient"; import { createDataforseoClient } from "./dataforseoClient";
@ -66,10 +65,6 @@ const billingCustomer = {
const backlinksInput = { const backlinksInput = {
target: "example.com", target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live" as const,
}; };
function setupHostedMode() { function setupHostedMode() {

View File

@ -22,12 +22,11 @@ import { fetchDataforseoLighthouseResultRaw } from "@/server/lib/dataforseoLight
import type { LighthouseStrategy } from "@/server/lib/dataforseoLighthousePayload"; import type { LighthouseStrategy } from "@/server/lib/dataforseoLighthousePayload";
import type { StoredLighthousePayload } from "@/server/lib/lighthouseStoredPayload"; import type { StoredLighthousePayload } from "@/server/lib/lighthouseStoredPayload";
import { import {
fetchBacklinksHistoryRaw,
fetchBacklinksRowsRaw, fetchBacklinksRowsRaw,
fetchBacklinksSummaryRaw, fetchBacklinksSummaryRaw,
fetchDomainPagesSummaryRaw, fetchDomainPagesSummaryRaw,
fetchNewLostTimeseriesRaw,
fetchReferringDomainsRaw, fetchReferringDomainsRaw,
fetchTimeseriesSummaryRaw,
type BacklinksListRequest, type BacklinksListRequest,
type BacklinksRequest, type BacklinksRequest,
type BacklinksTimeseriesRequest, type BacklinksTimeseriesRequest,
@ -62,14 +61,9 @@ export function createDataforseoClient(customer: BillingCustomerContext) {
fetchDomainPagesSummaryRaw(input), fetchDomainPagesSummaryRaw(input),
); );
}, },
timeseriesSummary(input: BacklinksTimeseriesRequest) { history(input: BacklinksTimeseriesRequest) {
return meterDataforseoCall(customer, () => return meterDataforseoCall(customer, () =>
fetchTimeseriesSummaryRaw(input), fetchBacklinksHistoryRaw(input),
);
},
newLostTimeseries(input: BacklinksTimeseriesRequest) {
return meterDataforseoCall(customer, () =>
fetchNewLostTimeseriesRaw(input),
); );
}, },
}, },

View File

@ -18,10 +18,6 @@ export const getBacklinksOverview = createServerFn({
const input = { const input = {
target: data.target, target: data.target,
scope: data.scope, scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
}; };
const profile = await BacklinksService.profileOverview(input, { const profile = await BacklinksService.profileOverview(input, {
organizationId: context.organizationId, organizationId: context.organizationId,
@ -50,10 +46,6 @@ export const getBacklinksReferringDomains = createServerFn({
const input = { const input = {
target: data.target, target: data.target,
scope: data.scope, scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
}; };
const profile = await BacklinksService.profileReferringDomains(input, { const profile = await BacklinksService.profileReferringDomains(input, {
organizationId: context.organizationId, organizationId: context.organizationId,
@ -76,10 +68,6 @@ export const getBacklinksTopPages = createServerFn({
const input = { const input = {
target: data.target, target: data.target,
scope: data.scope, scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
}; };
const profile = await BacklinksService.profileTopPages(input, { const profile = await BacklinksService.profileTopPages(input, {
organizationId: context.organizationId, organizationId: context.organizationId,

View File

@ -46,10 +46,6 @@ export const testBacklinksAccess = createServerFn({
try { try {
await dataforseo.backlinks.summary({ await dataforseo.backlinks.summary({
target: "dataforseo.com", target: "dataforseo.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
}); });
const status = buildVerifiedBacklinksAccessStatus(checkedAt); const status = buildVerifiedBacklinksAccessStatus(checkedAt);

View File

@ -1,19 +1,11 @@
import { z } from "zod"; import { z } from "zod";
export const backlinksStatusSchema = z.enum(["live", "lost", "all"]);
export const backlinksTabSchema = z.enum(["backlinks", "domains", "pages"]); export const backlinksTabSchema = z.enum(["backlinks", "domains", "pages"]);
export const backlinksTargetScopeSchema = z.enum(["domain", "page"]); export const backlinksTargetScopeSchema = z.enum(["domain", "page"]);
const booleanSearchParamSchema = z
.union([z.boolean(), z.enum(["true", "false"])])
.transform((value) => value === true || value === "true");
export const backlinksLookupSchema = z.object({ export const backlinksLookupSchema = z.object({
target: z.string().min(1, "Target is required").max(2048), target: z.string().min(1, "Target is required").max(2048),
scope: backlinksTargetScopeSchema.optional(), scope: backlinksTargetScopeSchema.optional(),
includeSubdomains: z.boolean().default(true),
includeIndirectLinks: z.boolean().default(true),
excludeInternalBacklinks: z.boolean().default(true),
status: backlinksStatusSchema.default("live"),
}); });
export const backlinksProjectSchema = z.object({ export const backlinksProjectSchema = z.object({
@ -27,14 +19,9 @@ export const backlinksOverviewInputSchema = backlinksLookupSchema.extend({
export const backlinksSearchSchema = z.object({ export const backlinksSearchSchema = z.object({
target: z.string().optional(), target: z.string().optional(),
scope: backlinksTargetScopeSchema.optional(), scope: backlinksTargetScopeSchema.optional(),
subdomains: booleanSearchParamSchema.optional(),
indirect: booleanSearchParamSchema.optional(),
excludeInternal: booleanSearchParamSchema.optional(),
status: backlinksStatusSchema.optional(),
tab: backlinksTabSchema.optional(), tab: backlinksTabSchema.optional(),
}); });
export type BacklinksLookupInput = z.infer<typeof backlinksLookupSchema>; export type BacklinksLookupInput = z.infer<typeof backlinksLookupSchema>;
export type BacklinksStatus = z.infer<typeof backlinksStatusSchema>;
export type BacklinksTab = z.infer<typeof backlinksTabSchema>; export type BacklinksTab = z.infer<typeof backlinksTabSchema>;
export type BacklinksTargetScope = z.infer<typeof backlinksTargetScopeSchema>; export type BacklinksTargetScope = z.infer<typeof backlinksTargetScopeSchema>;

View File

@ -3,17 +3,15 @@ import { backlinksSearchSchema } from "@/types/schemas/backlinks";
import { domainSearchSchema } from "@/types/schemas/domain"; import { domainSearchSchema } from "@/types/schemas/domain";
describe("search param boolean parsing", () => { describe("search param boolean parsing", () => {
it("parses explicit false values for backlinks search params", () => { it("parses backlinks search params with target and tab", () => {
const parsed = backlinksSearchSchema.parse({ const parsed = backlinksSearchSchema.parse({
subdomains: "false", target: "example.com",
indirect: "false", tab: "domains",
excludeInternal: "false",
}); });
expect(parsed).toEqual({ expect(parsed).toEqual({
subdomains: false, target: "example.com",
indirect: false, tab: "domains",
excludeInternal: false,
}); });
}); });