-
-
Notifications
You must be signed in to change notification settings - Fork 668
Add article view counts #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| namespace App\Console\Commands; | ||
|
|
||
| use App\Models\Article; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Support\Facades\Http; | ||
|
|
||
| final class UpdateArticleViewCounts extends Command | ||
| { | ||
| protected $signature = 'lio:update-article-view-counts'; | ||
|
|
||
| protected $description = 'Queries the Fathom Analytics API to update the view counts for all articles'; | ||
|
|
||
| protected $siteId; | ||
|
|
||
| protected $token; | ||
|
|
||
| public function __construct() | ||
| { | ||
| parent::__construct(); | ||
|
|
||
| $this->siteId = config('services.fathom.site_id'); | ||
| $this->token = config('services.fathom.token'); | ||
| } | ||
|
|
||
| public function handle() | ||
| { | ||
| if (! $this->siteId || ! $this->token) { | ||
| $this->error('Fathom site ID and token must be configured'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| Article::published()->chunk(100, function ($articles) { | ||
| $articles->each(function ($article) { | ||
| $article->update([ | ||
| 'view_count' => $this->getViewCountFor($article), | ||
| ]); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| protected function getViewCountFor(Article $article): ?int | ||
| { | ||
| $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. | ||
| 'field_grouping' => 'pathname', | ||
| 'entity' => 'pageview', | ||
| 'aggregates' => 'pageviews,visits,uniques', | ||
| 'entity_id' => $this->siteId, | ||
| 'filters' => json_encode([ | ||
| [ | ||
| 'property' => 'pathname', | ||
| 'operator' => 'is', | ||
| 'value' => "/articles/{$article->slug()}", | ||
| ], | ||
| ]), | ||
| ]); | ||
|
|
||
| if ($response->failed()) { | ||
| return null; | ||
| } | ||
|
|
||
| return $response->json('0.pageviews'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
database/migrations/2022_07_09_191433_update_articles_table_add_view_count.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up() | ||
| { | ||
| Schema::table('articles', function (Blueprint $table) { | ||
| $table->bigInteger('view_count')->nullable()->after('is_pinned'); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/Integration/Commands/UpdateArticleViewCountsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| use App\Console\Commands\UpdateArticleViewCounts; | ||
| use App\Models\Article; | ||
| use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
| use Illuminate\Support\Facades\Http; | ||
| use Tests\TestCase; | ||
|
|
||
| uses(TestCase::class); | ||
| uses(DatabaseMigrations::class); | ||
|
|
||
| test('article view counts can be updated', function () { | ||
| Http::fake(function () { | ||
| return Http::response([[ | ||
| 'pageviews' => 1234, | ||
| ]]); | ||
| }); | ||
|
|
||
| $article = Article::factory()->create([ | ||
| 'title' => 'My First Article', | ||
| 'slug' => 'my-first-article', | ||
| '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); | ||
| }); | ||
|
|
||
| $article = Article::factory()->create([ | ||
| 'title' => 'My First Article', | ||
| 'slug' => 'my-first-article', | ||
| 'submitted_at' => now(), | ||
| 'approved_at' => now(), | ||
| ]); | ||
|
|
||
| (new UpdateArticleViewCounts)->handle(); | ||
|
|
||
| expect($article->fresh()->view_count)->toBeNull(); | ||
| }); | ||
|
|
||
| test('view counts are not updated for unpublished articles', function () { | ||
| Http::fake(); | ||
|
|
||
| Article::factory()->create([ | ||
| 'title' => 'My First Article', | ||
| 'slug' => 'my-first-article', | ||
| 'submitted_at' => now(), | ||
| ]); | ||
|
|
||
| (new UpdateArticleViewCounts)->handle(); | ||
|
|
||
| Http::assertNothingSent(); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.