Add pageview logging via PostHog
This commit is contained in:
		
					parent
					
						
							
								3824f01f9b
							
						
					
				
			
			
				commit
				
					
						0f4da7e3df
					
				
			
		
					 7 changed files with 114 additions and 14 deletions
				
			
		|  | @ -26,3 +26,6 @@ MEMCACHED_HOST=127.0.0.1 | ||||||
| 
 | 
 | ||||||
| LASTFM_KEY= | LASTFM_KEY= | ||||||
| LASTFM_USER= | LASTFM_USER= | ||||||
|  | 
 | ||||||
|  | POSTHOG_KEY= | ||||||
|  | POSTHOG_HOST= | ||||||
|  |  | ||||||
							
								
								
									
										28
									
								
								app/Http/Middleware/PageView.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								app/Http/Middleware/PageView.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -2,23 +2,27 @@ | ||||||
| 
 | 
 | ||||||
| namespace App\Providers; | namespace App\Providers; | ||||||
| 
 | 
 | ||||||
|  | use Illuminate\Support\Facades\Config; | ||||||
| use Illuminate\Support\ServiceProvider; | use Illuminate\Support\ServiceProvider; | ||||||
|  | use PostHog\PostHog; | ||||||
| 
 | 
 | ||||||
| class AppServiceProvider extends ServiceProvider | class AppServiceProvider extends ServiceProvider { | ||||||
| { |  | ||||||
|     /** |     /** | ||||||
|      * Register any application services. |      * Register any application services. | ||||||
|      */ |      */ | ||||||
|     public function register(): void |     public function register(): void { | ||||||
|     { |  | ||||||
|         //
 |         //
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Bootstrap any application services. |      * 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') | ||||||
|  |             ] | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ | ||||||
|         "guzzlehttp/guzzle": "^7.2", |         "guzzlehttp/guzzle": "^7.2", | ||||||
|         "laravel/framework": "^10.10", |         "laravel/framework": "^10.10", | ||||||
|         "laravel/tinker": "^2.8", |         "laravel/tinker": "^2.8", | ||||||
|  |         "posthog/posthog-php": "^3.3", | ||||||
|         "scrivo/highlight.php": "v9.18.1.10", |         "scrivo/highlight.php": "v9.18.1.10", | ||||||
|         "sentry/sentry-laravel": "^4.1", |         "sentry/sentry-laravel": "^4.1", | ||||||
|         "spatie/laravel-honeypot": "^4.3", |         "spatie/laravel-honeypot": "^4.3", | ||||||
|  |  | ||||||
							
								
								
									
										56
									
								
								composer.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										56
									
								
								composer.lock
									
										
									
										generated
									
									
									
								
							|  | @ -4,7 +4,7 @@ | ||||||
|         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | ||||||
|         "This file is @generated automatically" |         "This file is @generated automatically" | ||||||
|     ], |     ], | ||||||
|     "content-hash": "c228a32331b8e43e449ba12dc4439757", |     "content-hash": "3204be06a1b7a37cc0af50a0babca0a0", | ||||||
|     "packages": [ |     "packages": [ | ||||||
|         { |         { | ||||||
|             "name": "auth0/auth0-php", |             "name": "auth0/auth0-php", | ||||||
|  | @ -3041,6 +3041,60 @@ | ||||||
|             ], |             ], | ||||||
|             "time": "2023-11-12T21:59:55+00:00" |             "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", |             "name": "psr-discovery/all", | ||||||
|             "version": "1.0.1", |             "version": "1.0.1", | ||||||
|  |  | ||||||
|  | @ -17,5 +17,10 @@ | ||||||
|     'lastfm' => [ |     'lastfm' => [ | ||||||
|         'key' => env('LASTFM_KEY'), |         'key' => env('LASTFM_KEY'), | ||||||
|         'user' => env('LASTFM_USER'), |         'user' => env('LASTFM_USER'), | ||||||
|  |     ], | ||||||
|  | 
 | ||||||
|  |     'posthog' => [ | ||||||
|  |         'key' => env('POSTHOG_KEY'), | ||||||
|  |         'host' => env('POSTHOG_HOST'), | ||||||
|     ] |     ] | ||||||
| ]; | ]; | ||||||
|  |  | ||||||
|  | @ -9,6 +9,7 @@ | ||||||
| use App\Http\Controllers\GuestbookController; | use App\Http\Controllers\GuestbookController; | ||||||
| use App\Http\Controllers\HomeController; | use App\Http\Controllers\HomeController; | ||||||
| use App\Http\Controllers\MusicController; | use App\Http\Controllers\MusicController; | ||||||
|  | use App\Http\Middleware\PageView; | ||||||
| use Illuminate\Support\Facades\Route; | use Illuminate\Support\Facades\Route; | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
|  | @ -22,14 +23,18 @@ | ||||||
| | | | | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
|  | // Run the PageView middleware for *all* public GET routes
 | ||||||
|  | Route::middleware(PageView::class)->group(function () { | ||||||
|     Route::get('/', [HomeController::class, 'show']); |     Route::get('/', [HomeController::class, 'show']); | ||||||
|     Route::get('/bookmarks', [BookmarksController::class, 'show']); |     Route::get('/bookmarks', [BookmarksController::class, 'show']); | ||||||
|     Route::get('/guestbook', [GuestbookController::class, 'show']); |     Route::get('/guestbook', [GuestbookController::class, 'show']); | ||||||
| Route::post('/guestbook', [GuestbookController::class, 'addEntry']) |  | ||||||
|     ->middleware('rate_limit'); |  | ||||||
|     Route::get('/calculators', [CalculatorsController::class, 'show']); |     Route::get('/calculators', [CalculatorsController::class, 'show']); | ||||||
|     Route::get('/computers', [ComputersController::class, 'show']); |     Route::get('/computers', [ComputersController::class, 'show']); | ||||||
|     Route::get('/music', [MusicController::class, 'show']); |     Route::get('/music', [MusicController::class, 'show']); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | Route::post('/guestbook', [GuestbookController::class, 'addEntry']) | ||||||
|  |     ->middleware('rate_limit'); | ||||||
| 
 | 
 | ||||||
| // Admin pages
 | // Admin pages
 | ||||||
| Route::get('/admin/guestbook', [AdminGuestbookController::class, 'show']) | Route::get('/admin/guestbook', [AdminGuestbookController::class, 'show']) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Frankie B
				Frankie B