简体   繁体   English

当浏览器处于后台时,无法在移动设备上触发使用javascript触发的像素跟踪

[英]Pixel tracking fired with javascript is not fired on mobile when browser is in background

I'm trying to find a solution to fire a pixel from Safari browser on iPhone 我正在尝试找到一种从iPhone上的 Safari浏览器触发像素的解决方案

Here is my scenario: 这是我的情况:

I have a Web Site (not an native app) that needs to fire a pixel to a third party 10 seconds after clicking submit button (currently I'm using JavaScript ). 我有一个网站(不是本机应用程序),需要在单击“提交”按钮10秒后将像素发射到第三方(当前我正在使用JavaScript)。

The problem is that when leaving to another App, the JavaScript stops running, therefore the pixel is not fired. 问题在于,当离开另一个应用程序时,JavaScript会停止运行,因此不会触发像素。

Is there any other way to trigger the pixel? 还有其他触发像素的方法吗?

There is no way to do it in mobile browsers. 在移动浏览器中无法做到这一点。 The only thing I can think of is detecting you are on a mobile and only then trigger the gif on submit. 我唯一能想到的就是检测到您在移动设备上,然后在提交时触发gif。 If it must be after 10 seconds then maybe fire it from the server, faking the data sent in the request of the gif . 如果必须在10秒之后,则可以从服务器启动它,伪造gif请求中发送的数据。

If I understand your problem, I think you have only two options: 如果我了解您的问题,我认为您只有两种选择:

  1. send your tracking pixel from the form page (assuming you control it); 从表单页面发送您的跟踪像素(假设您控制它); or 要么
  2. submit the form only after issuing the tracking pixel request (or safer yet, after getting a non-error response). 仅在发出跟踪像素请求后才提交表单(或更安全的是,在收到无错误响应后)。

The problem is that Mobile Safari puts tabs to sleep completely when they are not active. 问题在于,Mobile Safari处于非活动状态时会使选项卡完全休眠。 I believe that they go to sleep immediately (unlike a native webview, where you have two seconds to shut down your app and do any housekeeping you need to do before the app "resigns".) 我相信他们会立即入睡(与本机Webview不同,在Webview中,您有两秒钟的时间关闭应用程序并做任何必要的内务处理,然后应用程序“辞职”。)

Your question has no reasonable solution that can be implemented on the browser. 您的问题没有可以在浏览器上实现的合理解决方案。 This is due to (and you covered this already) the fact that mobile safari simply will not execute anything after a being backgrounded. 这是由于(并且您已经介绍过)以下事实:移动浏览器在后台运行后根本不会执行任何操作。 Therefore you either need a 10s loading screen, which might tank conversion, or something like the following hack/last-resort: 因此,您需要一个10秒的加载屏幕(可能会进行转换),或类似以下hack / last-resort的内容:

1) Detect if you are on mobile 1)检测您是否在手机上

2) Make a pixel request to your server instead, along with the initial submit (or even piggy-back it off of the submit) 2)向您的服务器发出像素请求,连同初始提交一起(甚至从提交中piggy带它)

3) Create a callback on your server side code, with a delay of 10 seconds, then forward the request (headers and all) to the third party. 3)在服务器端代码上创建一个回调,延迟10秒,然后将请求(标头和所有)转发给第三方。 This would have to include at the very least user-agent and IP. 这至少必须包括用户代理和IP。 Depending on your server-side language, spoofing an IP may be difficult. 根据您的服务器端语言,欺骗IP可能很困难。

Drawbacks: 缺点:

More code, and this is exceptionally brittle. 更多的代码,这非常脆弱。 I'm not saying this is a pretty solution by any means. 我并不是说这绝对是一个不错的解决方案。 Also, you will lose any cookie information the pixel would have sent to the third party since the pixel is presumably hosted on a domain that you do not control. 此外,由于该像素可能托管在您无法控制的域中,因此您将丢失该像素将发送给第三方的任何cookie信息。 If the third party's implementation requires cookie info in the request, this work around will not work. 如果第三方的实现在请求中需要Cookie信息,则无法解决此问题。 Also, any third party pixel-specific data like ETags on the pixel will not be passed. 同样,任何第三方像素特定的数据(如像素上的ETag)也不会传递。

Given that you are trying to circumvent a basic behavior (aka not executing javascript while in the background) of mobile safari, however, I don't see any options for doing this on the client with any reliability. 但是,鉴于您正试图规避移动Safari的基本行为(也就是在后台不执行javascript),因此我看不到任何在客户端上具有任何可靠性的选择。 Your other option is to set a cookie on your domain and the next time they load one of your pages, fire the pixel. 您的另一种选择是在您的域上设置一个Cookie,下次他们加载您的一个页面时,触发像素。 This presupposes that the user will re-visit your page, which depending on your users may or may not happen with any frequency. 前提是用户将重新访问您的页面,这取决于您的用户是否会以任何频率出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM