Back to Community

Resolving the '__gaTracker is not defined' Error in MonsterInsights

56 threads Sep 16, 2025 PluginMonsterinsights

Content

If you're using the MonsterInsights plugin and seeing a 'ReferenceError: __gaTracker is not defined' message in your browser's console, you're not alone. This is a common issue that can break custom tracking events and cause confusion. This guide will explain why this error occurs and provide the most effective solutions to fix it.

Why Does This Error Happen?

The MonsterInsights plugin uses a custom function name, __gaTracker, instead of the standard Google Analytics ga() function. This is done to prevent conflicts with other scripts. The '__gaTracker is not defined' error typically appears for one of two reasons:

  1. You are a logged-in administrator: By default, MonsterInsights does not load the tracking code for logged-in site administrators to prevent skewing your own analytics data. If you are testing custom event code while logged in, the __gaTracker function will not be defined.
  2. Your custom code executes too early: Your custom JavaScript that calls __gaTracker() might be running before the MonsterInsights plugin has had a chance to load and define the function.

How to Fix the '__gaTracker is not defined' Error

Solution 1: Check Your User Status

The simplest fix is to test your website while logged out or using an incognito/private browser window. If the error disappears, it confirms the issue was simply because you were a logged-in administrator.

Solution 2: Wrap Your Custom Code (Recommended)

For a permanent fix, especially if your custom events need to work for all users, you must wrap your JavaScript code to check if the __gaTracker function is available before using it. This is the most robust method.

Instead of writing your event code like this:

__gaTracker('send', 'event', 'Category', 'Action');

Wrap it in a conditional check like this:

if ( typeof __gaTracker !== 'undefined' ) {
    __gaTracker('send', 'event', 'Category', 'Action');
}

For even greater reliability, you can check for the loaded property as mentioned in the support threads:

if ( __gaTracker.hasOwnProperty( "loaded" ) && __gaTracker.loaded == true ) {
    __gaTracker('send', 'event', 'Category', 'Action');
}

Solution 3: Use Script Dependencies Correctly

If you are enqueuing your own custom JavaScript file that contains __gaTracker calls, you should declare the MonsterInsights script as a dependency. This tells WordPress to load your script only after the MonsterInsights tracking code has been loaded. You can do this in your theme's functions.php file:

wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/script.js', array( 'monsterinsights-frontend-script' ), '1.0', true );

The key part is array( 'monsterinsights-frontend-script' ), which sets the dependency.

Important Note on Function Names

As confirmed in the support threads, MonsterInsights uses __gaTracker, not the standard ga() function. Always use __gaTracker in any custom code you write for use with this plugin.

By following these steps, you should be able to eliminate the '__gaTracker is not defined' error and ensure your custom tracking works seamlessly for all your visitors.

Related Support Threads Support