(function() {
// 1. 載入外部廣告與追蹤腳本
var scripts = [
"https://securepubads.g.doubleclick.net/tag/js/gpt.js",
"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6327508837875273",
"https://www.googletagmanager.com/gtag/js?id=G-NYKJF5H5T3",
"https://www.googletagmanager.com/gtag/js?id=UA-222890203-3"
];
scripts.forEach(function(src) {
var s = document.createElement("script");
s.src = src;
s.async = true;
document.head.appendChild(s);
});
// GA 初始化
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-NYKJF5H5T3');
gtag('config', 'UA-222890203-3');
// Punwave Mapping
var mapping = document.createElement('iframe');
mapping.src = 'https://server.punwave.com/a/mapping/foodnext_cht_mapping.html';
mapping.width = "1";
mapping.height = "1";
mapping.style.display = "none";
document.body.appendChild(mapping);
// 2. 廣告容器插入工具 (針對 sb_2 增加 min-height 以提升可視率)
function adupdate(adunit, adcode) {
var elements = document.getElementsByClassName(adunit);
if (elements.length > 0) {
elements[0].insertAdjacentHTML('afterEnd', adcode);
}
}
adupdate('foodnext_all_hp_1', '
');
adupdate('foodnext_all_hp_2', '');
adupdate('foodnext_all_sb_2', '');
// 影音版位容器:加上 min-height 確保可見性計算正確
adupdate('QA-post mb15', '');
adupdate('foodnext_all_tv_ir_1', '');
adupdate('foodnext_mobile_bottom', '');
// 3. 判定環境與路徑
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
var path = window.location.pathname;
var isHome = (path === '/' || path === '/index.html');
var isTV = path.includes('/tv');
// 4. Google Tag 廣告定義
window.googletag = window.googletag || {cmd: []};
googletag.cmd.push(function() {
var videoSlot = null; // 影音版位物件暫存
// 設定 Targeting
googletag.pubads().setTargeting('website', location.hostname);
googletag.pubads().setTargeting('url', path);
if (isMobile) {
googletag.defineSlot('/22675675822/foodnext_mobile_bottom', [320, 50], 'div-gpt-ad-1646808855263-0').addService(googletag.pubads());
if (isHome) {
googletag.defineSlot('/22675675822/foodnext_all_hp_1', [[300, 250], [336, 280]], 'div-gpt-ad-1646809005819-0').addService(googletag.pubads());
googletag.defineSlot('/22675675822/foodnext_all_hp_2', [300, 250], 'div-gpt-ad-1646809030691-0').addService(googletag.pubads());
} else {
// 移動版 sb_2 (賦予 videoSlot)
videoSlot = googletag.defineSlot('/22675675822/foodnext_all_sb_2', [[336, 280], [468, 60], [1, 1], [300, 250], [728, 90]], 'div-gpt-ad-1646808878332-0').addService(googletag.pubads());
if (isTV) {
googletag.defineSlot('/22675675822/foodnext_all_tv_ir_1', [[336, 280], [300, 250]], 'div-gpt-ad-1646808987044-0').addService(googletag.pubads());
}
}
} else {
if (isHome) {
googletag.defineSlot('/22675675822/foodnext_all_hp_1', [[300, 250], [336, 280]], 'div-gpt-ad-1646809005819-0').addService(googletag.pubads());
googletag.defineSlot('/22675675822/foodnext_all_hp_2', [[336, 280], [300, 250]], 'div-gpt-ad-1646809030691-0').addService(googletag.pubads());
} else {
googletag.defineSlot('/22675675822/foodnext_all_sb_3', [[250, 250], [300, 250]], 'div-gpt-ad-1659696645761-0').addService(googletag.pubads());
googletag.defineSlot('/21727358763/BT_a_O_1', [1, 1], 'div-gpt-ad-1724050069235-0').addService(googletag.pubads());
// 桌機版 sb_2 (賦予 videoSlot)
videoSlot = googletag.defineSlot('/22675675822/foodnext_all_sb_2', [[336, 280], [468, 60], [1, 1], [300, 250], [728, 90]], 'div-gpt-ad-1646808878332-0').addService(googletag.pubads());
if (isTV) {
googletag.defineSlot('/22675675822/foodnext_all_tv_ir_1', [[336, 280], [300, 250]], 'div-gpt-ad-1646808987044-0').addService(googletag.pubads());
}
}
}
// 全域配置:啟用 SRA 並縮小 Lazy Load 範圍以提升可視率
googletag.pubads().enableSingleRequest();
googletag.pubads().enableLazyLoad({
fetchMarginPercent: 50, // 原 100 降至 50,提升普通版位可視率
renderMarginPercent: 20, // 原 50 降至 20
mobileScaling: 1.5
});
googletag.enableServices();
// 【核心修改】影音版位一入站就強制執行 display 與 refresh,無視 Lazy Load 距離
if (videoSlot) {
googletag.display('div-gpt-ad-1646808878332-0');
googletag.pubads().refresh([videoSlot]);
}
});
// 5. 統一處理剩餘版位的顯示 (延遲 500ms)
setTimeout(function() {
googletag.cmd.push(function() {
var ads = document.querySelectorAll('div[id^="div-gpt-ad-"]');
ads.forEach(function(ad) {
// 排除剛才已經手動觸發過的影音版位 ID
if (ad.id !== 'div-gpt-ad-1646808878332-0') {
googletag.display(ad.id);
}
});
});
}, 500);
})();