您可以參照來自下層和上層 iframe 的 AppMeasurement 變數。 您必須在 AppMeasurement 資料庫存在的相同位置定義所有變數。 以下範例說明如何在 iframe 內外設定基本 AppMeasurement 變數和方法。
如果您使用 Adobe Experience Platform 中的標記,請確定追蹤器物件可在全域範圍存取。 請參閱「Adobe Analytics 擴充功能總覽」。
避免同時在上層頁面和 iframe 中包含 AppMeasurement 資料庫。 這樣做會帶來傳送多個影像要求、誇大報告及增加可計費伺服器呼叫的風險。
您可以透過 iframe 物件來存取 AppMeasurement 變數。 這些範例會使用參照 iframe 物件的兩個不同方式來設定 pageName 及呼叫 t() 方法。
// Reference AppMeasurement code that resides within an iframe and send an image request
document.getElementById('targetFrame').contentWindow.s.pageName="Page name within iframe";
document.getElementById('targetFrame').contentWindow.s.t();
// An alternate method to the above if there's only one iframe on the page
window.frames[0].contentWindow.s.pageName = "Page name within iframe";
window.frames[0].contentWindow.s.t();
您可以從 iframe 內存取上層頁面上的 AppMeasurement 變數。 此範例會使用 parent
屬性設定 pageName 及呼叫 t() 方法。
// Reference AppMeasurement code on a parent page from within an iframe and send an image request
parent.s.pageName = "Page Name on Hosted Window";
parent.s.t();
postMessage
和事件接聽程式另外,您也可以使用 postMessage
和事件接聽程式來設定變數。 此方法不需要直接參照 iframe。
// Place this code in your parent window
function listenMessage(e) {
if(e.data == "Example page view call") {
s.pageName = "Page name using postMessage";
s.t();
}
}
window.addEventListener("message", listenMessage, false);
// Place this code in the iframe
window.top.postMessage("Example page view call","https://example.com");
referrer
變數會設定為上層 URL,而不是實際參照的 URL。 您可以手動設定 referrer
變數來解決此問題。