Extension:Google Analytics

From MediaWiki.org
Jump to: navigation, search
For a better updated extension with more features, please see: Extension:Google Analytics Integration
MediaWiki extensions manual - list
Crystal Clear action run.png
Google Analytics

Release status: stable

Implementation Tag, User activity
Description Enable Google Analytics within your wiki
Author(s) Naoise Golden Santos
Last version 1.0 (1-29-2007)
License No license specified
Download see below
Example live demo

Check usage (experimental)

Analytics is a simple extension which allows you to put a Google Analytics tracker in your MediaWiki articles, so you can obtain statistics on the articles that you are interested in.

Contents

[edit] Usage

<analytics uacct="UA-17301139-1" ></analytics>

Replace UA-17301139-1 with your own Google Analytics account number.

[edit] Source code

Source code of "extensions/analytics.php":

 
<?php
# Google Analytics MediaWiki extension
# by Naoise Golden Santos (naoise at goldensantos dot com)
# http://www.goldensantos.com
# Modified for the new Google Code
# Modified by Fernando Azevedo (fazevedo at fazevedo dot org)
# http://www.fazevedo.org

# Usage:
# <analytics uacct="UA-XXXXXX-X" ></analytics>
 
# To install it put this file in the extensions directory 
# To activate the extension, include it from your LocalSettings.php
# with: require("extensions/analytics.php");
 
$wgExtensionFunctions[] = "wfAnalytics";
 
function wfAnalytics() {
    global $wgParser;
    # registers the <analytics> extension with the WikiText parser
    $wgParser->setHook( "analytics", "renderAnalytics" );
}
 
# The callback function for converting the input text to HTML output
function renderAnalytics( $input, $argv ) {
 
        $output = '<script type="text/javascript">';
        $output .= 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
        $output .= 'document.write(unescape("%3Cscript src='."'".'" + gaJsHost + "google-analytics.com/ga.js';
        $output .= "'".' type='."'".'text/javascript'."'".'%3E%3C/script%3E"));';
        $output .= '</script>';
        $output .= '<script type="text/javascript">';
        $output .= 'try {';
        $output .= 'var pageTracker = _gat._getTracker("'.$argv["uacct"].'");';
        $output .= 'pageTracker._trackPageview();';
        $output .= '} catch(err) {}</script>';
 
    return $output;
}

Add in LocalSettings.php

 require_once("$IP/extensions/analytics.php");

[edit] Older Source code

This was the code for older Google Analytics installations, sometime pre-January 2007.

Source code of "extensions/analytics.php":

 
<?php
# Google Analytics MediaWiki extension
# by Naoise Golden Santos (naoise at goldensantos dot com)
# http://www.goldensantos.com

# Usage:
# <analytics uacct="UA-XXXXXX-X" ></analytics>

# To install it put this file in the extensions directory 
# To activate the extension, include it from your LocalSettings.php
# with: require("extensions/analytics.php");

$wgExtensionFunctions[] = "wfAnalytics";
 
function wfAnalytics() {
    global $wgParser;
    # registers the <analytics> extension with the WikiText parser
    $wgParser->setHook( "analytics", "renderAnalytics" );
}
 
# The callback function for converting the input text to HTML output
function renderAnalytics( $input, $argv ) {
 
        $output = '<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">';
        $output .= '</script>';
        $output .= '<script type="text/javascript">';
        $output .= '_uacct = "'.$argv["uacct"].'";';
        $output .= 'urchinTracker();';
        $output .= '</script>';
 
    return $output;
}

[edit] More info

[edit] See also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox