How to add Google Analytics to your MediaWiki
Two clean ways to track a MediaWiki with Google Analytics 4: the GTag extension, or injecting the gtag.js tag via a BeforePageDisplay hook.
MediaWiki has no analytics built in, but adding Google Analytics is a small, well-trodden job — as long as you use the current product (GA4) and one of the supported injection points. This guide covers the two clean ways: the GTag extension, and a manual hook for setups that avoid extensions. (Google Analytics 4 is the only supported version; Universal Analytics properties were retired in 2023/2024.)
Option A — the GTag extension (recommended)
GTag injects the GA4 global site tag (gtag.js) via MediaWiki's output pipeline, which means proper module handling, caching and per-page control:
- Install: place the
GTagfolder inextensions/and addwfLoadExtension( 'GTag' );toLocalSettings.php - Configure the measurement ID:
$wgGTagID = 'G-XXXXXXXXXX'; // your GA4 measurement ID
- Optional: exclude groups from tracking with the
noanalyticsright —
$wgGroupPermissions['sysop']['noanalytics'] = true;
The extension docs also cover event tracking and Consent Mode, which matters in the EU context.
Option B — manual injection via a hook
No extension needed; the BeforePageDisplay hook adds the tag to every page's head:
$wgHooks['BeforePageDisplay'][] = static function ( OutputPage $out, Skin $skin ): void {
if ( $out->getUser()->isRegistered() ) {
return; // optional: skip logged-in users / edit bots
}
$out->addHeadItem( 'google-analytics',
Html::inlineScript( '' ) // see below
);
};
The script itself is the standard GA4 snippet from Google's documentation (gtag.js with your measurement ID), embedded via Html::inlineScript() or a <script async> tag as a head item. This approach works on any MediaWiki 1.35+ and keeps everything in LocalSettings.php.
Excluding traffic sensibly
Analytics noise comes from editors and bots, not visitors. The useful filters:
- Logged-in users — editors inflate pageviews and skew content metrics; exclude them (as in the hook above) or use the
noanalyticsright with GTag - Special pages —
Special:RecentChanges, login and preferences generate traffic that means nothing; exclude by namespace or page - Bots — GA4's default bot filtering plus
$wgShowIPinHeader-unrelated exclusions: keep bots out via the 'isBot' user property if you classify them
Privacy notes
Google Analytics stores IP-address-derived geography and uses cookies. For GDPR-covered audiences: enable IP anonymization in GA4 (standard data processing settings), provide a privacy policy page describing the tracking, and consider Consent Mode (supported by GTag) to gate the tag until consent. If those requirements sound heavy, alternative self-hosted analytics — e.g. Matomo with its MediaWiki extension — avoid third-party data transfer entirely.
Verification
After enabling, check: the gtag script in the page source (view-source, search for gtag), the network tab showing google-analytics.com requests, and GA4's realtime view showing your test visit. The GTag extension page documents the remaining options, including event payloads for page views and link clicks.