2

我目前正在开发一个脚本,允许我检索网站上连接尝试的用户名。但主要限制是我无法编辑页面的 HTML,只能通过跟踪代码管理器添加我的脚本。

我必须解释我使用什么工具。我正在使用网络跟踪工具SnowPlow Analytics。它的工作原理与 Google Analytics 相同,带有一个数组 _snaq,(将)包含要处理的数据。问题的核心在于,即使将数据很好地添加到数组中,主脚本 (sp.js) 也没有时间完成工作并在 POST 请求之前向 CloudFront 收集器发送请求。

这是我使用的示例页面,以及捕获数据的脚本:

页:

<html>
    <head>
        <title>Sign In Example</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <!-- SnowPlow modified -->
            <script type="text/javascript">
                var _snaq=_snaq||[];
                _snaq.push(['setCollectorCf','xxxxxxxxxxxxxx']);
                _snaq.push(['trackPageView']);
                _snaq.push(['enableLinkTracking']);
                /*
                * Here is the anonymous function to include properly the snowplow.js
                * minified: d1fc8wv8zag5ca.cloudfront.net/0.11.1/sp.js
                * github : github.com/snowplow/snowplow/tree/master/1-trackers/javascript-tracker
                */
            </script>
         <!--/ SnowPlow modified -->
         <script src="js/jquery.min.js"></script>
         <script async="" type="text/javascript" src="js/getLogin.min.js"></script>
    </head>
     <body>
        <form class="formClass">
            <h2>Please sign in</h2>
            <input type="text" class="textInput" placeholder="Email address" />
            <input type="password" class="passwordInput" placeholder="Password" />
            <label class="checkbox">
                <input type="checkbox" value="remember-me" /> Remember me
            </label>
            <button class="submitButton" type="submit">Sign in</button>
        </form>
    </body>
</html>`

脚本:

var checked, cb = $('input[value="remember-me"]')[0], butt = $('button[type="submit"]')[0];
if(cb.checked)
    checked = 1.0;
else
    checked = 0.0;

function snaqPush()
{
    _snaq.push(['trackStructEvent', 'connection', 'connectionAttempt', 'email',  $('input[placeholder = "Email address"]').val(), checked]);
}

cb.addEventListener("click", function(e)
{
    if(cb.checked)
        checked = 1.0;
    else
        checked = 0.0;
}, false);

butt.addEventListener("click", function()
    {
        snaqPush(); //or directly the _snaq push([...]) here
        _snaq.push(function()
        {
            setTimeout(function(){}, 1000);
        }); //I have try the setTimeout in and out of a _snaq.push, and it stills not work
    });

注意:POST 请求在包含数据的 _snaq.push() 之后使用断点!

在此先感谢您的帮助!

4

2 回答 2

2

您应该将事件侦听器绑定到表单的提交操作,而不是按钮单击。这样,您可以在返回之前进行分析调用,true从而允许提交完成。

于 2013-03-27T18:01:08.257 回答
1

非常感谢它运行良好,但与您提出的建议相比,我只需要进行一些更改:

/*your proposal (if I'm right)*/
setTimeout(function(){}, 250);
return true;

/*the adaptation I have done for it to work*/
setTimeout(function(){myForm.submit();}, 250);
于 2013-03-28T11:04:04.450 回答