[英]How do I make sure all of the code in an on click function gets executed before opening a new window?
I have a function that triggers on click that sends off one tracking event to Google, and one request to my server to create a record in the database. 我有一个触发点击的功能,该功能向Google发送一个跟踪事件,向我的服务器发送一个请求,以在数据库中创建一条记录。
Everything works fine for desktop users but for some mobile users Google will record click events that my database doesn't. 一切对于台式机用户都可以正常运行,但对于某些移动用户,Google会记录我的数据库不记录的点击事件。 After some research I think I've narrowed down the problem to the fact that opening a new window via a web view causes the previous window to stop executing JavaScript, so the request to my server never gets sent.
经过一些研究,我认为我已经将问题缩小为以下事实:通过Web视图打开新窗口会导致前一个窗口停止执行JavaScript,因此对我的服务器的请求永远不会发送。 I temporarily mitigated the problem by adding a small delay to the window opening but recently I've noticed that the built-in pop-up blockers in web browsers are blocking the window so I've had to remove the delay.
我通过在窗口打开时添加一个小的延迟来暂时缓解该问题,但是最近我注意到Web浏览器中的内置弹出窗口阻止程序正在阻止窗口,因此我不得不删除该延迟。
Html: HTML:
<a ng-click="captureClick(pageUrl)" target="_blank">
JS: JS:
$scope.captureClick = function(link){
//Send Google Analytics click event
ga('send', 'event', 'click');
var linkObj = {
//Create object with click details
};
Links.save(linkObj);
$window.open(link);
};
Use Promises: 使用承诺:
new Promise(function(resolve, reject) {
// your code to your server
// after your server replied to the call
resolve();
}).then(function(data) {
// google track code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.