[Alpha]{class="badge informative"}

onBeforeEventSend (服务器SDK)

本页介绍了Server SDK configure方法中的onBeforeEventSend。 对于Web SDK,请参阅onBeforeEventSend.

onBeforeEventSend回调允许您注册一个函数,该函数在Server 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