我正在尝试实现对用户的选择进行跟踪,并在 beforeinstallprompt
被解雇和 迷你信息栏 出现。以下是MDN的一个片段,关于 BeforeInstallPromptEvent
window.addEventListener("beforeinstallprompt", function(e) {
// log the platforms provided as options in an install prompt
console.log(e.platforms); // e.g., ["web", "android", "windows"]
e.userChoice.then(function(outcome) {
console.log(outcome); // either "accepted" or "dismissed"
}, handleError);
});
这是我基于谷歌的 例子
window.addEventListener('beforeinstallprompt', (e) => {
ga(`${experimentName}.send`, 'speedlink_offer', 'show', 'true');
e.userChoice.then((choice) => {
if (choice.outcome === 'accepted') {
ga(`${experimentName}.send`, 'speedlink_offer', 'click', 'true');
} else {
ga(`${experimentName}.send`, 'speedlink_offer', 'close', 'true');
}
} );
});
但是承诺 userChoice
从未解决。为什么用户点击 "取消 "或 "添加 "按钮后无法解决?是bug还是我遗漏了什么?
PS.我发现,如果你捕捉到用户的动作(如点击),然后执行 event.prompt()
然后 userChoice
将得到解决。但它将独立于用户与 "原生 "Chrome浏览器的 小资料栏.
PPS.我的Android设备上的Chrome版本是 70.0.3538.110
我看了一下文档,也遇到了同样的问题。所以,当你调用 prompt
在保存的提示上,你实际上会得到一个 Promise<UserChoice>
哪儿 UserChoice
是
type UserChoice = {
outcome: "accepted" | "dismissed";
platform: string;
};
这不是来自文档,而是我自己的typeScript实现。所以,与其等待userChoice被解析,不如检查来自 prompt
.
下面是我的应用程序的实现。
return prompt
.prompt()
.then(userChoice => {
console.log("Prompted, result = ", userChoice);
switch (userChoice.outcome) {
case "accepted":
analytics_track(key, userChoice);
break;
case "dismissed":
analytics_track(key, userChoice);
break;
}
})
.catch(reason => {
console.error("Could not prompt", reason);
});