Repository navigation
Vanegraph RTD Provider: initial release #16189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Overview | ||
|
|
||
| ``` | ||
| Module Name: Vanegraph Rtd Provider | ||
| Module Type: Rtd Provider | ||
| Maintainer: hello@vanegraph.com | ||
| ``` | ||
|
|
||
| # Description | ||
|
|
||
| Vanegraph adds contextual signals for the current page to bid requests, with no personal data: | ||
|
|
||
| - IAB Content Taxonomy 3.1 categories in `site.content.cat` (`cattax: 9`) | ||
| - Segments in `site.content.data` with `segtax` 9 (CT 3.1) and 6 (CT 2.2) | ||
| - Vendor segments for brand-safety tier (`vg-safety-*`), risk categories (`vg-risk-*`), reader intent (`vg-intent-*`), commercial intent (`vg-commercial-*`), and the publisher's own audience segments (`vg-seg-*`) | ||
|
|
||
| Pages are classified once and served from cache. An unseen page is queued and gets labels on a later view, so the module never delays an auction waiting for a model. | ||
|
|
||
| Optionally mirrors the labels as Google Ad Manager key-values (`vg_cat`, `vg_safety`, `vg_intent`, `vg_seg`) for direct-sold line items. | ||
|
|
||
| Create a publishable key (`pk_…`, locked to your domains) with `POST https://api.vanegraph.com/v1/publishable-keys`. See https://vanegraph.com/docs#rtd. | ||
|
|
||
| # Integration | ||
|
|
||
| ``` | ||
| gulp build --modules=rtdModule,vanegraphRtdProvider,... | ||
| ``` | ||
|
|
||
| ```javascript | ||
| pbjs.setConfig({ | ||
| realTimeData: { | ||
| auctionDelay: 150, | ||
| dataProviders: [{ | ||
| name: 'vanegraph', | ||
| waitForIt: true, | ||
| params: { | ||
| publishableKey: 'pk_...', | ||
| setGptTargeting: true | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| # Parameters | ||
|
|
||
| | Name | Type | Description | Default | | ||
| | :--- | :--- | :--- | :--- | | ||
| | `publishableKey` | String | Publishable key (`pk_…`) | required | | ||
| | `url` | String | Page URL to look up | current page | | ||
| | `setGptTargeting` | Boolean | Also set GAM key-values | `true` | | ||
| | `endpoint` | String | API endpoint override | `https://api.vanegraph.com/v1/rtd` | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /** | ||
| * Vanegraph Real-Time Data submodule. | ||
| * | ||
| * Adds contextual signals for the current page to every auction, with no personal data, no cookies | ||
| * and no storage: IAB Content Taxonomy 3.1 categories (site.content.cat, cattax 9), segtax 9 and 6 | ||
| * segments, brand-safety tier, reader intent and the publisher's own Vanegraph segments in | ||
| * site.content.data. Pages are classified once and served from cache; an unseen page gets labels on | ||
| * a later view, so the module never waits for a model during an auction. | ||
| * | ||
| * @module modules/vanegraphRtdProvider | ||
| * @requires module:modules/realTimeData | ||
| */ | ||
|
|
||
| import { qualifiedAjaxBuilder } from '../src/ajax.js'; | ||
| import { MODULE_TYPE_RTD } from '../src/activities/modules.js'; | ||
| import { submodule } from '../src/hook.js'; | ||
| import { getRefererInfo } from '../src/refererDetection.js'; | ||
| import { logError, logInfo, mergeDeep } from '../src/utils.js'; | ||
| import type { StartAuctionOptions } from '../src/prebid.ts'; | ||
| import type { RTDProviderConfig, RtdProviderSpec } from './rtdModule/spec.ts'; | ||
|
|
||
| export const MODULE_NAME = 'vanegraph'; | ||
| export const DEFAULT_ENDPOINT = 'https://api.vanegraph.com/v1/rtd'; | ||
|
|
||
| export interface VanegraphRtdParams { | ||
| /** Publishable key (pk_…), locked to the publisher's domains. Safe to use in page code. */ | ||
| publishableKey: string; | ||
| /** Page URL to look up. Defaults to the current page. */ | ||
| url?: string; | ||
| /** Also set Google Ad Manager key-values (vg_cat, vg_safety, vg_intent, vg_seg). Default true. */ | ||
| setGptTargeting?: boolean; | ||
| /** API endpoint override. */ | ||
| endpoint?: string; | ||
| } | ||
|
|
||
| declare module './rtdModule/spec.ts' { | ||
| interface ProviderConfig { | ||
| vanegraph: { | ||
| params: VanegraphRtdParams; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| type VanegraphTargeting = Record<string, string | string[]>; | ||
|
|
||
| type VanegraphResponse = { | ||
| status: 'ready' | 'pending' | 'quota_exceeded'; | ||
| ortb2?: Record<string, unknown>; | ||
| targeting?: VanegraphTargeting; | ||
| }; | ||
|
|
||
| const ajax = qualifiedAjaxBuilder(MODULE_TYPE_RTD, MODULE_NAME); | ||
|
|
||
| function init(config: RTDProviderConfig<'vanegraph'>): boolean { | ||
| const key = config?.params?.publishableKey; | ||
| if (typeof key !== 'string' || key.indexOf('pk_') !== 0) { | ||
| logError(`${MODULE_NAME}: params.publishableKey (pk_...) is required`); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** The page to classify, without its fragment. */ | ||
| export function pageUrl(params?: Partial<VanegraphRtdParams>): string { | ||
| const url = params?.url || getRefererInfo().page || window.location.href; | ||
| return String(url).split('#')[0]; | ||
| } | ||
|
|
||
| /** Mirrors the labels as Google Ad Manager key-values for direct-sold line items. */ | ||
| export function setGptTargeting(targeting?: VanegraphTargeting): void { | ||
| if (!targeting) return; | ||
| const w = window as any; | ||
| const gt = (w.googletag = w.googletag || { cmd: [] }); | ||
| gt.cmd = gt.cmd || []; | ||
| gt.cmd.push(() => { | ||
| const pubads = gt.pubads(); | ||
| Object.keys(targeting).forEach((k) => pubads.setTargeting(k, targeting[k])); | ||
| }); | ||
| } | ||
|
|
||
| function getBidRequestData(reqBidsConfigObj: StartAuctionOptions, callback: () => void, config: RTDProviderConfig<'vanegraph'>): void { | ||
| const params = config?.params || ({} as VanegraphRtdParams); | ||
| const endpoint = `${params.endpoint || DEFAULT_ENDPOINT}?key=${encodeURIComponent(params.publishableKey)}&url=${encodeURIComponent(pageUrl(params))}`; | ||
| ajax( | ||
| endpoint, | ||
| { | ||
| success(response: string, xhr: { status: number }) { | ||
| try { | ||
| if (xhr && xhr.status !== 200) { | ||
| logInfo(`${MODULE_NAME}: page not classified yet`); | ||
| } else { | ||
| const data = JSON.parse(response) as VanegraphResponse; | ||
| if (data && data.status === 'ready' && data.ortb2) { | ||
| mergeDeep(reqBidsConfigObj.ortb2Fragments.global, data.ortb2); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a publisher already has Useful? React with 👍 / 👎. |
||
| if (params.setGptTargeting !== false) setGptTargeting(data.targeting); | ||
| } | ||
| } | ||
| } catch (e) { | ||
| logError(`${MODULE_NAME}: invalid response`, e); | ||
| } | ||
| callback(); | ||
| }, | ||
| error(err: unknown) { | ||
| logError(`${MODULE_NAME}: request failed`, err); | ||
| callback(); | ||
| }, | ||
| }, | ||
| null, | ||
| { method: 'GET', withCredentials: false } | ||
| ); | ||
| } | ||
|
|
||
| export const vanegraphSubmodule: RtdProviderSpec<'vanegraph'> = { | ||
| name: MODULE_NAME, | ||
| init, | ||
| getBidRequestData, | ||
| }; | ||
|
|
||
| submodule('realTimeData', vanegraphSubmodule); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { vanegraphSubmodule, pageUrl, setGptTargeting } from 'modules/vanegraphRtdProvider.js'; | ||
| import { server } from 'test/mocks/xhr.js'; | ||
|
|
||
| const KEY = 'pk_test123'; | ||
|
|
||
| function readyResponse() { | ||
| return { | ||
| status: 'ready', | ||
| url: 'https://news.example.pl/article', | ||
| ortb2: { | ||
| site: { | ||
| content: { | ||
| cattax: 9, | ||
| cat: ['22', '30'], | ||
| data: [ | ||
| { name: 'vanegraph.com', ext: { segtax: 9 }, segment: [{ id: '22' }, { id: '30' }] }, | ||
| { name: 'vanegraph.com', segment: [{ id: 'vg-safety-none' }, { id: 'vg-intent-commercial_investigation' }] } | ||
| ], | ||
| language: 'pl' | ||
| } | ||
| } | ||
| }, | ||
| targeting: { vg_cat: ['22', '30'], vg_safety: 'none', vg_intent: 'commercial_investigation' } | ||
| }; | ||
| } | ||
|
|
||
| function bidConfig() { | ||
| return { ortb2Fragments: { global: { site: { page: 'https://news.example.pl/article' } }, bidder: {} } }; | ||
| } | ||
|
|
||
| describe('vanegraphRtdProvider', function () { | ||
| describe('init', function () { | ||
| it('requires a publishable key', function () { | ||
| expect(vanegraphSubmodule.init({ params: { publishableKey: KEY } })).to.equal(true); | ||
| expect(vanegraphSubmodule.init({ params: {} })).to.equal(false); | ||
| expect(vanegraphSubmodule.init({ params: { publishableKey: 'ak_secret' } })).to.equal(false); | ||
| expect(vanegraphSubmodule.init({})).to.equal(false); | ||
| }); | ||
|
|
||
| it('is named vanegraph', function () { | ||
| expect(vanegraphSubmodule.name).to.equal('vanegraph'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('pageUrl', function () { | ||
| it('uses the configured url without its fragment', function () { | ||
| expect(pageUrl({ url: 'https://a.example.pl/x?y=1#comments' })).to.equal('https://a.example.pl/x?y=1'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getBidRequestData', function () { | ||
| let savedGoogletag; | ||
| beforeEach(function () { | ||
| savedGoogletag = window.googletag; | ||
| window.googletag = { cmd: [] }; | ||
| }); | ||
| afterEach(function () { | ||
| window.googletag = savedGoogletag; | ||
| }); | ||
|
|
||
| it('asks the API for the page with the publishable key', function () { | ||
| const callback = sinon.spy(); | ||
| vanegraphSubmodule.getBidRequestData(bidConfig(), callback, { params: { publishableKey: KEY, url: 'https://news.example.pl/article#top' } }); | ||
| const request = server.requests[0]; | ||
| expect(request.method).to.equal('GET'); | ||
| expect(request.url).to.equal(`https://api.vanegraph.com/v1/rtd?key=${KEY}&url=${encodeURIComponent('https://news.example.pl/article')}`); | ||
| expect(request.withCredentials).to.not.equal(true); | ||
| }); | ||
|
|
||
| it('merges labels into ortb2 and queues GAM key-values when the page is classified', function () { | ||
| const callback = sinon.spy(); | ||
| const reqBidsConfigObj = bidConfig(); | ||
| vanegraphSubmodule.getBidRequestData(reqBidsConfigObj, callback, { params: { publishableKey: KEY } }); | ||
| server.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(readyResponse())); | ||
|
|
||
| expect(callback.calledOnce).to.equal(true); | ||
| const site = reqBidsConfigObj.ortb2Fragments.global.site; | ||
| expect(site.page).to.equal('https://news.example.pl/article'); | ||
| expect(site.content.cattax).to.equal(9); | ||
| expect(site.content.cat).to.deep.equal(['22', '30']); | ||
| expect(site.content.data[0].ext.segtax).to.equal(9); | ||
| expect(window.googletag.cmd.length).to.equal(1); | ||
|
|
||
| const setTargeting = sinon.spy(); | ||
| window.googletag.pubads = () => ({ setTargeting }); | ||
| window.googletag.cmd[0](); | ||
| expect(setTargeting.calledWith('vg_safety', 'none')).to.equal(true); | ||
| expect(setTargeting.calledWith('vg_cat', ['22', '30'])).to.equal(true); | ||
| }); | ||
|
|
||
| it('can leave GAM targeting alone', function () { | ||
| const callback = sinon.spy(); | ||
| vanegraphSubmodule.getBidRequestData(bidConfig(), callback, { params: { publishableKey: KEY, setGptTargeting: false } }); | ||
| server.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(readyResponse())); | ||
| expect(callback.calledOnce).to.equal(true); | ||
| expect(window.googletag.cmd.length).to.equal(0); | ||
| }); | ||
|
|
||
| it('adds nothing while a new page is still being classified', function () { | ||
| const callback = sinon.spy(); | ||
| const reqBidsConfigObj = bidConfig(); | ||
| vanegraphSubmodule.getBidRequestData(reqBidsConfigObj, callback, { params: { publishableKey: KEY } }); | ||
| server.requests[0].respond(202, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'pending' })); | ||
| expect(callback.calledOnce).to.equal(true); | ||
| expect(reqBidsConfigObj.ortb2Fragments.global.site.content).to.equal(undefined); | ||
| }); | ||
|
|
||
| it('never blocks the auction on errors', function () { | ||
| const callback = sinon.spy(); | ||
| vanegraphSubmodule.getBidRequestData(bidConfig(), callback, { params: { publishableKey: KEY } }); | ||
| server.requests[0].respond(403, { 'Content-Type': 'application/json' }, '{"error":{"code":"domain_not_allowed"}}'); | ||
| expect(callback.calledOnce).to.equal(true); | ||
|
|
||
| const callback2 = sinon.spy(); | ||
| vanegraphSubmodule.getBidRequestData(bidConfig(), callback2, { params: { publishableKey: KEY } }); | ||
| server.requests[1].respond(200, { 'Content-Type': 'application/json' }, 'not json'); | ||
| expect(callback2.calledOnce).to.equal(true); | ||
| }); | ||
|
|
||
| it('uses a custom endpoint when given', function () { | ||
| vanegraphSubmodule.getBidRequestData(bidConfig(), sinon.spy(), { params: { publishableKey: KEY, endpoint: 'https://eu.api.vanegraph.com/v1/rtd' } }); | ||
| expect(server.requests[0].url.indexOf('https://eu.api.vanegraph.com/v1/rtd?key=')).to.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setGptTargeting', function () { | ||
| it('ignores empty targeting', function () { | ||
| const saved = window.googletag; | ||
| window.googletag = { cmd: [] }; | ||
| setGptTargeting(undefined); | ||
| expect(window.googletag.cmd.length).to.equal(0); | ||
| window.googletag = saved; | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
params.urlor the current page has user-specific query values, such as an account identifier or reset token, this strips only the fragment and then sends the remaining URL in theurl=query to Vanegraph; it also ignores the RTD consent data passed to the provider. Please redact sensitive query values or require consent before making the third-party request.AGENTS.md reference: AGENTS.md:L116-L116
Useful? React with 👍 / 👎.