Google事件跟踪使用jquery多个事件

时间:2015-06-16 21:25:19

标签: javascript jquery analytics

使用google和youtube完成新手并惊讶地发现这一点。

<script type="text/javascript">
    window.onload = function() {
        jQuery('.apply_now_green, .apply_now_red, .feat_lender img, .feat_lender h3').click(function() {
            var label = jQuery(this).parents('.col-md-3').find('h3').text();
            ga('send', 'event', 'apply now button', 'click', label);

        });
    };
</script>

<script type="text/javascript">
    window.onload = function() {
        jQuery('.more_details').click(function() {
            var label = jQuery(this).parents('.col-md-3').find('h3').text();
            ga('send', 'event', 'more details', 'click', label);

        });
    };
</script>

如何以当前格式合并两者,它不会用于跟踪两个不同事件的分析。

1 个答案:

答案 0 :(得分:0)

您可以使用HTML5 data attributes在元素本身上保存Google Analytics类别/其他数据。

在jquery中你可以这样做:

enum UIKeyboardType : Int {

    case Default // Default type for the current input method.
    case ASCIICapable // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
    case NumbersAndPunctuation // Numbers and assorted punctuation.
    case URL // A type optimized for URL entry (shows . / .com prominently).
    case NumberPad // A number pad (0-9). Suitable for PIN entry.
    case PhonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
    case NamePhonePad // A type optimized for entering a person's name or phone number.
    case EmailAddress // A type optimized for multiple email address entry (shows space @ . prominently).
    @availability(iOS, introduced=4.1)
    case DecimalPad // A number pad with a decimal point.
    @availability(iOS, introduced=5.0)
    case Twitter // A type optimized for twitter text entry (easy access to @ #)
    @availability(iOS, introduced=7.0)
    case WebSearch // A default keyboard type with URL-oriented addition (shows space . prominently).
}
$('.clickable').on('click', function() {
  var gaCategory = $(this).data('category');
  var gaLabel = $(this).data('label');
  
  alert('Category: ' + gaCategory + ', Label: ' + gaLabel);
});
.clickable {
  width: 50px;
  height: 50px;
  background: green;
  margin: 5px;
}