refreshAds
Overview
Section titled “Overview”The refreshAds method refreshes ads on the page, either all ads or specific placements. This is useful for single-page applications (SPAs) when the page content changes without a full page reload.
Example
Section titled “Example”// Refresh all adswindow.htag.api('1').refreshAds();
// Refresh specific placementswindow.htag.api('1').refreshAds({ elementIds: ['div1', 'div2'] });
// Force refresh even if URL hasn't changedwindow.htag.api('1').refreshAds({ force: true });
// Mark as page transition even when refreshing specific elementswindow.htag.api('1').refreshAds({ elementIds: ['div1'], pageTransition: true });Parameters
Section titled “Parameters”- options (optional): An object with the following properties:
- elementIds (array, optional): An array of element IDs to refresh. If not provided, all placements will be refreshed.
- force (boolean, optional): If true, ads will be refreshed even if the URL hasn’t changed.
- pageTransition (boolean, optional): If true, the refresh is considered a page transition, which affects effect cleanup.
Return Value
Section titled “Return Value”Returns the Htag API object, allowing for method chaining.
URL Check
Section titled “URL Check”By default, Htag checks if the current URL is the same as the previous one. If the URL hasn’t changed, ads won’t be refreshed unless you set the force option to true. This prevents unnecessary ad refreshes and improves performance.
Page Transition
Section titled “Page Transition”A refresh is considered a page transition in two cases:
- When no specific elements are specified (
elementIdsis not provided) - When the
pageTransitionoption is set totrue
Page transitions trigger additional behaviors like cleaning up effects with the htag:refresh-page tag.
Side Effects
Section titled “Side Effects”When you call refreshAds, several things happen:
-
Effects Cleanup:
htag:refresh-pagetag (for page transitions)ad:{elementId}tags for each refreshed placementraw-script:alltag
-
Raw JS Execution: Scripts marked with
runOnce: falseare executed again. -
Keywords Update: New keywords are fetched from the Semantiq service for the new URL.
-
Additional Refetching:
- Priority creative IDs are refetched
- Geo information is refetched