Google Analytics: Uncaught ReferenceError: jQuery is not defined

By Haktan Suren, PhD
In Blog
Nov 11th, 2018
0 Comments
9731 Views

Couple days ago, I noticed I have “Uncaught ReferenceError: jQuery is not defined” error in my console. And I decided to dig more to find what causing it. And quickly realized, it is because of the plugin called “Google Analytics” I’ve been using for a while.

Google Analytics: Uncaught ReferenceError: jQuery is not defined

I previously mentioned that I am using PageSpeed Insights of Google to speed up my site. Hands down it is a great Apache module but you should be extra careful using some of the functions like DeferJavaScript. As its name indicates, it is to defer JavaScripts codes until page load.

Unfortunately, most of the WordPress plugins don’t follow WordPress Coding Standards. And one of the most common mistakes that WordPress developers make is not to declare JavaScript dependencies for their own library. Google Analytics is a good (or bad!) example for that. This plugin makes `googleanalytics_get_script` call using an Ajax post. And the returned code is appended to the footer using wp_footer hook instead of using `wp_enqueue_scripts` in googleanalytics/class/Ga_Frontend.php.

Unfortunately, most of the WordPress plugins don’t follow WordPress Coding Standards

public static function add_actions() {
if ( Ga_Helper::are_features_enabled() ) {
add_action( 'wp_enqueue_scripts', 'Ga_Frontend::platform_sharethis' );
}
add_action( 'wp_footer', 'Ga_Frontend::insert_ga_script');
}

The one of the easiest way to fix this to delay the execution of the the action by adding priority flag at the end. In my case adding 999 fixed it 🙂

public static function add_actions() {
if ( Ga_Helper::are_features_enabled() ) {
add_action( 'wp_enqueue_scripts', 'Ga_Frontend::platform_sharethis' );
}
add_action( 'wp_footer', 'Ga_Frontend::insert_ga_script',999);
}

Hoping someone can get benefit from this post. Please comment down below if you have similar problem or just to show your support to encourage me writing more about similar topics.

About the Author

Haktan Suren, PhD
- Webguru, Programmer, Web developer, and Father :)

Wrap your code in <code class="{language}"></code> tags to embed!

Leave a Reply

E-mail address is required for commenting. However, it won't be visible to other users.

Loading Facebook Comments ...
Loading Disqus Comments ...