javascript – 设置在ReactJS项目中识别Appcues的用户
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 设置在ReactJS项目中识别Appcues的用户,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2517字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 设置在ReactJS项目中识别Appcues的用户](/upload/InfoBanner/zyjiaocheng/789/06025d5e73c44cffbae4a0fbdd9a7182.jpg)
我正在尝试在ReactJS项目和documentation状态中设置Appcues以添加以下内容以标识当前用户:
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导入的,我尝试通过窗口访问Appcues,但是当我在浏览器中访问我的项目时,它导致我的demo没有被加载:
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是否为null;然后,要么设置用户的身份(如果不为null),要么加载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的用户全部内容,希望文章能够帮你解决javascript – 设置在ReactJS项目中识别Appcues的用户所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。