[Alpha]{class="badge informative"}

使用伺服器SDK的混合個人化

在混合個人化實作中,您的伺服器會要求個人化內容,而訪客的瀏覽器會轉譯內容。 適用於JavaScript🔗的Adobe Experience Platform Server SDK (或Server SDK)會在您的伺服器建置頁面時要求內容,而Web SDK會在瀏覽器中轉譯內容。

相較於使用者端個人化,此方法的優點如下:

  • Personalization已提早準備就緒。 伺服器會在建置頁面時要求主張,因此瀏覽器不會等到向Edge Network發出個別請求後,才能呈現內容。
  • 較少閃爍。 由於主張已在頁面中,瀏覽器可在網頁SDK載入後立即呈現。
  • 伺服器也可以使用決定。 例如,您的伺服器可以在傳送頁面之前,根據主張選擇要載入哪些產品。
NOTE
伺服器SDK為Alpha版。 在您計畫實作之前,請先檢閱支援的功能。

運作方式 how-it-works

  1. 訪客的瀏覽器會向您的伺服器要求頁面。
  2. 您的伺服器使用訪客的forRequest控制代碼上的personalization選項呼叫sendEvent。 結果包含訪客的主張。
  3. 您的伺服器會包含傳回HTML中的主張。
  4. 在瀏覽器中,網頁SDK會以applyPropositions命令呈現主張。
  5. 網頁SDK會傳送其呈現之主張的顯示事件。

先決條件 prerequisites

  • 啟用Adobe Target或Adobe Journey Optimizer的資料流。 將其​ 存取型別 ​設定為​混合驗證,因為伺服器會傳送已驗證的請求,而瀏覽器會傳送未驗證的請求。 請參閱驗證。
  • 伺服器SDK 已安裝且已設定在您的伺服器上。
  • 您頁面上的Web SDK已設定相同的orgId和datastreamId。

步驟1:要求伺服器上的主張 server

當您的伺服器處理頁面請求時,為該頁面上的表面或決定範圍要求主張。 明確地設定頁面URL,因為請求的Referer標頭在導覽期間包含​ previous ​頁面。 請參閱內容與轉送的標頭。

async function getPropositions(req) {
  const visitor = alloy.forRequest({ request: req });

  try {
    const { propositions = [] } = await visitor.sendEvent({
      xdm: {
        eventType: "decisioning.propositionFetch",
        web: {
          webPageDetails: {
            URL: `https://www.example.com${req.url}`
          }
        },
        identityMap: {
          Email: [{ id: "visitor@example.com", primary: true }]
        }
      },
      personalization: {
        surfaces: ["web://www.example.com/home#hero"],
        decisionScopes: ["daily-special"]
      }
    });
    return propositions;
  } catch (error) {
    // Render the default experience if personalization isn't available
    console.error("Failed to fetch propositions", error);
    return [];
  }
}

伺服器SDK不會轉譯主張或傳送顯示事件。 而是隻傳回值。 如果請求失敗,請傳回空白陣列,讓您的頁面仍可呈現其預設內容。

步驟2:將主張加入頁面中 page

將主張序列化至伺服器傳回的HTML。 逸出<字元,讓主張中的內容無法提早關閉<script>元素。

const propositions = await getPropositions(req);
const serialized = JSON.stringify(propositions).replace(/</g, "\\u003c");

res.end(`<!DOCTYPE html>
<html>
  <head>
    <script>window.serverPropositions = ${serialized};</script>
    <!-- Web SDK base code and library -->
  </head>
  <body>
    <div id="hero">Default hero content</div>
    <div id="daily-special">Default special</div>
  </body>
</html>`);

步驟3:在瀏覽器中轉譯主張 browser

在頁面上設定網頁SDK後,將主張傳遞至applyPropositions。 對於不包含選擇器的HTML選件,請使用metadata來告知Web SDK每個選件的放置位置。 請參閱呈現HTML選件。

alloy("applyPropositions", {
  propositions: window.serverPropositions || [],
  metadata: {
    "daily-special": { selector: "#daily-special", actionType: "setHtml" }
  }
}).then(({ propositions: renderedPropositions = [] }) => {
  if (!renderedPropositions.length) { return; }
  return alloy("sendEvent", {
    xdm: {
      eventType: "decisioning.propositionDisplay",
      _experience: {
        decisioning: {
          propositions: renderedPropositions.map(({ id, scope, scopeDetails }) => ({
            id,
            scope,
            scopeDetails
          })),
          propositionEventType: { display: 1 }
        }
      }
    }
  });
});

applyPropositions不會自行傳送顯示事件。 訪客看到的第二個sendEvent呼叫記錄。 請參閱管理顯示事件。

維持訪客的身分一致 identity

為了提供精確的報告和個人化,伺服器和瀏覽器必須以相同的方式識別訪客:

  • 主要身分: Server SDK傳送的每個事件都必須在xdm.identityMap中包含主要身分。 請參閱包含主要身分。
  • ECID: Web SDK會將訪客的ECID儲存在kndctr_<orgId>_identity Cookie中。 伺服器SDK只有在可透過您傳遞至forRequest的Cookie服務讀取該Cookie時,才會使用相同的ECID。 否則,伺服器和瀏覽器會為同一訪客使用不同的ECID。 請參閱執行個體和要求。

伺服器SDK不包含讀取訪客請求Cookie的Cookie服務。 請參閱平台服務,瞭解Cookie服務必須實作的介面。

recommendation-more-help
experience-platform-help-collection