Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions app/Console/Commands/UpdateArticleViewCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ public function handle()

protected function getViewCountFor(Article $article): ?int
{
$viewCount = $this->getViewCountForUrl(route('articles.show', $article->slug));
$canonicalViewCount = ($url = $article->originalUrl()) ? $this->getViewCountForUrl($url) : 0;

return ($total = $viewCount + $canonicalViewCount) > 0 ? $total : null;
}

protected function getViewCountForUrl(string $url): int
{
if (! $url = parse_url($url)) {
return 0;
}

$scheme = $url['scheme'] ?? null;
$host = $url['host'] ?? null;
$path = $url['path'] ?? null;

if (! $scheme || ! $host || ! $path) {
return 0;
}

$response = Http::withToken($this->token)
->get('https://api.usefathom.com/v1/aggregations', [
'date_from' => '2021-03-01 00:00:00', // Fathom data aggregations not accurate prior to this date.
Expand All @@ -54,13 +74,18 @@ protected function getViewCountFor(Article $article): ?int
[
'property' => 'pathname',
'operator' => 'is',
'value' => "/articles/{$article->slug()}",
'value' => $path,
],
[
'property' => 'hostname',
'operator' => 'is',
'value' => "{$scheme}://{$host}",
],
]),
]);

if ($response->failed()) {
return null;
return 0;
}

return $response->json('0.pageviews');
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/_fathom.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@production
<!-- Fathom - beautiful, simple website analytics -->
<script src="https://boom.laravel.io/script.js" site="UXCUXOED" defer></script>
<script src="https://boom.laravel.io/script.js" site="UXCUXOED" defer @if(isset($canonical) && !Str::startsWith($canonical, config('app.url')))data-canonical="false"@endif></script>
<!-- / Fathom -->
@endproduction
@endproduction
25 changes: 25 additions & 0 deletions tests/Feature/CanonicalUrlTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

use App\Models\Article;
use App\Models\Tag;
use App\Models\Thread;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\App;
use Tests\Feature\BrowserKitTestCase;

uses(BrowserKitTestCase::class);
uses(DatabaseMigrations::class);

function inProduction()
{
App::detectEnvironment(fn () => 'production');
}

afterEach(fn () => App::detectEnvironment(fn () => 'testing'));

test('pages without a canonical url explicitly set fall back to the current url', function () {
$this->get('/register')
->see('<link rel="canonical" href="http://localhost/register" />');
Expand Down Expand Up @@ -51,3 +60,19 @@
$this->get('?utm_source=twitter&utm_medium=social&utm_term=abc123')
->see('<link rel="canonical" href="http://localhost" />');
});

test('canonical tracking is turned off when using external url', function () {
Article::factory()->create(['slug' => 'my-first-article', 'submitted_at' => now(), 'approved_at' => now(), 'original_url' => 'https://example.com/external-path']);

$this->get('/articles/my-first-article')
->see('data-canonical="false"');
})->inProduction();

test('canonical tracking is turned on when using external url', function () {
App::detectEnvironment(fn () => 'production');

Article::factory()->create(['slug' => 'my-first-article', 'submitted_at' => now(), 'approved_at' => now()]);

$this->get('/articles/my-first-article')
->dontSee('data-canonical="false"');
})->inProduction();
40 changes: 40 additions & 0 deletions tests/Integration/Commands/UpdateArticleViewCountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@
expect($article->fresh()->view_count)->toBe(1234);
});

test('article view counts can be merged with original url', function () {
Http::fake(function () {
return Http::response([[
'pageviews' => 1234,
]]);
});

$article = Article::factory()->create([
'title' => 'My First Article',
'slug' => 'my-first-article',
'original_url' => 'https://example.com/my-first-article',
'submitted_at' => now(),
'approved_at' => now(),
]);

(new UpdateArticleViewCounts)->handle();

expect($article->fresh()->view_count)->toBe(2468);
});

test('article view counts are not merged when url is invalid', function () {
Http::fake(function () {
return Http::response([[
'pageviews' => 1234,
]]);
});

$article = Article::factory()->create([
'title' => 'My First Article',
'slug' => 'my-first-article',
'original_url' => 'erhwerhwerh',
'submitted_at' => now(),
'approved_at' => now(),
]);

(new UpdateArticleViewCounts)->handle();

expect($article->fresh()->view_count)->toBe(1234);
});

test('article view counts are not updated if API call fails', function () {
Http::fake(function () {
return Http::response('Uh oh', 500);
Expand Down