Skip to content

Commit

Permalink
Add pre-cache command, issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
jcohlmeyer committed Sep 2, 2019
1 parent 59d803c commit 0ffc903
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
116 changes: 116 additions & 0 deletions Commands/PrecacheHitsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Statamic\Addons\GoogleAnalytics\Commands;

use Carbon;
use Analytics;
use JRC9DS\Analytics\Period;
use Statamic\API\Cache;
use Statamic\API\Page;
use Statamic\API\Taxonomy;
use Statamic\Extend\Command;
use Statamic\API\Collection;

class PrecacheHitsCommand extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'googleanalytics:precache-hits';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Precache hit values for the hits tag.';

/**
* Create a new command instance.
*/
public function __construct() {
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
// Hold urls to pre-cache
$urls = [];

// Get urls for pages
$pages = Page::all()->removeUnpublished();
foreach ($pages as $page) {
$urls[] = $page->url();
}

// Get urls for collections
$handles = Collection::handles();
foreach($handles as $handle) {
$urls = array_merge($urls, $this->collectionUrls($handle));
}

// Get urls for taxonomies
$handles = Taxonomy::handles();
foreach($handles as $handle) {
$urls = array_merge($urls, $this->taxonomyUrls($handle));
}

$index = 1;
$size = sizeof($urls);

foreach ($urls as $url) {
$data = (int)Analytics::performQuery(
// This is the date that Google Analytics was started
Period::create(new Carbon('2005-01-01'), new Carbon()),
'ga:pageviews',
[
'filters' => 'ga:pagePath==' . $url,
]
)->totalsForAllResults['ga:pageviews'];
$this->cache->put($url, $data, $this->getConfig('page_hits_cache_time', 1440));
$this->info($index . "/" . $size . " -- Cached URL: " . $url . " Page Views: " . $data);
$index++;
}
}

/**
* Gets the collection urls for every entry in a given collection
*
* @return array(string)
*/
private function collectionUrls($handle) {
$urls = [];
$collection = Collection::whereHandle($handle);
$entries = $collection->entries()->removeUnpublished();
foreach ($entries as $entry) {
$url = $entry->url();
if ($url !== '/') {
$urls[] = $url;
}
}
return $urls;
}

/**
* Gets the taxonomy urls for every entry in a given taxonomy
*
* @return array(string)
*/
private function taxonomyUrls($handle) {
$urls = [];
$taxonomy = Taxonomy::whereHandle($handle);
$terms = $taxonomy->terms()->removeUnpublished();
foreach ($terms as $term) {
$url = $term->url();
if ($url !== '/') {
$urls[] = $url;
}
}
return $urls;
}
}
6 changes: 6 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ If you want to show how many hits a page has, you can use the page hits tag: `{{
## Page Hits Sorting (Filter)
If you want to sort your collection entities by page hits you can do so using the custom sort filter and setting the default sort to none. `{{ collection:blog filter="google_analytics" sort="false" }}`

## Page Hits Precache
If you are sorting or filtering content by page hits you may want to pre cache your page hits numbers in the background. You can do this using the `php please googleanalytics:precache-hits` command.

In order for this to be effective you should set up a cron job to run this command before your cache expires.

You can set the cache time for page hits on the settings page for Google Analytics.

## Access Restrictions
By default, anybody with *CP Access* will be able to see Google Analytics reports.
Expand Down
2 changes: 1 addition & 1 deletion GoogleAnalyticsTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function index() {
* @return integer
*/
public function hits() {
$key = $this->context['uri'];
$key = $this->context['url'];

$data = $this->cache->get($key);

Expand Down

0 comments on commit 0ffc903

Please sign in to comment.