0

I have a button that need to trigger a div and a Google Analytics event. I can't get this to work. Right now, the div triggers fine, but the GA event does not trigger.

This is the HTML:

<a class="knappmedium knappfeedbackyes" href="#" onclick="showFeedback('yes');ga('send', 'event', 'Feedback', 'Feedback positive', 'PAGEURL');">YES</a>

Edit: This button will be on multiple pages, with the GA tacking parameters unique for each instance it is used.

Help would be very much appreciated!

Kind regards, Pete

pepe.knoch
  • 17
  • 1

3 Answers3

1

Why don't you try querySelector ?

document.querySelector('.knappmedium.knappfeedbackyes').addEventListener('click', function(){
    showFeedback('yes');
    ga('send', 'event', 'Feedback', 'Feedback positive', 'PAGEURL');
});
Zyigh
  • 425
  • 4
  • 16
1

Your code is correct, verify that:

  • You are using google analytics.js instead of gtag.js. You can follow the instructions here.
  • Verify that you have added correctly analitycs js here
  • The analitycs.js should be added before the events that you want to call.
  • Verify that you have the correct GA api key.
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37
0

It does work. Check the snippet below.

function showFeedback(){
  console.log('showFeedback');
}

function ga(){
  console.log('ga');
}
<a href="#" onclick="showFeedback();ga();">YES</a>
Vinod Bhavnani
  • 2,185
  • 1
  • 16
  • 19