【问题标题】:Integrating Drift chat service within a Gatsby site在 Gatsby 网站中集成 Drift 聊天服务
【发布时间】:2020-02-06 16:31:13
【问题描述】:

我正在尝试将 Drift 聊天服务功能添加到我的 Gatsby 网站。 Drift 教程声明要在标签之间添加这段代码;

<!-- Start of Async Drift Code -->
<script>
"use strict";

!function() {
  var t = window.driftt = window.drift = window.driftt || [];
  if (!t.init) {
    if (t.invoked) return 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 = Array.prototype.slice.call(arguments);
        return n.unshift(e), t.push(n), t;
      };
    }, t.methods.forEach(function(e) {
      t[e] = t.factory(e);
    }), t.load = function(t) {
      var e = 3e5, n = 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/" + n + "/" + t + ".js";
      var i = document.getElementsByTagName("script")[0];
      i.parentNode.insertBefore(o, i);
    };
  }
}();
drift.SNIPPET_VERSION = '0.3.1';
drift.load('XYZXYZXYZXYZ');
</script>
<!-- End of Async Drift Code -->

但是我不确定在哪里添加它,因为我在我的项目中找不到标签。我的项目与此相同 - https://github.com/gatsbyjs/gatsby-starter-blog

我也知道 Drift 有一个 gatsby 插件,但我也不知道如何使用它。

我的问题是,我在哪里添加这个 javascript 以便它在我的基于 gatsby 的网站中运行?

谢谢。

【问题讨论】:

  • 更新 - 我已经运行 cp .cache/default-html.js src/html.js,它允许我将自定义 HTML 添加到 html.js 文件。然而,自定义漂移代码未在此文件中编译,并且其中很大一部分显示为灰色,并且存在错误,表明需要 JS 表达式。

标签: javascript reactjs chatbot gatsby drift


【解决方案1】:

原来这条评论是我需要做的—— https://stackoverflow.com/a/54835129/5443318

希望这对其他人有所帮助。

 <script
                  dangerouslySetInnerHTML={{
                      __html: `
            "use strict";

            !function() {
              var t = window.driftt = window.drift = window.driftt || [];
              if (!t.init) {
                if (t.invoked) return 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 = Array.prototype.slice.call(arguments);
                              return n.unshift(e), t.push(n), t;
                            };
                }, t.methods.forEach(function(e) {
                                  t[e] = t.factory(e);
                }), t.load = function(t) {
                  var e = 3e5, n = 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/" + n + "/" + t + ".js";
                              var i = document.getElementsByTagName("script")[0];
                              i.parentNode.insertBefore(o, i);
                            };
                          }
                        }();
                        drift.SNIPPET_VERSION = '0.3.1';
                        drift.load('XYZXYZXYZ');
            `,}}/>

【讨论】:

  • 感谢您的回答。它真的帮助了我。为 gatsby 添加漂移的插件无法正常工作。
  • 是的,很遗憾 Gatsby 插件似乎无法正常工作。很高兴这很有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多