Extension:GoogleAnalyticsMetrics

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
GoogleAnalyticsMetrics
Release status: stable
Implementation Parser function
Description Gets metrics from Google Analytics
Author(s) Ike Hecht (Tosfostalk)
Latest version 1.0.0 (April 2018)
MediaWiki 1.25+
PHP 5.3+
Database changes Yes
License GNU General Public License 2.0 or later
Download
Parameters
  • $wgGoogleAnalyticsMetricsAllowed,
  • $wgGoogleAnalyticsMetricsEmail,
  • $wgGoogleAnalyticsMetricsPath
Hooks used
ParserFirstCallInit
Translate the GoogleAnalyticsMetrics extension if it is available at translatewiki.net
Check usage and version matrix.

The GoogleAnalyticsMetrics extension gets various metrics from Google Analytics and inserts them into a wiki page using a parser function.

Installation[edit]

  • Setup Google Analytics API by following Step 1 here
  • Install the Google Client Library by following Step 2 here
  • Download and place the file(s) in a directory called GoogleAnalyticsMetrics in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    wfLoadExtension( 'GoogleAnalyticsMetrics' );
    
  • Run the update script which will automatically create the necessary database tables that this extension needs.
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage[edit]

The googleanalyticsmetrics parser function takes three parameters, in the form of {{#googleanalyticsmetrics: metric=Name of metric|page=Page Name|startDate=Start date|endDate=End date}}:

Name of metric
The name of the Google Analytics Metric that should be displayed. The full list of Google Analytics metrics is here. Omit the ga: prefix.
Page Name
The name of the page. This parameter is only used when the metric is a per page metric.
Start date
Show the metric since this date (optional). Defaults to 2005-01-01, which is the beginning of time according to Google Analytics.
End date
Show the metric until this date (optional). Defaults to today.

Both date parameters use the form: Year-Month-Day.

Examples[edit]

This example displays the number of page views for this site, from the beginning until the end of time:

{{#googleanalyticsmetrics: metric=pageviews}}

Show the number of sessions from February 3, 2011 until today.

{{#googleanalyticsmetrics: metric=session|startDate=2011-02-03}}

Show the average session duration until February 1 2012:

{{#googleanalyticsmetrics: metric=avgSessionDuration|endDate=2012-02-01}}

Show the number of page views for last month only, with the help of the ParserFunctions extension:

{{#googleanalyticsmetrics: metric=pageviews|startDate={{#time:Y-m-d|first day of last month}}|endDate={{#time:Y-m-d|first day of this month}}}}

Or, to make that nicer:

{{formatnum:{{#googleanalyticsmetrics: metric=pageviews|startDate={{#time:Y-m-d|first day of last month}}|endDate={{#time:Y-m-d|first day of this month}}}}}}

Show the number of page views for a page, from the beginning until the end of time:

{{#googleanalyticsmetrics: metric=pageviews|page=Main_Page}}

See this page for some help with formatting this extension's output.

Configuration parameters[edit]

$wgGoogleAnalyticsMetricsAllowed
An array containing the name of Google Analytics metrics that are allowed to be retrieved by the extension. Defaults to *, which allows all metrics.
$wgGoogleAnalyticsMetricsEmail
The email address of the Google Analytics account.
$wgGoogleAnalyticsMetricsPath
Path to your certificate key file.
$wgGoogleAnalyticsMetricsViewId
Google Analytics View ID.

See also[edit]