此頁面說明伺服器SDK configure方法中的onBeforeEventSend。 若為Web SDK,請參閱onBeforeEventSend.
onBeforeEventSend回呼可讓您註冊在伺服器SDK傳送每個事件之前執行的函式。 用它來新增、變更或移除一個位置中每個事件的資料,或取消您不想傳送的事件,例如來自已知機器人的流量。
SDK會使用包含以下屬性的content物件來呼叫函式:
直接變更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