1

我正在使用 Driftbot,请参见此处 ,并尝试根据当前路由/url 启用/禁用它。我正在使用反应路由器。

我可以用来drift.unload()阻止它运行(在我的componentWillMount()函数中),但无法让它重新启动。

我们有一些从这里提取的代码一直在工作,除了我们现在需要修改它以便在特定页面上禁用它。

这是我们目前使用的代码:

let driftbot_function = function() {
      !function() {
        var t;
        if (t = window.driftt = window.drift = window.driftt || [], !t.init) return t.invoked ? void (window.console && console.error && console.error("Drift snippet included twice.")) : (t.invoked = !0,
        t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on"],
        t.factory = function(e) {
          return function() {
            var n;
            return n = Array.prototype.slice.call(arguments), n.unshift(e), t.push(n), t;
          };
        }, t.methods.forEach(function(e) {
          t[e] = t.factory(e);
        }), t.load = function(t) {
          var e, n, o, i;
          e = 3e5, i = Math.ceil(new Date() / e) * e, o = document.createElement("script"),
          o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + i + "/" + t + ".js",
          n = document.getElementsByTagName("script")[0], n.parentNode.insertBefore(o, n);
        });
      }();

      drift.SNIPPET_VERSION = '0.3.1';
      drift.load('****__our-key__****');
      drift.page();
    }

    driftbot_function();

这是目前在componentDidMount()我们的,app.js因为它是我们的根'/'。我也尝试过将其移到脚本标签中,index.html但结果相同。

当链接到我希望禁用它的应用程序的一部分时,唯一可以禁用它的方法是window.drift.unload(). .hide并且.off不会工作。

返回根目录时,我有命令,window.drift.load()但现在它是一个空函数,在运行该unload()函数之前它正在工作。

我还尝试在我的root.jsapp.js文件中包含上面链接的 Drift Docs 中的功能,但无济于事

有没有人有任何想法?

4

3 回答 3

1

drift.page() 不适合我

我正在使用 npm 包进行反应
https://github.com/chardmd/react-drift
漂移包含在我的顶级组件之一中

   <Drift appId='123456789' />
    <Favicon url={this.favicon} />
    <Meta
      meta={this.state.data.metaTags}
    />

设置路线规则,然后在漂移 UI 中正常设置

   componentDidUpdate (prevProps, prevState, snapshot) {

const prevLoc = prevProps.location.pathname
const newLoc = this.props.location.pathname

if (prevLoc !== newLoc) {
 window.drift.reset()
于 2019-05-15T19:06:44.223 回答
0

我可以为您的情况考虑两种选择。首先是创建一个仅用于启动/停止漂移机器人的组件。您将初始化driftbot on然后componentDidMount调用。然后不是用它来包装整个应用程序,而是只将它放在主页中,这样当你导航到另一个部分时它就会被卸载。window.drift.unload()componentWillUnmount

另一个选项,因为您提到了 React Router,将是观看location应用程序,或者使用history.listen- https://github.com/ReactTraining/history - 或检查 React Router 提供的道具 - https://reacttraining.com /react-router/web/api/location - on componentWillUpdate,然后根据当前 url 打开和关闭driftbot。

于 2018-02-14T01:15:00.207 回答
0

所以这是一个非常令人困惑的旅程,但我最终在一些工程师的帮助下解决了这个问题。

首先,在他们的破折号设置中,当您将网址“列入黑名单”时,您只需要包含关键字而不是完整网址(我的第一个错误)。

其次,将功能放在文件中的脚本index.html

第三,找到您希望禁用小部件的所有顶级路由组件,并在他们的反应文章中包含以下代码

componentWillMount() {
  drift.page(this.props.location.pathname)
}
componentWillReceiveProps(nextProps) {
  if (nextProps.location.pathname !== this.props.location.pathname) {
    drift.page(nextProps.location.pathname)
  }
}

这将基本上重新初始化小部件并.includes从您提供的 url ( this.props.location.pathname) 中针对您的设置黑名单中的关键字运行检查。然后它将根据结果禁用或启用它。

就这样,祝其他坚持这一点的人好运……

于 2018-02-14T17:40:53.260 回答