A common way to use a scraper is to put it on a schedule: every morning, check this advertiser's ads, this company's job board, this app's reviews. The annoying part is that each run returns everything again, and the user has to diff it against yesterday. If the scraper charges per result, they also pay for the same items every day.
I added an "only new items" mode to most of my Apify actors. It's about 70 lines and works the same in all of them. Here's how it's built and the parts that weren't obvious.
The basic idea
Apify gives every run a default key-value store that disappears with the run. A named store outlives it and lives in the account of whoever runs the actor. So:
- Open a named store at the start of the run.
- Read a list of IDs delivered on earlier runs.
- Drop items whose ID is in that list.
- Add the IDs you actually delivered, and save.
const store = await Actor.openKeyValueStore(name); const saved = await store.getValue('SEEN_IDS'); const seen = new Set(Array.isArray(saved?.ids) ? saved.ids : []); const firstRun = !saved; On the first run the store is empty, so everything is new. The log says so explicitly, because a first monitor run that returns hundreds of items looks wrong if you expected only new ones.
Naming the store
If the user gives a store name, use it (after cleaning it to letters, digits and hyphens, with a warning if you had to change it). If not, derive the name from the input:
const basis = JSON.stringify([targets.map((t) => t.key).sort(), region, format, platform, startDate, endDate]); name = `google-ads-monitor-${createHash('sha1').update(basis).digest('hex').slice(0, 12)}`; Two schedules watching different brands then get different stores and don't mix their histories, and the same input on tomorrow's run finds its store again without anyone configuring anything. Targets are sorted first so that reordering the input doesn't create a new store.
Skip before you pay, remember after you push
Order matters here, for two reasons.
Skip as early as possible. On the Google Ads one, the listing page gives me the creative ID, and the detail page (format, regions, dates) costs another request. Seen ads are dropped after the listing, so they cost neither the detail request nor a charge to the user.
Remember only after pushData. If the run stops early because it hit the user's max cost or its timeout, the items it never pushed shouldn't be marked as seen. They come on the next run.
const fresh = items.filter((it) => !monitor.skip(idOf(it))); await Actor.pushData(fresh, 'ad-scraped'); monitor.remember(fresh.map(idOf)); Saving happens at most every 30 seconds during the run, on the SDK's persistState event, and once more at the end. A migration or crash in the middle then loses at most half a minute of history.
Stop paging early
Skipping seen items saves charges, but the run still pages through the whole listing to find the new ones. For a large advertiser that's thousands of ads to look at every day for maybe ten new ones.
On the Google Ads Transparency Center the listing turned out to be sorted by last shown date, newest first. I checked that on an 800-ad listing before relying on it. So the store also keeps, per target, when its last run started. The next run stops paging once it reaches ads last shown before that time, minus three days for Google's reporting lag. An ad older than that was either delivered last time or hasn't run since.
In a test with no cap on the number of ads, the monitor run stopped at page 48 of a listing of about 8,500 ads. A repeat run right after the first returned 0 new ads, which is what you want to see.
This only works where the listing has a reliable date order. Job boards and review feeds each need their own check before you trust it. Without that order, skipping is all you get.
Size
A set of IDs grows forever. I cap it at 150,000 IDs and drop the oldest first. A JavaScript Set keeps insertion order, so slicing the end of [...seen] keeps the most recent ones. At that size the stored JSON is still a few MB, and an ID that old is unlikely to show up again in a listing sorted by date.
A few details for users
- Where is the history stored? In their own account, as a named key-value store. They can open it, download it, or delete it to start over.
- What if I change the input? Without an explicit store name, a different input means a different store, so the first run with the new input returns everything. If you want to keep the history across input changes, set the store name yourself.
- Does a run with 0 new items fail? No. It succeeds and the status message says how many items were skipped because earlier runs delivered them. An empty dataset with no explanation looks like a broken scraper, so that message matters.
The Google Ads version has the date cutoff on top of the skip, so that's the one I'd point to: https://apify.com/pulsedata/google-ads-transparency-scraper
