AdSense for Search (AFS)
This page explains how Fuse integrates Google AdSense for Search (AFS). It covers what you need to do on your site so AFS ads render alongside your search results.
What AFS is
AFS is a Google AdSense product that returns sponsored search results based on a query a user submits on your site. When a visitor searches your site, Google’s AFS library renders contextual ad blocks tied to that query. Revenue accrues to your AdSense account.
AFS is separate from standard display AdSense and from the legacy Custom Search Engine (CSE). It requires its own setup in the Google AdSense console (pubId and styleId).
What Publift configures and what your site needs to do
Your Publift account manager configures the AFS integration on the tag side:
- AFS publisher ID (
pubId) and style ID (styleId) from your AdSense console. - The URL pattern Fuse uses to extract the search query from your page URL.
- Which ad zones on the page are AFS zones, along with per-zone rendering attributes (
maxTop,width, optionalnumberandrelatedSearches).
Your site needs to:
- Place standard Fuse ad zone divs (
<div data-fuse="...">) on your search results page, in the locations where AFS ad blocks should appear. - For most setups, no JavaScript is required: Fuse extracts the query from the URL automatically.
- For Single-Page Applications (SPAs) or custom URL schemes, use the public API methods below.
Default behavior: URL-based query extraction
When AFS is enabled on your tag, Fuse runs a regular expression (configured by Publift) against window.location.href to pull the search query and optional page number out of the URL. As long as the query is present in the URL by the time the page initialises, no additional code is required.
If your site is a Multi-Page Application (MPA) where each search submits a fresh page load with the query in the URL, this default path is usually all you need.
API methods for SPA navigation and custom URL schemes
When the default URL extraction is not enough (for example, in an SPA where navigation does not trigger a fresh page load, or when the query is stored outside the URL), use one of the three public API methods below.
fusetag.pageInit({ afsPageParams })
Pass AFS params when initialising the page:
<script>
const fusetag = window.fusetag || (window.fusetag = { que: [] });
fusetag.que.push(function () {
fusetag.pageInit({
afsPageParams: {
query: 'laptop computers',
page: '1',
},
});
});
</script>
Use this on the first load of a search results page in an SPA where you already know the query at init time.
Full reference: fusetag.pageInit.
fusetag.setAfsExtractPageParams(handler)
Register a callback that returns the AFS params on demand. Fuse calls it whenever it needs to re-extract params:
<script>
const fusetag = window.fusetag || (window.fusetag = { que: [] });
fusetag.que.push(function () {
fusetag.setAfsExtractPageParams(function () {
const params = new URLSearchParams(window.location.search);
// Prefer the query value your server used to render the results page.
// If you must read it from the URL, validate and length-cap it first.
const rawQuery = params.get('q') || '';
const query = rawQuery.slice(0, 200);
return {
query,
page: params.get('page') || undefined,
};
});
});
</script>
Use this when your URL scheme is too complex for a single regex, or when the query lives in sessionStorage, a header, or another non-URL source. Where possible, use the same query value your server used to render the search results page rather than re-reading raw URL state. AFS also supports a maxTermLength option configured on the Publift side.
Full reference: fusetag.setAfsExtractPageParams.
fusetag.resetAfs(params)
Re-render AFS blocks with new params after the page has already initialised:
<script>
const fusetag = window.fusetag || (window.fusetag = { que: [] });
fusetag.que.push(function () {
fusetag.resetAfs({
query: 'laptop computers',
page: '1',
});
});
</script>
Use this on SPA route changes when the user navigates from one search results page to another without a full reload.
Full reference: fusetag.resetAfs.
Common scenarios
Multi-page site with the query in the URL
No code is required. Confirm with your account manager that the URL pattern Publift configured matches your search URLs (e.g. /search?q=laptop or /search/laptop).
Single-page application
Call fusetag.pageInit({ afsPageParams: { query, page } }) on first load, then fusetag.resetAfs({ query, page }) on each subsequent client-side route change to a search results page.
Query stored outside the URL
Register a handler with fusetag.setAfsExtractPageParams(...) that reads from your custom location (DOM element, sessionStorage, etc.) and returns { query, page }. The handler will be invoked whenever Fuse needs to extract the params.
Troubleshooting
| Symptom | Where to look |
|---|---|
| AFS ads do not render. | Confirm with Publift that AFS is enabled on your tag and at least one zone is marked as an AFS zone. Verify the page URL contains the search query (or that your custom handler returns a non-empty query). Verify the ad zone <div data-fuse="..."> element is in the DOM by the time Fuse renders AFS — this is up to two seconds after page init, or as soon as the first ad auction completes. |
| Wrong query passed to Google. | If you rely on the default URL extractor, confirm the regex Publift configured matches your URL format. Otherwise, register a setAfsExtractPageParams handler. |
| AFS ads not refreshing after SPA navigation. | Call fusetag.resetAfs({ query, page }) on every client-side route change to a search results page. |
| Personalized ads not active. | Fuse only enables personalized AFS ads when your CMP reports that personalization is allowed. Confirm with your account manager that the CMP integration is reporting correctly. |
Enable Fuse debug logs by appending ?fuse_debug=true to the page URL. AFS-related logs are prefixed with [AFS].
Coordination with your account manager
AFS setup requires the pubId, styleId, and URL pattern to be configured on the Publift side before any of the methods above will have an effect. Coordinate with your account manager when introducing AFS to a new tag, or when changing URL formats.