Resolving the '__gaTracker is not defined' Error in MonsterInsights
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:
- 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
__gaTrackerfunction will not be defined. - 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
-
Counting adblock users?https://wordpress.org/support/topic/counting-adblock-users/
-
Configure user-agent transferhttps://wordpress.org/support/topic/configure-user-agent-transfer/
-
Local ga.js filehttps://wordpress.org/support/topic/local-gajs-file/
-
User-ID Tracking Featurehttps://wordpress.org/support/topic/user-id-tracking-feature/
-
Ajax pushhttps://wordpress.org/support/topic/ajax-push/
-
Enable Google optimizehttps://wordpress.org/support/topic/enable-google-optimize/
-
After v6 update should I use __gaTracker or gahttps://wordpress.org/support/topic/after-v6-update-should-i-use-__gatracker-or-ga/
-
Will My Analytics Show In The App?https://wordpress.org/support/topic/will-my-analytics-show-in-the-app/
-
6.0.4 Not saving manually entered GA codehttps://wordpress.org/support/topic/6-0-4-not-saving-manually-entered-ga-code/
-
Change __gaTracker to gahttps://wordpress.org/support/topic/change-__gatracker-to-ga/
-
Tracking Code and FREE version of GA by Yoasthttps://wordpress.org/support/topic/tracking-code-and-free-version-of-ga-by-yoast/
-
__gaTracker('set', 'forceSSL', true);https://wordpress.org/support/topic/_gatrackerset-forcessl-true/
-
I need "really custom" Custom Dimensionshttps://wordpress.org/support/topic/i-need-really-custom-custom-dimensions/
-
When I add a new page will GA for Yoast automatically add tracking codehttps://wordpress.org/support/topic/when-i-add-a-new-page-will-ga-for-yoast-automatically-add-tracking-code/
-
Add JS dependency for wp_enqueue_script()https://wordpress.org/support/topic/add-js-dependency-for-wp_enqueue_script/
-
How to add callback?https://wordpress.org/support/topic/how-to-add-callback/
-
Set _setSiteSpeedSampleRate to 100https://wordpress.org/support/topic/set-_setsitespeedsamplerate-to-100/
-
How to do custom even traction with this pluginhttps://wordpress.org/support/topic/how-to-do-custom-even-traction-with-this-plugin/
-
how to wrapping google analytics script codehttps://wordpress.org/support/topic/how-to-wrapping-google-analytics-script-code/
-
Need to migrate from ga.js to analytics.jshttps://wordpress.org/support/topic/need-to-migrate-from-ga-js-to-analytics-js/
-
Personalized code after ‘PageView’ callhttps://wordpress.org/support/topic/personalized-code-after-pageview-call/
-
Can't use with Google Analytics Content Experiments (_setDomainName = "none")https://wordpress.org/support/topic/cant-use-with-google-analytics-content-experiments-_setdomainname-none/
-
Add both domain and URL to your plugin, possible?https://wordpress.org/support/topic/add-both-domain-and-url-to-your-plugin-possible/
-
Integration With AMP Client ID APIhttps://wordpress.org/support/topic/integration-with-amp-client-id-api/
-
GA experimentshttps://wordpress.org/support/topic/ga-experiments/
-
Additional Custom gtag confighttps://wordpress.org/support/topic/additional-custom-gtag-config/
-
User-idhttps://wordpress.org/support/topic/user-id-8/
-
Why __gaTracker() instead of ga()?https://wordpress.org/support/topic/why-__gatracker-instead-of-ga/
-
Multiple Tracking Objectshttps://wordpress.org/support/topic/multiple-tracking-objects/
-
Local ga.jshttps://wordpress.org/support/topic/local-gajs/
-
Uncaught ReferenceError: __gaTracker is not definedhttps://wordpress.org/support/topic/uncaught-referenceerror-_gatracker-is-not-defined/
-
Tracker codehttps://wordpress.org/support/topic/tracker-code/
-
Facebook Retargeting Code; Issues with other Plugins?https://wordpress.org/support/topic/facebook-retargeting-code-issues-with-other-plugins/
-
Add code to footerhttps://wordpress.org/support/topic/add-code-to-footer/
-
Option to place in footerhttps://wordpress.org/support/topic/option-to-place-in-footer/
-
Multiple Tracking Codes – Running both Universal and Classichttps://wordpress.org/support/topic/multiple-tracking-codes-running-both-universal-and-classic/
-
A couple of questions, pleasehttps://wordpress.org/support/topic/a-couple-of-questions-please-2/
-
Adding Google script to specific pagehttps://wordpress.org/support/topic/adding-google-script-to-specific-page/
-
Disable Google Analytics tracking codehttps://wordpress.org/support/topic/how-to-disable-google-analytics-tracking-code-everywhere/
-
Google Tag manager support?https://wordpress.org/support/topic/google-tag-manager-support/
-
Integrating with other plugins that use different tracking variables?https://wordpress.org/support/topic/integrating-with-other-plugins-that-use-different-tracking-variables/
-
custom dimensions does not work for non-authenticated usershttps://wordpress.org/support/topic/custom-dimensions-does-not-work-for-non-authenticated-users/
-
Search Console verification failedhttps://wordpress.org/support/topic/search-console-verification-failed/
-
How to add two different Google Analytics IDhttps://wordpress.org/support/topic/how-to-add-two-different-google-analytics-id/
-
Use in combination with Google Analytics Dashboard for WPhttps://wordpress.org/support/topic/use-in-combination-with-google-analytics-dashboard-for-wp/
-
Tracking referral pathhttps://wordpress.org/support/topic/tracking-referral-path/
-
Tag Manager supporthttps://wordpress.org/support/topic/tag-manager-support/
-
How to put google analytics codehttps://wordpress.org/support/topic/how-to-put-google-analytics-code/
-
Upgrading to GTAGhttps://wordpress.org/support/topic/upgrading-to-gtag/
-
ReferenceError: __gaTracker is not definedhttps://wordpress.org/support/topic/referenceerror-__gatracker-is-not-defined/
-
Will this code work to enable autotrack?https://wordpress.org/support/topic/will-this-code-work-to-enable-autotrack/
-
Personalized code before __trackPageViewhttps://wordpress.org/support/topic/personalized-code-before-_trackpageview/
-
Adding extra scripts after the initial script endshttps://wordpress.org/support/topic/adding-extra-scripts-after-the-initial-script-ends/
-
Adjusted Bounce Rate – Settimeouthttps://wordpress.org/support/topic/adjusted-bounce-rate-settimeout/
-
Tracking Code and Universa Analyticshttps://wordpress.org/support/topic/tracking-code-and-universa-analytics/
-
Typo in Cross-Domain Tracking Documentationhttps://wordpress.org/support/topic/typo-in-cross-domain-tracking-documentation/