Typed contracts between search backend and frontend ad templates

One funny thing about working full-stack on ad formats inside a large codebase is realizing how much breakage comes from plain old schema drift between backend responses and frontend templates.
You have a backend team adding a new field for a multimedia creative (say, a thumbnail aspect ratio, duration badge, or secondary call-to-action), a serving layer serializing it into a payload, and reusable frontend UI templates rendering it across multiple surfaces. If the contract between those two sides is a loose dictionary of optional strings, you end up with templates full of defensive if (item.meta && item.meta.badgeText) branches and silent fallback bugs when someone renames a key.
Treating ad templates as discriminated variants
When owning both the data contract and the reusable React templates, the cleanest safeguard we found was modeling each ad format as an explicit discriminated union and validating the payload boundary once, right where the response enters the UI layer:
type BaseCreative = {
creativeId: string;
title: string;
clickUrl: string;
impressionBeaconUrl: string;
};
type MultimediaAdPayload =
| (BaseCreative & {
format: "image_card";
imageUrl: string;
aspectRatio: "16:9" | "4:3" | "1:1";
merchantLabel?: string;
})
| (BaseCreative & {
format: "video_overlay";
thumbnailUrl: string;
durationSec: number;
ctaLabel: string;
});
function renderCreative(ad: MultimediaAdPayload) {
switch (ad.format) {
case "image_card":
return <ImageAdCard ad={ad} />;
case "video_overlay":
return <VideoOverlayAd ad={ad} />;
default: {
const _exhaustive: never = ad;
return _exhaustive;
}
}
}
That never check is tiny, yet it catches missing template handlers immediately whenever a new format gets added for an A/B flight.
Keeping attribution decoupled from visual layout
The other rule that kept reusable templates sane was separating visual layout from impression/click attribution. Early on, individual ad cards each had slightly different click handlers and intersection-observer logic for firing impression beacons. Whenever a new visual variant was built for an experiment, it was way too easy to forget a tracking parameter or double-fire an impression on re-render.
Wrapping every template in a shared attribution container that owns viewport visibility thresholds (e.g., 50% visible for at least 1 second) and click-through decoration meant new UI experiments could iterate on typography, spacing, and image crops without touching billing/attribution telemetry at all. Boring architecture, fewer broken flights.
Share this article
Reactions