Restore ad when returning
The same advertisement that was previously displayed can be displayed when returning to the page by going back can be applied with Persist.
Persist mode does not work if the page is restored to the BF Cache of the browser.
1. Concepts
-
In the case of applying Persist, SDK stores the ad information when rendering the advertisement.
The Ad information is stored as a separate page URL + adSlotElementId.
-
When rendering an ad slot, SDK displays the saved ad if all the conditions below are met.
- enablePersistAd must be set to true.
- The page must be loaded via go back.
- An Ad stored with page URL + adSlotElementId as the separator must exist.
2. Set Persist
-
Use gladsdk.setGlobalConfig() to set Persist.
window.gladsdk.setGlobalConfig({
enablePersistAd: true,
});
3. Ad rendering with Persist
-
Use gladsdk.displayAd() to load advertisements.
- This example code demonstrates ad restoration when going back in a typical web page environment, not in an SPA (Single Page Application) environment. For ad restoration in an SPA environment, please refer to 4. Using Ad Restoration Feature Explicitly.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello NAM</title>
<script async src="https://ssl.pstatic.net/tveta/libs/glad/prod/gfp-core.js"></script>
<script>
window.gladsdk = window.gladsdk || { cmd: [] };
window.gladsdk.cmd.push(function () {
window.gladsdk.setGlobalConfig({
enablePersistAd: true,
});
});
</script>
</head>
<body>
<div id="division_1"></div>
<script>
var adSlotInfo1 = {
adUnitId: 'p_stock',
adSlotElementId: 'division_1',
};
window.gladsdk.cmd.push(function () {
var adSlot1 = window.gladsdk.defineAdSlot(adSlotInfo1);
window.gladsdk.displayAd(adSlot1);
});
</script>
</body>
</html>
4. Using Ad Restoration Feature Explicitly
-
In an SPA (Single Page Application) environment, when returning to a previous page via back navigation or when you need to restore previously loaded ads under specific service conditions, you can explicitly use the ad restoration API.
-
Use gladsdk.restoreAd() to restore previously loaded ads for ad slots created through the gladsdk global interface (
gladsdk.defineAdSlot()).- Use this when you need to attempt ad restoration for a single ad slot without using the ad deduplication feature across multiple ad slots.
- If you want to restore multiple ad slots with ad deduplication applied, please refer to Restoring Deduplicated Ads for implementation.
-
In an SPA environment, retrieve the previously created ad slot and call
gladsdk.restoreAd().// Initial ad slot creation and ad loading
var adSlotInfo = {
adUnitId: 'WEB_nw_banner-N345765840',
adSlotElementId: 'division',
};
var adSlot = window.gladsdk.defineAdSlot(adSlotInfo);
window.gladsdk.displayAd(adSlot);
// -------------------------------------------------------
// Attempt ad restoration under specific service conditions (e.g., back navigation) after initial ad loading
var adSlot = window.gladsdk.findAdSlot('division');
window.gladsdk.restoreAd(adSlot);- If the adSlot parameter is not provided, it will attempt to restore all ad slots that were previously displayed via the
gladsdk.displayAd()API. - Ad slots initially requested through individual deduplication manager are not included in the
gladsdk.restoreAd()restoration targets. Ad restoration through individual deduplication manager manages restoration targets on a per-deduplication manager basis.
// Initial ad slot creation and ad loading
var adSlot1 = window.gladsdk.defineAdSlot(adSlotInfo1);
var adSlot2 = window.gladsdk.defineAdSlot(adSlotInfo2);
adDedupManager.displayAds([adSlot1, adSlot2]); // Display ads through a specific deduplication manager
var adSlot3 = window.gladsdk.defineAdSlot(adSlotInfo3);
window.gladsdk.displayAd(adSlot3); // Display ad at the global namespace level (gladsdk)
// -------------------------------------------------------
// Attempt ad restoration under specific service conditions (e.g., back navigation) after initial ad loading
window.gladsdk.restoreAd(); // Only adSlot3 displayed at the global namespace level will be restored - If the adSlot parameter is not provided, it will attempt to restore all ad slots that were previously displayed via the