[Alpha]{class="badge informative"}

onBeforeEventSend (伺服器SDK)

此頁面說明伺服器SDK configure方法中的onBeforeEventSend。 若為Web SDK,請參閱onBeforeEventSend.

onBeforeEventSend回呼可讓您註冊在伺服器SDK傳送每個事件之前執行的函式。 用它來新增、變更或移除一個位置中每個事件的資料,或取消您不想傳送的事件,例如來自已知機器人的流量。

SDK會使用包含以下屬性的content物件來呼叫函式:

  • xdm:事件的XDM資料。 其中已包含SDK自動新增的資料,例如implementationDetails和從Referer標頭取得的頁面URL。 請參閱內容與轉送的標頭。
  • data:事件的非XDM資料,來自sendEvent的data選項。

直接變更content物件。 傳回false以取消事件。 當您取消事件時,sendEvent會以空白物件解析,而SDK不會連絡Adobe。

WARNING
如果您的函式擲回錯誤,則SDK不會傳送事件,且sendEvent會以包含您函式訊息的錯誤拒絕事件。

程式碼範例

呼叫configure時註冊onBeforeEventSend回呼。 SDK會從執行個體和每個forRequest控制代碼對每個事件呼叫相同的函式,因此函式不得依賴來自單一請求的資料。 請改用content物件。

await alloy.configure({
  datastreamId: "ebebf826-a01f-4458-8cec-ef61de241c93",
  orgId: "ADB3LETTERSANDNUMBERS@AdobeOrg",
  edgeCredentials: {
    clientId: process.env.ADOBE_CLIENT_ID,
    clientSecret: process.env.ADOBE_CLIENT_SECRET,
    scopes: process.env.ADOBE_SCOPES.split(",")
  },
  onBeforeEventSend: (content) => {
    // Cancel events that your application marked as bot traffic
    if (content.data?.isBot) {
      return false;
    }

    // Add a value to every event
    content.xdm.application ??= {};
    content.xdm.application.name = "Example storefront";

    // Remove the query string from the page URL
    const pageUrl = content.xdm.web?.webPageDetails?.URL;
    if (pageUrl) {
      content.xdm.web.webPageDetails.URL = pageUrl.split("?")[0];
    }
  }
});
recommendation-more-help
experience-platform-help-collection