手動呈現主張

當您需要完全控制主張專案的套用方式時,請使用此模式。 例如,您是從JSON內容構成複雜的UI,或想在轉譯之前套用自訂商業規則。

1.請求主張

alloy("sendEvent", {
  personalization: {
    decisionScopes: ["salutation", "discount"]
  },
  xdm: { }
}).then(({ propositions = [] }) => {
  // Render in the next step
});

如需詳細資訊,請參閱personalization命令中的sendEvent物件。

2.轉譯主張專案(範例)

手動呈現主張時,它們可以採用許多不同的表單或形狀。 以下是依範圍尋找主張,然後套用找到的第一個HTML內容專案的最小範例。

function findPropositionByScope(propositions, scope) {
  return propositions.find((p) => p.scope === scope);
}

function renderHtmlInto(selector, html) {
  const el = document.querySelector(selector);
  if (!el) return;
  el.innerHTML = html;
}

alloy("sendEvent", {
  personalization: { decisionScopes: ["discount"] },
  xdm: { }
}).then(({ propositions = [] }) => {
  const discount = findPropositionByScope(propositions, "discount");
  if (!discount) return { propositions, rendered: [] };

  const htmlItem = discount.items?.find(
    (i) => i.schema === "https://ns.adobe.com/personalization/html-content-item"
  );

  if (!htmlItem) return { propositions, rendered: [] };

  renderHtmlInto("#daily-special", htmlItem.data.content);
  return { propositions, rendered: [discount] };
});
IMPORTANT
如果您轉譯HTML,請確保其對於您的環境是安全的。 將內容呈現視為安全性界限(淨化、信任的來源和CSP考量事項)。

3.記錄所呈現內容的顯示事件

對於手動呈現的主張,顯示事件會透過參考呈現的主張的sendEvent呼叫進行記錄。

function toDisplayPayload(propositions) {
  return propositions.map((p) => ({
    id: p.id,
    scope: p.scope,
    scopeDetails: p.scopeDetails
  }));
}

// Example: record display for the propositions you actually rendered.
alloy("sendEvent", {
  xdm: {
    _experience: {
      decisioning: {
        propositions: toDisplayPayload(renderedPropositions),
        propositionEventType: { display: 1 }
      }
    }
  }
});

如需詳細資訊,請參閱管理顯示事件

4.重新呈現

當UI變更需要重新呈現時,請針對您快取的主張資料重新執行手動呈現邏輯(或視需要再次擷取)。 如果您需要記錄重新呈現案例的顯示,請參閱管理顯示事件

recommendation-more-help
1ae86b30-e55e-49c1-ab11-9d0356a5f3e1