How can I insert a Google Analytics snippet into my Mediawiki wiki pages?

Viewed 1151

This is turning out to be much more challenging than I expected.

There is a Google Analytics Integration extension:

But for the Global Site Tag version of Google Analytics, it refers you to the HeadScript extension:

The problem here is the download link (http://downloads.jingames.net/mediawiki/HeadScript.zip) looks a bit dodgy and the documentation mentions a bug.

I've also reviewed this discussion on the Mediawiki site:

But it's a lot of telling and not much showing.

The Google Analytics snippet looks like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'TRACKING_ID');
</script>

I would prefer to insert it by updating a setting in LocalSettings.php config file.

Thanks!

3 Answers

I found a way to insert the tag into LocalSettings.php using the $wgHooks setting as demonstrated here:

Just replace TRACKING_ID (in both spots) with your Analytics tracking ID and paste this at the bottom of LocalSettings.php:

$wgHooks['BeforePageDisplay'][] = function( OutputPage &$out, Skin &$skin ) {
     $code = <<<HTML
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'TRACKING_ID');
</script>
HTML;

     $out->addHeadItem( 'gtag-insert', $code );
     return true;
};

This is a very old post but for anyone else who might happen upon this I have found a possible solution by combining other examples.

I have used Tom's code as an example and replace the corresponding variables within the google analytics extension.

  1. Change the hook in googleAnalytics.hooks.php line 9.

Old:

...
public static function onSkinAfterBottomScripts( Skin $skin, &$text = '' ) {
...

New:

...
public static function onBeforePageDisplay( OutputPage &$out, Skin $skin ) {
...
  1. Then replace all mentions of $text with $code, there are 5 of them on lines, 14, 24, 31, 54, and 59.

  2. Then at the end of the parser function above the return true; insert the following code on line 61.

$out->addHeadItem( 'gtag-insert', $code );
  1. Finally go into googleAnalytics.php line 52 (Bottom of file) and replace the hook call to use the new hook.

Old:

...
$wgHooks['SkinAfterBottomScripts'][] = 'GoogleAnalyticsHooks::onSkinAfterBottomScripts';
...

New:

...
$wgHooks['BeforePageDisplay'][] = 'GoogleAnalyticsHooks::onBeforePageDisplay';
...

If everything worked out you will see the googleAnalytics extension script in the head element as opposed to within the body. I am going to try and contact the creator/contributors of the extension and see if it can be change or ask why its not this way already.

There is a google analytics extension. Alternatively, the script can be added to Mediawiki:Common.js using document.write to link to the url and your site's UA-xxxxxx-x:

// GOOGLE ANALYTICS (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-xxxxxx-x', 'auto'); ga('send', 'pageview');

// END GOOGLE ANALYTICS

Related