Extension:Google Tag Manager

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
GoogleTagManager
Release status: unmaintained
Implementation User interface
Description Adds Google Tag Manager container for tracking, e.g. with Google Universal Analytics
Author(s) Felix Kaiser (FelixKaisertalk)
Latest version 0.1 (2014-01-20)
Database changes No
License GNU General Public License 2.0 or later
Download See the code section
Parameters
  • $wgGoogleTagManagerContainerID
Hooks used
SkinAfterBottomScripts
Translate the Google Tag Manager extension if it is available at translatewiki.net
Check usage and version matrix.

The GoogleTagManager extension integrates a Google Tag Manager container, e.g. containing a Google Universal Analytics tracking code, into each mediawiki page.

This extension is based on the extension Google Analytics Integration.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/GoogleTagManager/GoogleTagManager.php";
    // Replace GTM-XXXXXX with your Google Tag Manager container ID
    $wgGoogleTagManagerContainerID = "GTM-XXXXXX";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Known issues[edit]

The container code is integrated before the closing body tag. The official documentation in contrast proposed integration right after the opening body tag. General tracking however works fine.

Code[edit]

GoogleTagManager.php
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point.\n' );
}

$wgExtensionCredits['other'][] = array(
	'path'           => __FILE__,
	'name'           => 'Google Tag Manager Integration',
	'version'        => '0.1',
	'author'         => 'Felix Kaiser',
	'descriptionmsg' => 'googletagmanager-desc',
	'url'            => 'https://www.mediawiki.org/wiki/Extension:Google_Tag_Manager'
);

$wgExtensionMessagesFiles['GoogleTagManager'] = dirname(__FILE__) . '/GoogleTagManager.i18n.php';

$wgHooks['SkinAfterBottomScripts'][]  = 'efGoogleTagManagerHookText';

$wgGoogleTagManagerContainerID = "";

// These options are deprecated.
// You should add the "noanalytics" right to the group
// Ex: $wgGroupPermissions["sysop"]["noanalytics"] = true;
$wgGoogleTagManagerIgnoreSysops = true;
$wgGoogleTagManagerIgnoreBots = true;

function efGoogleTagManagerHookText( $skin, &$text='' ) {
	$text .= efAddGoogleTagManager();
	return true;
}

function efAddGoogleTagManager() {
	global $wgGoogleTagManagerContainerID, $wgGoogleTagManagerIgnoreSysops, $wgGoogleTagManagerIgnoreBots, $wgUser;
	if ( $wgUser->isAllowed( 'noanalytics' ) ||
		 $wgGoogleTagManagerIgnoreBots && $wgUser->isAllowed( 'bot' ) ||
		 $wgGoogleTagManagerIgnoreSysops && $wgUser->isAllowed( 'protect' ) ) {
		return "\n<!-- Google Tag Manager tracking is disabled for this user -->";
	}
	if ( $wgGoogleTagManagerContainerID === '' ) {
		return "\n<!-- Set \$wgGoogleTagManagerContainerID to your cointainer ID provided by Google Tag Manager. -->";
	}
	return <<<HTML
<!-- Google Tag Manager (added by extension GoogleTagManager) -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id={$wgGoogleTagManagerContainerID}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{$wgGoogleTagManagerContainerID}');</script>
<!-- End Google Tag Manager -->
HTML;
}
GoogleTagManager.i18n.php
<?php
/**
 * Internationalisation file for extension GoogleTagManager.
 *
 * @file
 * @ingroup Extensions
 */

$messages = array();

/** English
 * @author Felix Kaiser
 */
$messages['en'] = array(
	'googletagmanager-desc' => 'Inserts a Google Tag Manager container into pages for tracking',
);

/** Message documentation (Message documentation)
 * @author Felix Kaiser
 */
$messages['qqq'] = array(
	'googletagmanager-desc' => 'The name of the extension\'s entry in Special:SpecialPages',
);

/** German (Deutsch)
 * @author Kghbln
 */
$messages['de'] = array(
	'googletagmanager-desc' => 'Ergänzt Seiten zum Tracking um einen „Google Tag Manager“-Container'
);

See also[edit]