Troubleshooting MonsterInsights Link Tracking Conflicts with Lightboxes and Custom Code
Content
Many WordPress users rely on MonsterInsights to simplify Google Analytics tracking. However, a common point of confusion arises when the plugin's automated tracking features conflict with existing site functionality, such as lightbox popups (e.g., Fancybox, Magnific Popup) or custom event tracking code. This guide explains why these conflicts happen and provides the most effective solutions.
Why Do These Conflicts Occur?
MonsterInsights replaces the standard Google Analytics ga() function with its own __gaTracker() function to manage its automated tracking. This is the core reason for two primary types of conflicts:
- Custom Event Tracking Breaks: If you have manually added onclick event tracking using the standard
ga('send', 'event'...)code, it will cease to function because thegaobject is no longer defined. The console will show an error: "Uncaught ReferenceError: ga is not defined". - Lightboxes and Popups Malfunction: MonsterInsights' outbound link and download tracking can interfere with scripts that power lightboxes. This often manifests as a popup opening correctly but then immediately closing or redirecting to the full-size image file. This happens because the plugin's JavaScript misidentifies a lightbox trigger as a standard outbound link or file download.
How to Resolve Custom Event Tracking Issues
If your manually implemented Google Analytics events have stopped working, the solution is to update your code to use the correct function name.
Solution 1: Update the Function Name
Replace all instances of ga('send' in your custom tracking code with __gaTracker('send'.
Old Code:<a onclick="ga('send', 'event', 'Downloads', 'Click', 'File_Name.pdf');">Download</a>
New Code:<a onclick="__gaTracker('send', 'event', 'Downloads', 'Click', 'File_Name.pdf');">Download</a>
This change ensures your custom events are pushed through the same tracker that MonsterInsights uses.
Solution 2: Use MonsterInsights' Custom Attribution System
As an alternative to writing manual JavaScript, the MonsterInsights team suggests using HTML attributes for event tracking. This method can be more stable across updates. You can find instructions for this method on their website.
How to Resolve Lightbox and Popup Conflicts
If your image popups (Fancybox, Magnific Popup, etc.) are broken, the conflict is likely caused by the file download tracking feature incorrectly processing the lightbox triggers.
Solution: Adjust File Download Settings
The most effective fix is to modify the file extensions that MonsterInsights tracks as downloads. Image file extensions (like .jpg, .png, .gif) are often included by default, which causes the plugin to treat a lightbox action as a file download.
- In your WordPress dashboard, navigate to Insights » Settings.
- Click on the Engagement tab.
- Find the setting labeled Extensions of Files to Track as Downloads.
- In this field, remove the image file extensions (e.g., jpg, jpeg, gif, png, bmp).
- Save your settings.
This tells MonsterInsights to ignore clicks on image files, allowing your lightbox script to handle them without interference. The side effect is that actual downloads of image files will no longer be tracked in Analytics, which is typically an acceptable trade-off.
Conclusion
Conflicts between MonsterInsights' automated tracking and other site elements are almost always due to the plugin's JavaScript overriding default functions or being overly aggressive in classifying clicks. By updating custom event code to use __gaTracker and fine-tuning the file download settings, you can eliminate these common issues and enjoy seamless analytics tracking alongside your site's functionality.
Related Support Threads Support
-
MonsterInsight breaks GA custom links eventhttps://wordpress.org/support/topic/monsterinsight-breaks-ga-custom-links-event/
-
Banners / Images click trackinghttps://wordpress.org/support/topic/banners-images-click-tracking/
-
stops lightbox links workinghttps://wordpress.org/support/topic/stops-lightbox-links-working/
-
Allow Linker not workinghttps://wordpress.org/support/topic/allow-linker-not-working/
-
Outbound link tracking breaks image maps with multiple linkshttps://wordpress.org/support/topic/outbound-link-tracking-breaks-image-maps-with-multiple-links/
-
Still breaks lightbox/fancybox/whateverboxhttps://wordpress.org/support/topic/still-breaks-lightbox-fancybox-whateverbox/
-
Link Attribution can’t be disabledhttps://wordpress.org/support/topic/link-attribution-cant-be-disabled/
-
Track Outbound Links in custom categoryhttps://wordpress.org/support/topic/track-outbound-links-in-custom-category/
-
Outbound links trackinghttps://wordpress.org/support/topic/outbound-links-tracking/
-
Track outbound clicks via JS breaks jQuery Magnific Popuphttps://wordpress.org/support/topic/track-outbound-clicks-via-js-breaks-jquery-magnific-popup/
-
Outbound Event Tracking parmas are wrong?https://wordpress.org/support/topic/outbound-event-tracking-parmas-are-wrong/
-
event tracking are showing image source not attributeshttps://wordpress.org/support/topic/event-tracking-are-showing-image-source-not-attributes/
-
Google Analytics and Fancyboxhttps://wordpress.org/support/topic/google-analytics-and-fancybox/
-
onclick gahttps://wordpress.org/support/topic/onclick-ga/
-
outbound linkshttps://wordpress.org/support/topic/outbound-links-9/
-
Onclick settingshttps://wordpress.org/support/topic/onclick-settings/
-
Tracking IMG outband article links – No Event Labelhttps://wordpress.org/support/topic/tracking-img-outband-article-links-no-event-label/
-
Image links are not recording on latest update to pluginhttps://wordpress.org/support/topic/image-links-are-not-recording-on-latest-update-to-plugin/
-
image tag damagedhttps://wordpress.org/support/topic/image-tag-damaged/
-
Problems with fancybox plugins.https://wordpress.org/support/topic/problems-with-fancybox-plugins/