Lightweight Google Analytics

Plugin Banner

Lightweight Google Analytics

by Andy Feliciotti

Download
Description

This simple plugin adds Google Analytics 4 (GA4) to your WordPress site using your Measurement ID. It prints Google’s standard gtag.js snippet, nothing more, with a few options for where it loads, who gets tracked, privacy and testing.

Features

  • Performance focused with no bloat and never any upsells
  • Only needs your GA4 Measurement ID (G-XXXXXXXX)
  • Load the tracking code in the header or footer
  • Exclude logged-in users by role (admins, editors and authors are excluded by default)
  • Post previews and the Customizer are never tracked
  • Option to disable Google’s advertising features (Google signals)
  • Google Consent Mode v2 support for GDPR/CCPA compliance with cookie consent plugins
  • Debug Mode for testing in GA4 DebugView
  • Optional Minimal Analytics 4 mode: a 3.5 KB inline script that reports page views, scrolls, downloads, outbound clicks and site searches without loading gtag.js at all
  • The settings page tells you whether tracking is live and why your own visits may not show up
  • Works with WP Rocket’s local Google Analytics feature
  • Filters for developers to change the gtag config, the consent defaults or when tracking is skipped

The plugin does not include any reporting. To see your statistics, open Google Analytics.

Note: Universal Analytics (UA-) was shut down by Google in 2023 and is no longer supported. If you still have a UA- ID saved, the plugin outputs nothing until you enter a GA4 Measurement ID.

For developers

Change the options passed to gtag("config", ...), for example to disable the automatic page view or set cookie flags:

add_filter( 'lightweight_ga_gtag_config', function ( $config, $measurement_id ) {
    $config['send_page_view'] = false;
    $config['cookie_flags']   = 'SameSite=None;Secure';
    return $config;
}, 10, 2 );

Change the Consent Mode defaults, for example to only deny storage for EU visitors:

add_filter( 'lightweight_ga_consent_defaults', function ( $defaults ) {
    $defaults['region'] = array( 'AT', 'BE', 'DE', 'FR', 'IE', 'NL' );
    return $defaults;
} );

Skip the tracking code for any request:

add_filter( 'lightweight_ga_disable_tracking', function ( $disabled ) {
    return $disabled || is_page( 'internal-dashboard' );
} );
  1. Upload the plugin to the wp-content/plugins directory, or install it from the Plugins screen.
  2. Activate it from the Plugins screen.
  3. Go to Settings > Lightweight Google Analytics, enter your Measurement ID and save.
  1. Plugin settings page for Lightweight Google Analytics

    Plugin settings page for Lightweight Google Analytics

How do I find my Google Analytics 4 Measurement ID?

In Google Analytics go to Admin > Data Streams, open your web stream and copy the Measurement ID. It starts with G-.

Tracking is enabled but I don’t see my own visits

By default the tracking code is not loaded for logged-in administrators, editors and authors, so your own browsing does not pollute your reports. The status message at the top of the settings page tells you whether this applies to your account. Change the Tracking Level setting to “Track all users” if you want to be counted.

What is Consent Mode and do I need it?

Google Consent Mode v2 tells Google that analytics and advertising storage are denied until the visitor consents. Enable it if you use a cookie consent plugin that updates the consent state through gtag("consent", "update", ...) and you need to comply with GDPR or similar rules. It only applies to Google’s gtag.js code.

What is the Minimal Analytics 4 option?

It replaces Google’s gtag.js with Minimal Analytics 4 by Dariusz Wieckiewicz (MIT), a small inline script that talks to Google Analytics directly. It sends page views, session start and engagement events, 90% scrolls, file downloads, outbound link clicks and site searches, plus UTM campaign attribution. It does not support Consent Mode or Google’s enhanced measurement, and it stores its client and session IDs in localStorage, so check your privacy requirements before using it.

How do I test if tracking is working?

Enable Debug Mode in the plugin settings, then in Google Analytics go to Admin > DebugView to watch hits arrive in real time. Turn Debug Mode off when you are done.

Is Universal Analytics (UA-) still supported?

No. Google stopped processing Universal Analytics data in July 2023 and deleted the properties in 2024, so version 1.6.0 removed the UA code. Enter a GA4 Measurement ID instead.

Good plugin...

By VannDigital (VDN Staff) on August 16, 2021

...so far. Hope it stays updated!!!

Significantly reduced page load time

By Ryan Robinson (ryrob) on September 30, 2020

I've been on a quest to speed up my blog for a long time, and reducing the number of requests my previous Google Analytics plugin was making (and the amount of code it was injecting onto my site) was a major tipping point for getting my pages loading lightning fast after making some other site-wide optimizations. I'm using the minimal analytics option with the plugin and it took my GA file size down to 2kb (instead of 17kb where it previously was with Monster Analytics). This plugin is incredibly simple, easy-to-use and straight forward... it does exactly what it says!

1.6.0

  • Removed Universal Analytics (UA-) support, which Google shut down in 2023. UA- IDs are no longer accepted and the IP anonymization option, which only applied to UA, is gone because GA4 always anonymizes IPs.
  • New status message on the settings page that shows whether tracking is active and whether it is disabled for your own account.
  • Post previews and the Customizer are no longer tracked.
  • New filters for developers: lightweight_ga_gtag_config, lightweight_ga_consent_defaults and lightweight_ga_disable_tracking.
  • Updated Minimal Analytics 4 to the current release, which adds session handling, engagement time, UTM attribution and outbound click tracking, and keeps all of its variables out of the global scope. Its source and license now ship alongside the minified file, and Debug Mode applies to it too.
  • Tracking code is printed through WordPress’s script tag functions, so Content Security Policy nonces added with the wp_inline_script_attributes and wp_script_attributes filters are applied.
  • The Measurement ID is validated and escaped on output, including IDs saved before validation was added in 1.5.0.
  • Text domain corrected to lightweight-google-analytics so translations from WordPress.org load.
  • Deleting the plugin now removes its settings.
  • Tested up to WordPress 7.1. Requires WordPress 5.7 or newer.

1.5.0

  • Added Google Consent Mode v2 support for GDPR/CCPA compliant tracking.
  • Added Debug Mode toggle for GA4 testing via DebugView.
  • Updated messaging to prioritize Google Analytics 4 (GA4) over deprecated Universal Analytics.
  • Added admin notice warning for sites still using Universal Analytics (UA-) tracking IDs.
  • Added note clarifying that IP anonymization is automatic in GA4.
  • Added input sanitization and validation for tracking IDs and all settings.
  • Code cleanup and optimization.
  • Tested up to WordPress 6.9.

1.4.3

  • Tested up to WordPress 6.5.
  • Upgraded minimal analytics tracking scripts.

1.4.2

  • Tested up to WordPress 6.3.

1.4.1

  • PHP Error bugfix.

1.4

  • Included MinimalAnalytics4 by idarek on GitHub. This allows you to use a gtag tracking code for page views & site search without loading Google’s bloated tracking library.
  • Removed jQuery references in the admin for vanilla JS.
  • Tested up to WordPress 6.0.

1.3

  • Tested up to WordPress 5.8.
  • Added link to analytics.google.com in settings page

1.2.2

  • PHP Warning bug fix.

1.2.1

  • Tested up to WordPress 5.7.

1.2.0

  • Added the ability to use Global site tag measurement ID (gtag.js) as well as Universal Analytics tracking ID (analytics.js).

1.1.1

  • Admin page tweaks and added a setup prompt at first install

1.1.0

  • Added option to use MinimalAnalytics.com’s tracking code

1.0.0

  • Initial Release.
Back to top