javascript - 在 ReactJS 项目中为 Appcues 设置识别用户

标签 javascript reactjs window

我正在尝试在 ReactJS 项目中设置 Appcues 并且 documentation 声明添加以下内容以识别当前用户:

  Appcues.identify({UNIQUE_USER_ID}, { // Unique identifier for current user
    name: "John Doe",   // Current user's name
    email: "john.doe@example.com", // Current user's email
    created_at: 1234567890,    // Unix timestamp of user signup date    
  });

但是,这会引发以下错误:

'Appcues' is not defined

这个错误对我来说很有意义,因为对象 Appcues 被引用但没有被导入或创建。

尝试修复:

由于 Appcues 是从 script 导入的,所以我尝试通过 window 访问 Appcues,但是当我在浏览器:

  window.Appcues.identify({UNIQUE_USER_ID}, { // Unique identifier for current user
    name: "John Doe",   // Current user's name
    email: "john.doe@example.com", // Current user's email
    created_at: 1234567890,    // Unix timestamp of user signup date    
  });

有谁知道如何在 ReactJS 项目中为 Appcues 设置用户标识?

最佳答案

创建一个递归函数来检查 window.Appcues 是否为空;然后,设置用户的身份(如果不为空)或加载 Appcues 脚本,然后递归调用自身(函数)。

识别用户

function identifyUser(userID, name, email, createdAt) {
  // if window.Appcues is not undefined or null..
  if (window.Appcues != undefined && window.Appcues != null) {
    // set up the identity of the user
    window.Appcues.identify(userID, { // Unique identifier for current user
      name: name,   // Current user's name
      email: email, // Current user's email
      created_at: createdAt,    // Unix timestamp of user signup date
    });
  // else...
  } else {
    // load the script for Appcues
    newScript("//fast.appcues.com/30716.js").then(function() {
      // then recursively call identifyUser to initialize the identity of the user
      identifyUser(userID, name, email, createdAt);
    // catch any error and print to the console
    }.bind(this)).catch(function(error) {
      console.log('identifyUser: error on loading script');
    });
  }
}

在需要时动态加载脚本

function newScript(src) {
  // create a promise for the newScript
  return new Promise(function(resolve, reject){
    // create an html script element
    var script = document.createElement('script');
    // set the source of the script element
    script.src = src;
    // set a listener when the script element finishes loading the script
    script.addEventListener('load', function () {
      // resolve if the script element loads
      resolve();
    }.bind(this));
    // set a listener when the script element faces any errors while loading
    script.addEventListener('error', function (e) {
      // reject if the script element has an error while loading the script
      reject(e);
    }.bind(this));
    // append the script element to the body
    document.body.appendChild(script);
  }.bind(this))
};

干杯!

关于javascript - 在 ReactJS 项目中为 Appcues 设置识别用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662483/

相关文章:

javascript - 如何通过倒计时器填充表格单元格

javascript - D3条形图和html数据属性

reactjs - "react-app-polyfill"在 IE11 中不起作用

html - React渲染粘贴的html作为标签,而不是字符串

javascript - 为什么以及何时我们需要在 React 中绑定(bind)函数和事件处理程序?

macos - 设置窗口级别 Swift 错误 OSX

javascript - 查找损坏的 Creep(或如何调试)

javascript - Express 生成器 - TypeError : app. set 不是一个函数

javascript - Extjs 3.4 -> 窗口不显示

macos - 如何以编程方式在 macOS SwiftUI 应用程序中打开设置窗口