设置通知侦听器 ======= 本主题介绍如何为云眼灰度发布(特性标帜)AB实验 JavaScript(浏览器)SDK 设置和删除通知侦听器。 [建议修改](/experimentation/v4.0.0-full-stack/edit/set-up-notification-listener-javascript) 通知侦听器触发您在 SDK 中触发某些操作时定义的回调函数。 最常见的用例是将所有灰度发布(特性标帜)决策的流发送到分析提供商或内部数据仓库,以将其与您拥有的有关用户的其他数据联接。 通知侦听器类型 [](#notification-listener-types) ------------------------------------------- 有关通知侦听器类型和用例的更多信息,请参阅[通知侦听器](/experimentation/v4.0.0-full-stack/docs/notification-listeners)。 有关代码示例,请参阅以下部分。 添加和删除所有通知侦听器 [](#add-and-remove-all-notification-listeners) -------------------------------------------------------------- 下面的示例代码演示如何添加侦听器、删除侦听器、删除特定类型的所有侦听器(例如所有决策侦听器)以及删除所有侦听器。 JavaScript `import { enums } from '@eyeofcloud/eyeofcloud-sdk'; // Remove Notification Listener eyeofcloudClient.notificationCenter.removeNotificationListener(notificationId); // Remove All Notification Listeners eyeofcloudClient.notificationCenter.clearAllNotificationListeners(); // Remove all Notification Listeners of a certain type eyeofcloudClient.notificationCenter.clearNotificationListeners(enums.NOTIFICATION_TYPES.DECISION);` 设置每种类型的通知侦听器 [](#set-up-each-type-of-notification-listener) -------------------------------------------------------------- 下面的示例代码演示如何设置每种类型的通知侦听器。 JavaScript `import { enums } from '@eyeofcloud/eyeofcloud-sdk'; // import your third-party analytics integration here /////////////////////////////////////////// // SET UP DECISION NOTIFICATION LISTENER // /////////////////////////////////////////// const onDecision = ({ type, userId, attributes, decisionInfo }) => { // Add a DECISION Notification Listener for type FLAG if (type === 'flag') { // Access information about feature, for example, key and enabled status console.log(decisionInfo['flagKey']); console.log(decisionInfo['enabled']); console.log(decisionInfo['decisionEventDispatched']); // Send data to analytics provider here } } const notificationId = eyeofcloudClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.DECISION, onDecision); //////////////////////////////////////////// // SET UP LOG EVENT NOTIFICATION LISTENER // //////////////////////////////////////////// const onLogEvent = (logEvent) => { // process the logEvent object here (send to analytics provider, audit/inspect data) } eyeofcloudClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.LOG_EVENT, onLogEvent); //////////////////////////////////////////////////// // SET UP EYEOFCLOUD CONFIG NOTIFICATION LISTENER // //////////////////////////////////////////////////// // listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data const onConfigUpdateListener = () => { const config = eyeofcloudClient.getEyeofcloudConfig() } eyeofcloudClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.EYEOFCLOUD_CONFIG_UPDATE, onConfigUpdateListener); //////////////////////////////////////// // SET UP TRACK NOTIFICATION LISTENER // //////////////////////////////////////// const onTrack = (event) => { // process the event here (send to analytics provider, audit/inspect data) } eyeofcloudClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.TRACK, onTrack);`
About 2 min