Communicate with Google Analytics API.

1.1.1 2021-01-15 06:54 UTC

This package is auto-updated.

Last update: 2021-01-15 07:01:24 UTC


Let your WordPress communicate with Google Analytics API.

Getting Start

Install via composer.


composer require kunoichi/ga-communicator


From your theme or plugin, include autoloader.

// This path will be changed depending on your use case.
require __DIR__ . '/vendor/autload.php';

Run Boostrap

Calling bootstrap function, all hooks will be registered.


Now your code is ready to run.

Network Installation

If your site is under network, define constant GA_COMMUNICATOR_NETWORK_ACTIVE to true.


This will affect:

  • Setting screen will move to Network Admin > Setting.
  • All settings will be common for all sites under network.

Register Service Account

To communicate Google Analytics, you need permission to do so.

Get service account key at Google API Console(document).

A service account is something like a bot which has email and perform like a virtual Google account.

  1. Create service account. You may be requested to create a new project, or else you can also use an exsting project. The bot belongs to the project.
  2. Get private key in JSON format for the serivce account.
  3. Enable Analytics Reporting API and Google Analytics API for the project.
  4. Copy the email address of the service account and add it to a member of your Google Analytics account(properties, views and so on).
  5. Go to your WordPress admin Setting > Google Analytics Setting and save the private key which you get at step 2.
    NOTICE: Save whole credentials including empty line with +A and +C. Otherwise, your key will be considered as invalid by Google's API.


Here's example for this API.

Get popular posts

GaComunicator->popular_posts() method will:

  1. Request page views and path information from Analytics Reporting API.
  2. Convert path information to post ID.
  3. Query posts with IDs and returns WP_Query object.

With this method, you can get popular posts in WordPress way.



An array merged into new WP_Query( $query ). If you need just post type faq, $query is like below:

$query = [
	'post_type' => 'faq',


An array of conditions to filter results. Default is defined inside code like below:

$conditions = [
	'path_regexp' => $this->get_permalink_filter(), // Default is permalink structure.
	'number'      => 10, // Post count.
	'days_before' => 30,
	'offset_days' => 0,
	'start'       => '',
	'end'         => '',

To get posts well visited in recent 7 days(e.g. weekly ranking), specify days_before. offset_days works as an adjustment.

// Get Sun-Mon last week ranking.
$contiond = [
	'days_before' => 7,
	'offset_days' => date_i18n( 'w' ), // Set origin to Sunday.

If you need a list of popular posts in specific range(e.g. Olympic year), specify start and end in YYYY-mm-dd.

$conditions = [
	'start' => '2020-06-01', // As you know, this hasn't actually happen.
	'end'   => '2020-08-31',


Let's get popular posts post_type=faq in recent 7 days.

$query = \Kunoichi\GaCommunicator::get_instance()->popular_posts( [
	'post_type' => 'faq',
], [
	'path_regexp' => '^/faq/[0-9]+/$', // Regular expression to filter path.
	'number'      => 10, // Number of posts.
	'days_before' => 7, // Recent 7 days.
] );
if ( $query ) {
	while ( $query->have_posts() ) {
		// You can do things in WordPress way.
		get_template_part( 'template-parts/loop', get_post_type() );

Cache result makes your site's performance nicer.

Free Request

You can get any information in your Google Analytics account with this library.

You need understanding about Mertic and Dimensions.

GaCommunicator->get_report will provide