Tips how to create and maintain your blog

Blogging, WordPress

How to solve “Uncaught ReferenceError: ga is not defined” error

Uncaught ReferenceError: ga is not defined

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:

[code language=”javascript”]
(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
[/code]

And this is GA code after Yoast modification:

[code language=”javascript”]
(window,document,’script’,’//www.google-analytics.com/analytics.js’,’__gaTracker’);
[/code]

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:

[code language=”javascript”]
__gaTracker( function() {
window.ga = __gaTracker;
});
[/code]

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.

Leave a Reply