How to add Google Analytics to your MediaWiki

Analytics give you insight into visitor behaviour, page popularity and the impact of new content

Why track your wiki?

Analytics give you insight into visitor behaviour, page popularity and the impact of new content. MediaWiki does not ship with a built‑in analytics tag, but the platform provides several clean ways to embed Google Analytics without hacking core files.

Choose the right method

  • Extension:GTag – the recommended solution for GA4 (global site tag). It injects the gtag.js script via the BeforePageDisplay hook.
  • Extension:Google Analytics Integration – works with the older Universal Analytics (UA‑XXXXX‑X) but is marked unmaintained.
  • Manual hook in LocalSettings.php – useful when you cannot install extensions or need a quick one‑off.

Installing Extension:GTag

  1. Download the extension from Extension:GTag and place the folder GTag in extensions/.
  2. Add the registration line to LocalSettings.php: wfLoadExtension( 'GTag' );
  3. Configure your GA4 measurement ID: $wgGTagID = 'G-XXXXXXXXXX'; // replace with your ID
  4. Optional: disable tracking for certain groups (e.g. editors) by granting the noanalytics right. $wgGroupPermissions['sysop']['noanalytics'] = true;
  5. Visit Special:Version to confirm the extension is loaded.

Using the legacy Google Analytics Integration extension

If you still rely on a Universal Analytics property, the steps are similar:

  1. Get the extension from Extension:Google_Analytics_Integration and copy it to extensions/googleAnalytics.
  2. Enable it: wfLoadExtension( 'GoogleAnalytics' );
  3. Set your account ID: $wgGoogleAnalyticsAccount = 'UA-12345678-1';
  4. To respect privacy, you can anonymise IPs and exclude namespaces: $wgGoogleAnalyticsAnonymizeIP = true; $wgGoogleAnalyticsIgnoreNsIDs = [ NS_TALK, NS_USER ];

Note that this extension is no longer maintained; the injected script uses the deprecated ga.js format.

Injecting the tag manually via a hook

The following snippet works for any MediaWiki version (≥ 1.35) and does not require an extension.

$wgHooks['BeforePageDisplay'][] = function ( OutputPage &$out, Skin &$skin ) { $gtag = <<HTML; $out->addHeadItem('gtag', $gtag); return true; };

Replace G-XXXXXXXXXX with your GA4 measurement ID. The code is added to the <head> of every page, exactly where Google expects it.

Excluding users or pages

Privacy‑conscious wikis often hide analytics from logged‑in editors. The hook approach can be extended:

if ( $out->getUser()->isAllowed('noanalytics') ) { return true; // skip insertion }

Or exclude special pages: $ignore = [ 'Userlogin', 'Userlogout', 'Preferences' ]; if ( in_array( $out->getTitle()->getPrefixedText(), $ignore ) ) { return true; }

Testing your implementation

  • Open a page, view source and verify the gtag.js script appears in the <head>.
  • Use Google’s Realtime report to see hits from your own browser.
  • Check Special:Version for loaded extensions and any error messages.

Best practices

  1. Prefer Extension:GTag for new wikis – it stays up‑to‑date with Google’s API.
  2. Never edit core skin files; use extensions or hooks to keep upgrades painless.
  3. Grant the noanalytics right to groups that should not be tracked (bots, sysops, editors).
  4. Document the tracking ID in your wiki’s configuration documentation for future admins.

With these steps you’ll have a reliable analytics layer that survives MediaWiki upgrades and respects user privacy.

Subscribe to MediaWiki Tips and Tricks

Don’t miss out on the latest articles. Sign up now to get access to the library of members-only articles.
jamie@example.com
Subscribe