Overview

This is a WordPress plugin that shows articles ranking using Google Analytics.
The feature to work very lightweight, because it is not save ranking data in WordPress DB.

Install

Simple GA Ranking on wordpress.org. Please install below:

wordpress.org

Setup

  1. Plugin install in yout WordPress
  2. Oauth2 setting in /wp-admin/options-general.php?page=gapiwp-analytics. like below movie:
  3. Parameter setting in /wp-admin/options-general.php?page=sga_ranking.
  4. Set ranking module, using Simple GA Ranking Widget or [sga_ranking] shortcode or sga_ranking_get_date() function.

Function

The sga_ranking_get_date() function return post_id of ranking as array. Put the function at where you'd like to show the ranking in your theme.

Parameters

<?php 
array(
    'display_count'           => 10,
    'period'                  => 30,
    'post_type'               => 'post',
    'exclude_post_type'       => '',
    '`taxonomy_slug`__in'     => '',
    '`taxonomy_slug`__not_in' => '',
    'filter'                  => ''
);
?>

Examples

Sample Code 1 : Common use.
<?php 
if (function_exists('sga_ranking_get_date')) {
    $ranking_data = sga_ranking_get_date();
    if ( !empty( $ranking_data ) ) {
        echo '<ol>';
        foreach ( $ranking_data as $post_id ) {
            echo '<li><a href="' . esc_attr(get_permalink($post_id)) . '">' . esc_html(get_the_title($post_id)) . '</a></li>';
        }
        echo '</ol>';
    }
}
?>
Sample Code 2 : Filter a news category in posts.
<?php
$args = array(
    'display_count' => 10,
    'period'        => 30,
    'post_type'     => 'post',
    'category__in'  => 'news'
);
$ranking_data = sga_ranking_get_date($args);
?>
Sample Code 3 : Filter /en/ in URL.
<?php
$args = array(
    'display_count' => 10,
    'period'        => 30,
    'post_type'     => 'post,page',
    'filter'        => 'ga:pagePath=~^/en/'
);
$ranking_data = sga_ranking_get_date($args);
?>

Shortcode

Put the [sga_ranking] shortcode at where you'd like to show the ranking.

Parameters

The same as sga_ranking_get_date function.

Examples

Hooks

We provide some WordPress hooks. you can customize to use it.

Filter Hooks

Examples

Sample Code 1 : Cache expire is updated 12 hours.
<?php add_filter( 'sga_ranking_cache_expire', function($expire) { return 12*60*60; } ); ?>
Sample Code 2 : The number of data from GA is updated 200.
<?php add_filter( 'sga_ranking_limit_filter', function($limit) { return 200; } ); ?>

Share

Google+