aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrankie B <git@diskfloppy.me>2024-06-11 21:36:58 +0100
committerFrankie B <git@diskfloppy.me>2024-06-11 21:36:58 +0100
commite89a7bb329abf3cd252fea7f624acbc454d7caf4 (patch)
tree20808feb8932992013d25143bc9f4d12fa737b23
parent210363d17371a699b33a9502bd0f017dca98f915 (diff)
Add pageview logging via PostHog
-rw-r--r--.env.example3
-rw-r--r--app/Http/Middleware/PageView.php28
-rw-r--r--app/Providers/AppServiceProvider.php18
-rw-r--r--composer.json1
-rw-r--r--composer.lock56
-rw-r--r--config/services.php5
-rw-r--r--routes/web.php17
7 files changed, 114 insertions, 14 deletions
diff --git a/.env.example b/.env.example
index 72dbf35..d2e73e2 100644
--- a/.env.example
+++ b/.env.example
@@ -26,3 +26,6 @@ MEMCACHED_HOST=127.0.0.1
LASTFM_KEY=
LASTFM_USER=
+
+POSTHOG_KEY=
+POSTHOG_HOST=
diff --git a/app/Http/Middleware/PageView.php b/app/Http/Middleware/PageView.php
new file mode 100644
index 0000000..dda0662
--- /dev/null
+++ b/app/Http/Middleware/PageView.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Http\Request;
+use PostHog\PostHog;
+use Symfony\Component\HttpFoundation\Response;
+
+class PageView
+{
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
+ */
+ public function handle(Request $request, Closure $next): Response
+ {
+ PostHog::capture([
+ 'distinctId' => request()->ip(),
+ 'event' => '$pageview',
+ 'properties' => array(
+ '$current_url' => url()->current(),
+ ),
+ ]);
+ return $next($request);
+ }
+}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index 452e6b6..3aa155c 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -2,23 +2,27 @@
namespace App\Providers;
+use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
+use PostHog\PostHog;
-class AppServiceProvider extends ServiceProvider
-{
+class AppServiceProvider extends ServiceProvider {
/**
* Register any application services.
*/
- public function register(): void
- {
+ public function register(): void {
//
}
/**
* Bootstrap any application services.
*/
- public function boot(): void
- {
- //
+ public function boot(): void {
+ PostHog::init(
+ Config::get('services.posthog.key'),
+ [
+ 'host' => 'https://'.Config::get('services.posthog.host')
+ ]
+ );
}
}
diff --git a/composer.json b/composer.json
index 315bb81..0a21ccb 100644
--- a/composer.json
+++ b/composer.json
@@ -10,6 +10,7 @@
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/tinker": "^2.8",
+ "posthog/posthog-php": "^3.3",
"scrivo/highlight.php": "v9.18.1.10",
"sentry/sentry-laravel": "^4.1",
"spatie/laravel-honeypot": "^4.3",
diff --git a/composer.lock b/composer.lock
index 50858f8..fb73243 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "c228a32331b8e43e449ba12dc4439757",
+ "content-hash": "3204be06a1b7a37cc0af50a0babca0a0",
"packages": [
{
"name": "auth0/auth0-php",
@@ -3042,6 +3042,60 @@
"time": "2023-11-12T21:59:55+00:00"
},
{
+ "name": "posthog/posthog-php",
+ "version": "3.3.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/PostHog/posthog-php.git",
+ "reference": "34301d0f20e20b785d465c574557278d18620f31"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/PostHog/posthog-php/zipball/34301d0f20e20b785d465c574557278d18620f31",
+ "reference": "34301d0f20e20b785d465c574557278d18620f31",
+ "shasum": ""
+ },
+ "require": {
+ "ext-json": "*",
+ "php": ">=8.0"
+ },
+ "require-dev": {
+ "overtrue/phplint": "^3.0",
+ "phpunit/phpunit": "^9.0",
+ "slope-it/clock-mock": "^0.4.0",
+ "squizlabs/php_codesniffer": "^3.7"
+ },
+ "bin": [
+ "bin/posthog"
+ ],
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "PostHog\\": "lib/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "PostHog <hey@posthog.com>",
+ "homepage": "https://posthog.com/"
+ }
+ ],
+ "description": "PostHog PHP Library",
+ "homepage": "https://github.com/PostHog/posthog-php",
+ "keywords": [
+ "posthog"
+ ],
+ "support": {
+ "issues": "https://github.com/PostHog/posthog-php/issues",
+ "source": "https://github.com/PostHog/posthog-php/tree/3.3.2"
+ },
+ "time": "2024-04-03T13:19:42+00:00"
+ },
+ {
"name": "psr-discovery/all",
"version": "1.0.1",
"source": {
diff --git a/config/services.php b/config/services.php
index 21e97c7..a1cd7da 100644
--- a/config/services.php
+++ b/config/services.php
@@ -17,5 +17,10 @@ return [
'lastfm' => [
'key' => env('LASTFM_KEY'),
'user' => env('LASTFM_USER'),
+ ],
+
+ 'posthog' => [
+ 'key' => env('POSTHOG_KEY'),
+ 'host' => env('POSTHOG_HOST'),
]
];
diff --git a/routes/web.php b/routes/web.php
index b87d32d..6456439 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -9,6 +9,7 @@ use App\Http\Controllers\ComputersController;
use App\Http\Controllers\GuestbookController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MusicController;
+use App\Http\Middleware\PageView;
use Illuminate\Support\Facades\Route;
/*
@@ -22,14 +23,18 @@ use Illuminate\Support\Facades\Route;
|
*/
-Route::get('/', [HomeController::class, 'show']);
-Route::get('/bookmarks', [BookmarksController::class, 'show']);
-Route::get('/guestbook', [GuestbookController::class, 'show']);
+// Run the PageView middleware for *all* public GET routes
+Route::middleware(PageView::class)->group(function () {
+ Route::get('/', [HomeController::class, 'show']);
+ Route::get('/bookmarks', [BookmarksController::class, 'show']);
+ Route::get('/guestbook', [GuestbookController::class, 'show']);
+ Route::get('/calculators', [CalculatorsController::class, 'show']);
+ Route::get('/computers', [ComputersController::class, 'show']);
+ Route::get('/music', [MusicController::class, 'show']);
+});
+
Route::post('/guestbook', [GuestbookController::class, 'addEntry'])
->middleware('rate_limit');
-Route::get('/calculators', [CalculatorsController::class, 'show']);
-Route::get('/computers', [ComputersController::class, 'show']);
-Route::get('/music', [MusicController::class, 'show']);
// Admin pages
Route::get('/admin/guestbook', [AdminGuestbookController::class, 'show'])