I have added a custom url to the widget image in the new WP 4.9. Now I want to add an onclick event to the link.

The a tag looks like this

<a href="http://domain.com/this-is-a-page" class="" rel="" target=""></a>

And I want to have it like this

<a href="http://domain.com/this-is-a-page" onclick="ga('send', 'event', 'Sidebar Image', 'Promo Image', 'Clicked');"></a>

How can I do this?

2 Answers 2


I would use the Google Tag Manager firing options:


Old school jQuery solution:

      $('.widget_class').click(function() {
          ga('send', 'event', 'Sidebar Image', 'Promo Image', 'Clicked');

Now you just add a class name to your links.


Each widget do have their own unique IDs assigned to them. You may use jQuery to access that particular widget box with respective ID and perform the task accordingly.

Additional note: If your widget don't have any unique id then go to your functions.php and modify the code of widget declaration. Here is the WP codex link for your reference https://codex.wordpress.org/Sidebars

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.