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.jsscript via theBeforePageDisplayhook. - 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
- Download the extension from Extension:GTag and place the folder
GTaginextensions/. - Add the registration line to
LocalSettings.php:wfLoadExtension( 'GTag' ); - Configure your GA4 measurement ID:
$wgGTagID = 'G-XXXXXXXXXX'; // replace with your ID - Optional: disable tracking for certain groups (e.g. editors) by granting the
noanalyticsright.$wgGroupPermissions['sysop']['noanalytics'] = true; - Visit
Special:Versionto 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:
- Get the extension from Extension:Google_Analytics_Integration and copy it to
extensions/googleAnalytics. - Enable it:
wfLoadExtension( 'GoogleAnalytics' ); - Set your account ID:
$wgGoogleAnalyticsAccount = 'UA-12345678-1'; - 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.jsscript appears in the<head>. - Use Google’s Realtime report to see hits from your own browser.
- Check
Special:Versionfor loaded extensions and any error messages.
Best practices
- Prefer Extension:GTag for new wikis – it stays up‑to‑date with Google’s API.
- Never edit core skin files; use extensions or hooks to keep upgrades painless.
- Grant the
noanalyticsright to groups that should not be tracked (bots, sysops, editors). - 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.