Recently I updated Google Analytics by Yoast plugin on my WordPress site and all my custom Google Analytics events stopped working.

I was getting “Uncaught ReferenceError: ga is not defined” error.
It took awhile to figure out the problem, but fortunately it is easy to solve it.

The problem is Yoast without any explanation changed original Google Analytics javascript code and names function __gaTracker instead of original ga.

This is how looks Google Analytics Universal code:

(window,document,'script','//www.google-analytics.com/analytics.js','ga');

And this is GA code after Yoast modification:

(window,document,'script','//www.google-analytics.com/analytics.js','__gaTracker');

There are two ways to solve it.

The first way is to rename ga into __gaTracker in your source code. It is easy to do if you don’t use ga script heavily.

Second approach is useful if you have a lot of appearances of ga in your javascript code or you don’t have access to your source code.
In such case, you need go to Google Analytics by Yoast settings page in your website dashboard

Google Analytics Yoast settings: custom code

and add this custom code:

__gaTracker( function() {
  window.ga = __gaTracker;
});

Important: for testing of second solution you must log out from your website admin panel or test it in another browser. By default Google Analytics by Yoast doesn’t track logged user and this custom code won’t be executed.

This solution applies only to WordPress users who are using Goolge Analytics by Yoast plugin.