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

1

3 Answers 3

1

Why don't you try querySelector ?

document.querySelector('.knappmedium.knappfeedbackyes').addEventListener('click', function(){
    showFeedback('yes');
    ga('send', 'event', 'Feedback', 'Feedback positive', 'PAGEURL');
});
Sign up to request clarification or add additional context in comments.

1 Comment

This button will be on multiple pages, with the GA tacking parameters unique for each instance it is used.
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.

Comments

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>

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.