Extension:GoogleAnalyticsTopPages

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
GoogleAnalyticsTopPages
Release status: experimental
Implementation Tag , API
Description Provides a list of top pages with the use of Google Analytics data.
Author(s) Florian Schmidt (Florianschmidtwelzowtalk)
Latest version 0.0.1 (2014-11-22)
MediaWiki 1.23+
PHP 5.3+
Database changes Yes
License MIT License
Download
Translate the GoogleAnalyticsTopPages extension if it is available at translatewiki.net
Check usage and version matrix.
Issues Open tasks · Report a bug

GoogleAnalyticsTopPages provides a parser hook, which adds a list of top pages to the wikipage content. The top pages are retrieved from Google Analytics via the Google Analytics API. You need a Google Service account to use this Extension.

Requirements[edit]

This Extension requires the Extension GoogleAPIClient to work.

Installation[edit]

  • Download and place the file(s) in a directory called GoogleAnalyticsTopPages in your extensions/ folder.
  • Only when installing from git run Composer to install PHP dependencies, by issuing composer install --no-dev in the extension directory. (See T173141 for potential complications.)
  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/GoogleAnalyticsTopPages/GoogleAnalyticsTopPages.php";
    
  • Run the update script which will automatically create the necessary database tables that this extension needs.
  • Configure the required parameters
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration[edit]

Before you configure the Extension, make sure, you have a Service account and access to the Google Analytics API.

Google service account[edit]

The following, simple step-by-step tutorial helps you to setup a service account to use this extension:

  1. Open the developer console and login with your Google account
  2. Navigate to APIs & auth and click APIs
  3. Find Analytics API and activate it (click on off), you need to accept the Terms of Service!
  4. Go to Credentials
  5. In section OAuth click Create new Client ID
  6. As application type choose Service account
  7. Click Create Client ID
  8. You see a secret key and a download with the certificate will start. Save the certificate on your Computer and note the secret key (don't loose this key!)
  9. Save your certificate somewhere on your server, but be sure to not save it somewhere public (e.g. in your wikis root or webservers root path!)
  10. Use the data of the new created service account to configure GoogleAnalyticsTopPages

Extension configuration[edit]

The extension needs at least the following configuration variables (you see these in your Google Developer Console and Google Analytics account, remove "<" and ">"):

$wgGATPServiceAccountName = '<EMAIL ADDRESS>';
$wgGATPProfileId = '<View ID>';
$wgGATPKeyFileLocation = '<path to your certificate>';

View ID: The View ID is the ID of a View in your Google Analytics Account. Go to Google Analytics, click on Admin and select the view related to your wiki where you want to use GoogleanalyticsTopPages. Click on View Settings and use the number in View ID.

path to your certificate: The absolute path to your certificate file for your service account you created above, e.g. /var/cert/ga-wiki.p12.

Additional Configuration parameter[edit]

Configuration variable Default value Description
$wgGATPAppName 'googleanalyticstoppages' Used as the App name for the Google API request.
$wgGATPInterval 30 Interval to use to get top pages from Google Analytics. E.g. get top pages from 2014-10-23 to 2014-11-22.
$wgGATPProtectAPI true If set to true, you need to add your Secret key to the API request to update the Google Analytics data.

Usage[edit]

The usage of the extension is very simple. It provides a new parser tag to generate a list of top pages: <gatp />. Simply add this tag to a wiki page and position where you want to show this list and save.

Update data[edit]

To update the list of top pages, you need to use the API. The extension introduces a new API modules, googlepagestatsupdate, which updates the internal database table. To use it, you have to make a POST request to the API in the following schema:

/api.php?action=googlepagestatsupdate&key=$wgSecretKey&format=json]

$wgSecretKey: You need to add your Secret key (can be found in your LocalSettings.php) here (replace $wgSecretKey) to make sure, that the API is only used by authorized applications. To disable this check (and use the API without the Sectret key, which isn't recommended) set the following configuration variable in your LocalSettings.php:

$wgGATPProtectAPI = false;

The result will be (if all is configured correctly):

{
	"googlepagestatsupdate": {
		"success":"true",
		"text":null
	}
}

You can automate the requests to the API using cronjob, e.g. to update the data every day, add the following cronjob:

0 0 * * * curl --data "action=googlepagestatsupdate&key=$wgSecretKey&format=json" http://www.example.com/api.php > ~/out.txt

See also[edit]