最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send GA Events using onClick event - Stack Overflow

programmeradmin0浏览0评论

I am trying to send an event to Google Analytics when clicking on a specific button.

this is my button ID: (#data_243342)

This is my GA script to push on clicking on the above button.

onclick="dataLayer.push({"event": "eventGA","eventCategory" : "data1","eventAction" : "data-1-click","eventLabel" : "yes"})"

How can i pass this in a jqueryor javascript?

I am trying to send an event to Google Analytics when clicking on a specific button.

this is my button ID: (#data_243342)

This is my GA script to push on clicking on the above button.

onclick="dataLayer.push({"event": "eventGA","eventCategory" : "data1","eventAction" : "data-1-click","eventLabel" : "yes"})"

How can i pass this in a jqueryor javascript?

Share Improve this question edited Nov 18, 2017 at 6:13 Danny Fardy Jhonston Bermúdez 5,5715 gold badges26 silver badges38 bronze badges asked Oct 13, 2015 at 15:05 99problems99problems 712 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

There are many ways to do this:

With Javascript:

(function() {
  
    
  var button = document.getElementById("data_243342");
  button.addEventListener("click", function() {
    dataLayer.push({
      "event": "eventGA",
      "eventCategory": "data1",
      "eventAction": "data-1-click",
      "eventLabel": "yes"
    });
  });
  
   
})();
<button id="data_243342" type="button">Send</button>

With jQuery:

$(function() {
  
  
  $("#data_243342").on("click", function() {
    dataLayer.push({
      "event": "eventGA",
      "eventCategory": "data1",
      "eventAction": "data-1-click",
      "eventLabel": "yes"
    });
  });
  
  
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="data_243342" type="button">Send</button>

Update:

To add more button with the same function, you could try something like this:

With Javascript: By using document.getElementsByTagName to find the buttons to bind with the function.

(function() {
  
  
  onload = function() {
    var buttons = document.getElementsByTagName("button");
    for (var i = 0; i < buttons.length; i++) {
      buttons[i].addEventListener("click", function() {
        dataLayer.push({
          "event": "eventGA",
          "eventCategory": "data1",
          "eventAction": "data-1-click",
          "eventLabel": "yes"
        });
      });
    }
  };


})();
<button id="data_243342" type="button">Send 1</button>
<button id="data_243343" type="button">Send 2</button>
<button id="data_243344" type="button">Send 3</button>

With jQuery: By using the #id selectors of the buttons.

$(function() {

  // Set the buttons id in the jQuery function.
  $("#data_243342, #data_243343, #data_243344").on("click", function() {
    dataLayer.push({
      "event": "eventGA",
      "eventCategory": "data1",
      "eventAction": "data-1-click",
      "eventLabel": "yes"
    });
  });


});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="data_243342" type="button">Send 1</button>
<button id="data_243343" type="button">Send 2</button>
<button id="data_243344" type="button">Send 3</button>

Update:

Another way in jQuery is by using class selectors.

$(function() {

  // Every button with the btn-GA class can execute the function.
  $(".btn-GA").on("click", function() {
    dataLayer.push({
      "event": "eventGA",
      "eventCategory": "data1",
      "eventAction": "data-1-click",
      "eventLabel": "yes"
    });
  });


});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="data_243342" class="btn-GA" type="button">Send 1</button>
<button id="data_243343" class="btn-GA" type="button">Send 2</button>
<button id="data_243344" class="btn-GA" type="button">Send 3</button>

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论