Skip to content
This repository has been archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
Latest support for Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Sep 3, 2018
1 parent 175c004 commit bceaaf9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 65 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"illuminate/support": "5.6.*",
"grafite/builder": "^2.4",
"devfactory/minify": "1.0.*",
"spatie/laravel-analytics": "^1.4",
"aws/aws-sdk-php": "^3.18",
"league/flysystem-aws-s3-v3": "^1.0",
"graham-campbell/markdown": "^10.0",
Expand Down
13 changes: 8 additions & 5 deletions src/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Grafite\Cms\Controllers;

use Illuminate\Support\Facades\Schema;
use Spatie\LaravelAnalytics\LaravelAnalyticsFacade as LaravelAnalytics;
use Grafite\Cms\Services\AnalyticsService;
use Illuminate\Support\Facades\Schema;
use Spatie\Analytics\Analytics;
use Spatie\Analytics\Period;

class DashboardController extends GrafiteCmsController
{
Expand All @@ -19,14 +20,16 @@ public function __construct(AnalyticsService $service)

public function main()
{
if (!is_null(config('laravel-analytics.siteId')) && config('cms.analytics') == 'google') {
foreach (LaravelAnalytics::getVisitorsAndPageViews(7) as $view) {
if (!is_null(config('analytics.view_id')) && config('cms.analytics') == 'google') {
$period = Period::days(7);

foreach (app(Analytics::class)->fetchVisitorsAndPageViews($period) as $view) {
$visitStats['date'][] = $view['date']->format('Y-m-d');
$visitStats['visitors'][] = $view['visitors'];
$visitStats['pageViews'][] = $view['pageViews'];
}

return view('cms::dashboard.analytics-google', compact('visitStats', 'oneYear'));
return view('cms::dashboard.analytics-google', compact('visitStats', 'period'));
} elseif (is_null(config('cms.analytics')) || config('cms.analytics') == 'internal') {
if (Schema::hasTable(config('cms.db-prefix', '').'analytics')) {
return view('cms::dashboard.analytics-internal')
Expand Down
4 changes: 0 additions & 4 deletions src/GrafiteCmsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Intervention\Image\ImageServiceProvider;
use Spatie\LaravelAnalytics\LaravelAnalyticsFacade;
use Spatie\LaravelAnalytics\LaravelAnalyticsServiceProvider;

class GrafiteCmsProvider extends ServiceProvider
{
Expand Down Expand Up @@ -140,14 +138,12 @@ public function register()
$this->app->register(GrafiteBuilderProvider::class);
$this->app->register(MinifyServiceProvider::class);
$this->app->register(MarkdownServiceProvider::class);
$this->app->register(LaravelAnalyticsServiceProvider::class);
$this->app->register(ImageServiceProvider::class);

$loader = AliasLoader::getInstance();

$loader->alias('Minify', MinifyFacade::class);
$loader->alias('Markdown', Markdown::class);
$loader->alias('LaravelAnalytics', LaravelAnalyticsFacade::class);
$loader->alias('Image', Image::class);

/*
Expand Down
12 changes: 6 additions & 6 deletions src/Views/dashboard/analytics-google.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

<div class="row raw-margin-top-24">
<div class="col-md-4">
<p class="lead">Keywords</p>
<p class="lead">Browsers</p>
<table class="table table-striped">
<thead>
<th>Keyword</th>
<th>Browser</th>
<th>Sessions</th>
</thead>
@foreach (LaravelAnalytics::getTopKeywords(365, 10) as $word)
@foreach (Analytics::fetchTopBrowsers($period, 10) as $word)
<tr>
<td>{{ $word['keyword'] }}</td>
<td>{{ $word['browser'] }}</td>
<td>{{ $word['sessions'] }}</td>
</tr>
@endforeach
Expand All @@ -32,7 +32,7 @@
<th>URL</th>
<th>Views</th>
</thead>
@foreach (LaravelAnalytics::getMostVisitedPages(365, 10) as $browser)
@foreach (Analytics::fetchMostVisitedPages($period, 10) as $browser)
<tr>
<td>{{ str_limit($browser['url'], 30) }}</td>
<td>{{ $browser['pageViews'] }}</td>
Expand All @@ -47,7 +47,7 @@
<th>URL</th>
<th>Views</th>
</thead>
@foreach (LaravelAnalytics::getTopReferrers(365, 10) as $referers)
@foreach (Analytics::fetchTopReferrers($period, 10) as $referers)
<tr>
<td>{{ str_limit($referers['url'], 30) }}</td>
<td>{{ $referers['pageViews'] }}</td>
Expand Down
53 changes: 4 additions & 49 deletions src/Views/dashboard/empty.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

@section('content')

<div class="row">
<div class="col-md-12">
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<p>The Cms dashboard is powered by <b>Google Analytics</b> or by its own <b>Internal Analytics</b>.</p>

<h3>Internal Analytics</h3>
Expand All @@ -15,53 +16,7 @@
</pre>

<h3>Google Analytics</h3>
<p>By following the directions below, you will enable the Google Analytics charts and data sets on your dashboard.</p>

<p>First run:</p>
<pre>
php artisan vendor:publish
</pre>

<p>Once you've run that command you will need to edit the following file: <code>config/laravel-analytics.php</code>. To do that please follow the directions below:</p>
<br>
<h4 class="text-center">How to obtain the credentials to communicate with Google Analytics</h4>
<br>
<h5>Step 1:</h5>
<hr>
<p>If you haven't already done so, <a href="https://support.google.com/analytics/answer/1042508">set up a Google Analtyics property</a> and <a href="https://support.google.com/analytics/answer/1008080?hl=en#GA">install the tracking code on your site</a>.</p>

<br>
<h5>Step 2:</h5>
<hr>
<p>You will need: <code>siteId</code>, <code>clientId</code> and <code>serviceEmail</code>. Additionally a <code>p12-file</code> is required.</p>
<p>To obtain these credentials start by going to the <a href="https://console.developers.google.com">Google Developers Console</a>.</p>
<p>If you don't have a project present in the console yet, create one.</p>
<p>If you click on the project name, you'll see a menu item <code>APIs</code> under <code>APIs &amp; auth</code> on the left hand side. Click it to go the the Enabled API's screen. On that screen you should enable the Analytics API.</p>
<p>Now, again under the <code>APIs &amp; Auth</code>-menu click <code>Credentials</code>.</p>
<p>On this screen you should press <code>Create new Client ID</code>. In the creation screen make sure you select application type <code>Service Account</code> and key type <code>P12-key</code>.</p>

<p>The creation of the Service Account generated a new public/private key pair and the <code>.p12-file</code> will get downloaded to your machine. Store this file in the location specified in the config file of this package.</p>

<p>From the <code>Credentials</code> screen in Google Console. From there select the <a href="https://console.developers.google.com/permissions/serviceaccounts">'Manage service accounts'</a>. Within that screen, edit the service account you just made and select <code>Enable Google Apps Domain-wide Delegation</code> This will allow you to view the <code>client ID</code> where you can see the <code>client ID</code> and the <code>Client Email</code>.</p>

<br>
<h5>Step 3:</h5>
<hr>
<p>To find the right value for <code>siteId</code> log in to <a href="http://www.google.be/intl/en/analytics/">Google Analytics</a> go the the Admin section.</p>
<p>In the property-column select the website name of which you want to retrieve data, then click <code>View Settings</code> in the <code>View</code>-column.</p>
<p>The value presented as <code>View Id</code> prepended with 'ga:' can be used as <code>siteId</code>. You can see the 'ga:' example below.</p>

<br>
<h5>Notes:</h5>
<hr>
<p>For further information please follow these <a href="https://github.com/spatie/laravel-analytics">directions</a></p>

<p>Please add the following lines to you <code>.env</code> file.</p>
<pre>
ANALYTICS_SITE_ID=ga:&lt;Site ID&gt;
ANALYTICS_CLIENT_ID=&lt;Client ID&gt;
ANALYTICS_SERVICE_EMAIL=&lt;Client Email&gt;
</pre>
<p>Grafite CMS uses the Spatie package for Google Analytics integration. <br><br>Please follow its installation instructions: <a target="_blank" href="https://github.com/spatie/laravel-analytics#installation">https://github.com/spatie/laravel-analytics#installation</a></p>
</div>
</div>

Expand Down

0 comments on commit bceaaf9

Please sign in to comment.