Map trackers by sibling patterns sharing an origin

Tracker patterns detected on the same banner that share initiator
domains are a strong indicator of the same third party. Previously the
mapping worker only checked the global third-party domain catalog, so a
tracker whose domain was not registered there fell through to the
expensive LLM identification step even when a co-located pattern was
already mapped.

Add a matchBySiblingOrigin step that finds other patterns on the same
banner sharing the same initiator domains and reuses their resolved
common third party. It prefers siblings already promoted to an org
third party (the strongest signal) and falls back to siblings carrying
only a catalog link, skipping when the siblings disagree. The step runs
before the catalog domain lookup since an already-qualified sibling is
at least as reliable as a raw domain match.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-28 23:50:52 +02:00
parent 9dfd04b449
commit 2a0523c5f2
4 changed files with 829 additions and 15 deletions

View File

@@ -944,6 +944,80 @@ WHERE
return ids, nil
}
func (tps *TrackerPatterns) LoadDistinctThirdPartyIDsByIDs(
ctx context.Context,
conn pg.Querier,
scope Scoper,
ids []gid.GID,
) ([]gid.GID, error) {
if len(ids) == 0 {
return nil, nil
}
q := `
SELECT DISTINCT third_party_id
FROM tracker_patterns
WHERE
%s
AND id = ANY(@ids)
AND third_party_id IS NOT NULL
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"ids": ids}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query distinct third party ids by pattern ids: %w", err)
}
thirdPartyIDs, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
if err != nil {
return nil, fmt.Errorf("cannot collect distinct third party ids by pattern ids: %w", err)
}
return thirdPartyIDs, nil
}
func (tps *TrackerPatterns) LoadDistinctCommonTrackerPatternIDsByIDs(
ctx context.Context,
conn pg.Querier,
scope Scoper,
ids []gid.GID,
) ([]gid.GID, error) {
if len(ids) == 0 {
return nil, nil
}
q := `
SELECT DISTINCT common_tracker_pattern_id
FROM tracker_patterns
WHERE
%s
AND id = ANY(@ids)
AND common_tracker_pattern_id IS NOT NULL
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"ids": ids}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query distinct common tracker pattern ids by pattern ids: %w", err)
}
commonPatternIDs, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
if err != nil {
return nil, fmt.Errorf("cannot collect distinct common tracker pattern ids by pattern ids: %w", err)
}
return commonPatternIDs, nil
}
func (tps *TrackerPatterns) UpdateLastMatchedAt(
ctx context.Context,
tx pg.Tx,