2

現在、Web サイトでの接続試行のユーザー名を取得できるスクリプトを開発しています。ただし、主な制約は、タグ マネージャーを介してスクリプトを追加するためだけに、ページの HTML を編集できないことです。

使用するツールを説明する必要があります。私は Web 追跡ツールSnowPlow Analyticsを使用しています。これは、処理するデータを含む (予定の) 配列 _snaq を使用して、Google Analytics と同じ基準で機能します。問題の核心は、データが配列に適切に追加されたとしても、メイン スクリプト (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
    });

注意: 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 に答える