Extension:Google Analytics Links

MediaWiki extensions manual
Google Analytics Links
Release status: unmaintained
Implementation Tag , User activity
Description Enables outbound and file link tracking
Author(s) Nik Molnar (Algaetalk)
Latest version 1.2 (2009-12-28)
MediaWiki 1.23+
PHP 5.3+
Database changes No
License GNU General Public License 2.0 or later
Download see below
Example kJams
  • ‎<googa>

The Google Analytics Links extension enables outbound and file link tracking.

Usage edit

The third parameter is optional (the one starting with the last "|" and ending just before the "</").
<googa>http://whatever.com/whatever/|Pretty Link Name|/fake/analytic/path</googa>
 <googa>/local/link.xyz|Pretty Link Name|/fake/analytic/path</googa>

Installation edit

For this extension to work the Google Analytics Integration extension must be installed first.
  • Copy the code into a file called "GoogleAnalyticsLinks.php" and place the file(s) in a directory called GoogleAnalyticsLinks in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    require_once "$IP/extensions/GoogleAnalyticsLinks/GoogleAnalyticsLinks.php";
    // Place the above line AFTER the existing line: $googleAnalytics = "UA-xxxxxxx-1";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code edit

GoogleAnalyticsLinks.php
<?php

/*
	Google Analytics Links - v1.2
	(c)2007 Nik Molnar (nik.molnar@gmail.com)
	Distributed UNDER THE TERMS OF GNU GPL Licence.
	
	This extension allows you to easily make use of Google Analytics to track non-HTML files (PDF, AVI, etc.) via JavaScript.
	For more information: http://www.google.com/support/analytics/bin/answer.py?answer=27242
	
	Usage: (third parameter optional)
		<googa>http://yoursite.com/therealfile.pdf|Link Name|/google/friendly/path</googa>
		<googa>/local/path/file.ext|Link Name|/google/friendly/path</googa>
		<googa>http://external.com/whatever.html|Link Name|/google/friendly/path</googa>
	Output: <a href="http://yoursite.com/therealfile.pdf" onClick="javascript:pageTracker._trackPageview('/google/friendly/path')">Link Name</a>
*/

$wgExtensionCredits['parserhook'][] = array(
	'name'           => 'Google Analytics Links',
	'version'        => '1.2',
	'author'         => 'Nik Molnar',
	'description'    => 'Allows you to easily make use of Google Analytics to track non-HTML files.',
	'url'            => 'https://www.mediawiki.org/wiki/Extension:Google_Analytics_Links',
	'path'           => __FILE__,
);

$wgExtensionFunctions[] = "wfGAnalyticsExtension";

// Register the hook
function wfGAnalyticsExtension() {
	global $wgParser;
	$wgParser->setHook("googa", "renderGAnalytics");
}

// Render function
function renderGAnalytics($input) {
	// Parse the arguments
	$args = split("[|]", $input);
	
	// If 3rd argument is absent, use the 1st argument.
	if(!isset($args[2])) { $args[2] = $args[0]; }
	
	// Return the rendered output
	return
		"<a href=\"" . $args[0] .
		"\" class=\"external text\"" .
		" title=\"" . $args[0] . "\"" .
		" onClick=\"javascript:pageTracker._trackPageview('" . $args[2] .
		"');\">" . $args[1] .
		"</a>";
}

See also edit