2017-02-05 7 views
0

私はインデックスページを持っていますが、私は下にサブスクライブボタンを入れるつもりです。視聴者が自分のメールアドレスを入力し、ボタンをクリックすると。この記録は、私が追跡できるGoogleアナリティクスのイベントであると考えられています。しかし、私はこれからデータを取得する際にエラーが発生しています。以下のコードは次のとおりです。GoogleアナリティクスOnclickボタンイベントトラッキング

コードは以下の通りです:

<script> 
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); 

    ga('create', 'UA-87810245-1', 'auto'); 
    ga('send', 'pageview'); 

</script> 
     <script> 

$('#sub-form').on('post', function(event) { 
    // Prevent the browser's default form submission action. 
    event.preventDefault(); 

    ga('send', 'event', { 
    eventCategory: 'subscribe', 
    eventAction: 'post', 
    eventLabel: 'subscribe sent', 
    hitCallback: function() { 
     $('sub-form').trigger('post'); 
    } 
    }); 
}); 
     </script> 

ボタンのコード:

<div class="jumbotron text-center"> 
    <p>We specialize in all flower arrangements</p> 
    <form class="form-inline" id="sub-form"> 
    <div class="input-group"> 
     <input type="email" class="form-control" size="50" placeholder="Email Address" required> 
     <div class="input-group-btn"> 
     <button type="post" class="btn btn-danger" id="post" onclick="ga('send', 'event', 'subscribe', 'post', 'subscribe sent', 10);">Subscribe</button> 
     </div> 
    </div> 
    </form> 
</div> 

しかし、結果はGoogleアナリティクスに示されていません。 enter image description here

フォームの結果は私の別のイベントです。

+0

あなたはそれをデバッグしてコードが実行されたことを確認しましたか? –

+0

これは、 "$( '#サブフォーム')の" Uncaught ReferenceError:$が定義されていません "と書かれています( 'post'、function(event){" – xhinvis

+0

jQueryライブラリスクリプトを追加する必要があります。 ' - > jQuery –

答えて

0

フォームクラスを削除した後で動作しています。私は<form action="thankyou.html" id="sub-form"> に変更しました

0

スクリプトを最初に開始してから、$( '#サブフォーム')を呼び出す必要があります。 jqueryの初期化も確認してください。

ところで、このツールを見ましたか? www.customerlabs.co - コーディングを必要とせずに5分以内で作業を完了できます。

関連する問題