Redux

OpenReplay 的 Redux 插件。

Redux

该插件允许你捕获 Redux 的 actions/state,并在回放会话录制时进行检查。这对于理解和修复问题非常有用。

npm i @openreplay/tracker-redux --save

初始化 tracker,然后将生成的中间件放入你的 Redux 链中。

如果你的网站是单页应用(SPA)

Section titled 如果你的网站是单页应用(SPA)
import { applyMiddleware, createStore } from 'redux';
import trackerRedux from '@openreplay/tracker-redux';
import { tracker } from '@openreplay/tracker';

tracker.configure({
  projectKey: 'YOUR_PROJECT_KEY',
  ingestPoint: "https://openreplay.mydomain.com/ingest", // when dealing with the self-hosted version of OpenReplay
});
tracker.start();
const openReplayMiddleware = tracker.use(trackerRedux(<options>)); // check list of available options below

//...
const store = createStore(
  reducer,
  applyMiddleware(openReplayMiddleware) 
);

如果你的 Web 应用是服务端渲染(SSR)

Section titled 如果你的 Web 应用是服务端渲染(SSR)

如果你的应用是 SSR,请参照以下示例。 确保 tracker 在应用进入浏览器环境后再运行。

'use client'
import { applyMiddleware, createStore } from 'redux';
import trackerRedux from '@openreplay/tracker-redux/cjs';
import { tracker } from '@openreplay/tracker';

tracker.configure({
  projectKey: 'YOUR_PROJECT_KEY',
  ingestPoint: "https://openreplay.mydomain.com/ingest", // when dealing with the self-hosted version of OpenReplay
});

const openReplayMiddleware = tracker.use(trackerRedux(<options>)); // check list of available options below

const store = createStore( // You can define/use it anywhere in response handlers
  reducer,
  applyMiddleware(openReplayMiddleware)
);

//...
function MyApp() {
  useEffect(() => { // use componentDidMount in case of React Class Component
    tracker.start();
  }, []);
  //... Use store here
}

你可以通过选项自定义中间件的行为,以对数据进行脱敏处理。

trackerRedux({
  actionFilter: action => action.type !== 'DRAW', // only actions which pass this test will be recorded
  actionTransformer: action => action.type === 'LOGIN' ? null : action,
  actionType: action => action.type // action type for search, that's the default one
  stateTransformer: state => {
    const { jwt, ..._state } = state;
    return _state;
  },
})

如果你想了解如何使用此插件在会话回放中捕获状态变化的实际示例,请查看我们这里的详细教程

在设置此插件时遇到问题?请加入我们的 Slack,或访问我们的论坛,从我们的社区获取帮助。