2013-03-27 121 views
2

我目前正在開發一個腳本,它允許我檢索網站上的連接嘗試的用戶名。但主要的限制是我無法編輯頁面的HTML,只是通過跟蹤代碼管理器添加我的腳本。如何在POST請求之前處理新的數組元素

我必須解釋我使用的是什麼工具。我正在使用網絡跟蹤工具SnowPlow Analytics。它的工作原理與Google Analytics(分析)相同,其數據_snaq(將)包含要處理的數據。 問題的核心在於,即使數據已添加到數組中,主腳本(sp.js)也沒有時間完成工作並在POST請求之前向CloudFront Collector發送請求。

這裏是我使用的示例頁面,和​​腳本來捕獲數據:

頁:

<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 
    }); 

NB:在POST請求在包含數據的_snaq.push()後緊跟着一個斷點工作!

在此先感謝您的幫助!

回答

2

您應該將事件偵聽器綁定到表單的提交操作而不是按鈕單擊。這樣,您可以在返回true之前進行分析呼叫,從而允許完成提交。

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);