From 4835a551367f8223c754b7cba0a938a58cf515ca Mon Sep 17 00:00:00 2001 From: Richard Muvirimi Date: Fri, 6 Oct 2023 22:18:55 +0200 Subject: [PATCH] Switch Google Analytics logger --- angular.json | 12 +- app/Http/Middleware/LogApiAnalytics.php | 70 ++- composer.json | 9 +- composer.lock | 459 +++++++++++++----- config/sanctum.php | 2 +- ...2014_09_12_181218_create_zimrate_table.php | 5 +- ...3_10_03_175714_create_last_rate_column.php | 3 +- 7 files changed, 415 insertions(+), 145 deletions(-) diff --git a/angular.json b/angular.json index f12f77d..9cfd75c 100644 --- a/angular.json +++ b/angular.json @@ -37,7 +37,9 @@ "resources/js/styles.scss" ], "scripts": [ - "node_modules/safe-email/dist/safe-email.min.js" + "node_modules/safe-email/dist/safe-email.min.js", + "vendor/bootstrapdash/skydash-free-bootstrap-admin-template/template/vendors/js/vendor.bundle.base.js", + "vendor/bootstrapdash/skydash-free-bootstrap-admin-template/template/js/hoverable-collapse.js" ] }, "configurations": { @@ -46,13 +48,13 @@ "budgets": [ { "type": "initial", - "maximumWarning": "900kb", - "maximumError": "1mb" + "maximumWarning": "2mb", + "maximumError": "5mb" }, { "type": "anyComponentStyle", - "maximumWarning": "2kb", - "maximumError": "4kb" + "maximumWarning": "1mb", + "maximumError": "2mb" } ], "fileReplacements": [ diff --git a/app/Http/Middleware/LogApiAnalytics.php b/app/Http/Middleware/LogApiAnalytics.php index cd83438..c00cfe1 100644 --- a/app/Http/Middleware/LogApiAnalytics.php +++ b/app/Http/Middleware/LogApiAnalytics.php @@ -2,10 +2,14 @@ namespace App\Http\Middleware; +use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent; +use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\BaseParameter; +use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest; +use Br33f\Ga4\MeasurementProtocol\Service; use Closure; use Exception; -use Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol\Facades\GA4; use Illuminate\Http\Request; +use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Response; class LogApiAnalytics @@ -13,17 +17,67 @@ class LogApiAnalytics /** * Handle an incoming request. * - * @param Closure(Request): (Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { - try { - GA4::setClientId(request()->ip()) - ->postEvent([ - 'name' => 'api_request', + + $measurementId = env('MEASUREMENT_ID', ''); + $measurementSecret = env('MEASUREMENT_PROTOCOL_API_SECRET', ''); + + if (Str::of($measurementId)->isNotEmpty() && Str::of($measurementSecret)->isNotEmpty()) { + try { + ob_start(); + + $ip = $request->ip(); + $userAgent = $request->userAgent() ?: 'Zimrate/1.0'; + + $ga4Service = new Service($measurementSecret); + $ga4Service->setMeasurementId($measurementId); + $ga4Service->setIpOverride($ip); + $ga4Service->setOptions([ + 'User-Agent' => $userAgent, ]); - } catch (Exception) { - // Do nothing + + $baseRequest = new BaseRequest(); + $baseRequest->setClientId($ip); + + $sessionId = md5($ip . $userAgent); + + $baseRequest->setUserId($sessionId); + + $baseEvent = new BaseEvent('api_request'); + + // Create Base Event + $paramSessionId = new BaseParameter($sessionId); + $baseEvent->addParam('engagement_time_msec', $paramSessionId); + + $paramEngagementTime = new BaseParameter('100'); + $baseEvent->addParam('session_id', $paramEngagementTime); + + $baseRequest->addEvent($baseEvent); + + // Create View Page Event + $pageViewEvent = new BaseEvent('page_view'); + + $pageViewParam = new BaseParameter($request->url()); + $pageViewEvent->addParam('page_location', $pageViewParam); + + $localeParam = new BaseParameter($request->getLocale()); + $pageViewEvent->addParam('language', $localeParam); + + $titleParam = new BaseParameter($request->path()); + $pageViewEvent->addParam('page_title', $titleParam); + + $baseRequest->addEvent($pageViewEvent); + + // Send + $ga4Service->send($baseRequest); + } catch (Exception) { + //do nothing + } finally { + ob_clean(); + } } return $next($request); diff --git a/composer.json b/composer.json index 85ac72a..31dee2e 100755 --- a/composer.json +++ b/composer.json @@ -30,8 +30,11 @@ "ext-intl": "*", "ext-json": "*", "bootstrapdash/skydash-free-bootstrap-admin-template": "1.0", - "freshbitsweb/laravel-google-analytics-4-measurement-protocol": "^1.2", + "br33f/php-ga4-mp": "^0.1.3", + "doctrine/dbal": "^3.7", "guzzlehttp/guzzle": "^7.8", + "inpsyde/composer-assets-compiler": "^3.0", + "laravel/fortify": "^1.18", "laravel/framework": "^10.25", "laravel/sanctum": "^3.3", "laravel/tinker": "^2.8", @@ -40,9 +43,7 @@ "nuwave/lighthouse": "^6.21", "symfony/css-selector": "^6.3", "symfony/dom-crawler": "^6.3", - "wapmorgan/time-parser": "^2.0", - "doctrine/dbal": "^3.7", - "inpsyde/composer-assets-compiler": "^3.0" + "wapmorgan/time-parser": "^2.0" }, "require-dev": { "fakerphp/faker": "^1.23", diff --git a/composer.lock b/composer.lock index 7a8b5b4..7191c70 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,62 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2497e298aae1cf2d9b16ba8f97dc1d8f", + "content-hash": "52c0de59ba8b3fbacb9cb026578623d8", "packages": [ + { + "name": "bacon/bacon-qr-code", + "version": "2.0.8", + "source": { + "type": "git", + "url": "https://github.com/Bacon/BaconQrCode.git", + "reference": "8674e51bb65af933a5ffaf1c308a660387c35c22" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Bacon/BaconQrCode/zipball/8674e51bb65af933a5ffaf1c308a660387c35c22", + "reference": "8674e51bb65af933a5ffaf1c308a660387c35c22", + "shasum": "" + }, + "require": { + "dasprid/enum": "^1.0.3", + "ext-iconv": "*", + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "phly/keep-a-changelog": "^2.1", + "phpunit/phpunit": "^7 | ^8 | ^9", + "spatie/phpunit-snapshot-assertions": "^4.2.9", + "squizlabs/php_codesniffer": "^3.4" + }, + "suggest": { + "ext-imagick": "to generate QR code images" + }, + "type": "library", + "autoload": { + "psr-4": { + "BaconQrCode\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Ben Scholzen 'DASPRiD'", + "email": "mail@dasprids.de", + "homepage": "https://dasprids.de/", + "role": "Developer" + } + ], + "description": "BaconQrCode is a QR code generator for PHP.", + "homepage": "https://github.com/Bacon/BaconQrCode", + "support": { + "issues": "https://github.com/Bacon/BaconQrCode/issues", + "source": "https://github.com/Bacon/BaconQrCode/tree/2.0.8" + }, + "time": "2022-12-07T17:46:57+00:00" + }, { "name": "bootstrapdash/skydash-free-bootstrap-admin-template", "version": "1.0", @@ -16,6 +70,53 @@ }, "type": "library" }, + { + "name": "br33f/php-ga4-mp", + "version": "v0.1.3", + "source": { + "type": "git", + "url": "https://github.com/br33f/php-GA4-Measurement-Protocol.git", + "reference": "7fd3d60213e5955886e05a628c972b3291c0be31" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/br33f/php-GA4-Measurement-Protocol/zipball/7fd3d60213e5955886e05a628c972b3291c0be31", + "reference": "7fd3d60213e5955886e05a628c972b3291c0be31", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^6.5.5 || ^7.0.0", + "php": ">=7.1" + }, + "require-dev": { + "fakerphp/faker": "^1.14", + "php-coveralls/php-coveralls": "^2.4", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Br33f\\Ga4\\MeasurementProtocol\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Damian Zamojski", + "email": "damian.zamojski1@gmail.com" + } + ], + "description": "PHP GoogleAnalytics4 Measurement Protocol Library", + "support": { + "issues": "https://github.com/br33f/php-GA4-Measurement-Protocol/issues", + "source": "https://github.com/br33f/php-GA4-Measurement-Protocol/tree/v0.1.3" + }, + "time": "2023-03-20T20:12:12+00:00" + }, { "name": "brick/math", "version": "0.11.0", @@ -71,6 +172,56 @@ ], "time": "2023-01-15T23:15:59+00:00" }, + { + "name": "dasprid/enum", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/DASPRiD/Enum.git", + "reference": "6faf451159fb8ba4126b925ed2d78acfce0dc016" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/DASPRiD/Enum/zipball/6faf451159fb8ba4126b925ed2d78acfce0dc016", + "reference": "6faf451159fb8ba4126b925ed2d78acfce0dc016", + "shasum": "" + }, + "require": { + "php": ">=7.1 <9.0" + }, + "require-dev": { + "phpunit/phpunit": "^7 | ^8 | ^9", + "squizlabs/php_codesniffer": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "DASPRiD\\Enum\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Ben Scholzen 'DASPRiD'", + "email": "mail@dasprids.de", + "homepage": "https://dasprids.de/", + "role": "Developer" + } + ], + "description": "PHP 7.1 enum implementation", + "keywords": [ + "enum", + "map" + ], + "support": { + "issues": "https://github.com/DASPRiD/Enum/issues", + "source": "https://github.com/DASPRiD/Enum/tree/1.0.5" + }, + "time": "2023-08-25T16:18:39+00:00" + }, { "name": "dflydev/dot-access-data", "version": "v3.0.2", @@ -786,69 +937,6 @@ ], "time": "2023-01-14T14:17:03+00:00" }, - { - "name": "freshbitsweb/laravel-google-analytics-4-measurement-protocol", - "version": "1.2.0", - "source": { - "type": "git", - "url": "https://github.com/freshbitsweb/laravel-google-analytics-4-measurement-protocol.git", - "reference": "5e6c4a2cf8a61ae8aa76f0bb070e8df6a5e3f607" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/freshbitsweb/laravel-google-analytics-4-measurement-protocol/zipball/5e6c4a2cf8a61ae8aa76f0bb070e8df6a5e3f607", - "reference": "5e6c4a2cf8a61ae8aa76f0bb070e8df6a5e3f607", - "shasum": "" - }, - "require": { - "guzzlehttp/guzzle": "^6.0|^7.0", - "illuminate/http": "^7.0|^8.0|^9.0|^10.0", - "illuminate/view": "^7.0|^8.0|^9.0|^10.0", - "php": "^7.4|^8.0", - "spatie/laravel-package-tools": "^1.5" - }, - "require-dev": { - "phpunit/phpunit": "^9" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Freshbitsweb\\LaravelGoogleAnalytics4MeasurementProtocol\\GA4ServiceProvider" - ], - "aliases": { - "GA4": "Freshbitsweb\\LaravelGoogleAnalytics4MeasurementProtocol\\Facades\\GA4" - } - } - }, - "autoload": { - "psr-4": { - "Freshbitsweb\\LaravelGoogleAnalytics4MeasurementProtocol\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gaurav Makhecha", - "email": "info@freshbits.in", - "role": "Developer" - } - ], - "description": "A Laravel package to use Measurement Protocol for Google Analytics 4", - "homepage": "https://github.com/freshbitsweb/laravel-google-analytics-4-measurement-protocol", - "keywords": [ - "freshbitsweb", - "laravel-google-analytics-4-measurement-protocol" - ], - "support": { - "issues": "https://github.com/freshbitsweb/laravel-google-analytics-4-measurement-protocol/issues", - "source": "https://github.com/freshbitsweb/laravel-google-analytics-4-measurement-protocol/tree/1.2.0" - }, - "time": "2023-09-08T07:16:38+00:00" - }, { "name": "fruitcake/php-cors", "version": "v1.2.0", @@ -1552,6 +1640,70 @@ }, "time": "2023-05-16T12:51:35+00:00" }, + { + "name": "laravel/fortify", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/fortify.git", + "reference": "5af43d5cc10b70da20ddebdbe62e0dadd69c18e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/fortify/zipball/5af43d5cc10b70da20ddebdbe62e0dadd69c18e3", + "reference": "5af43d5cc10b70da20ddebdbe62e0dadd69c18e3", + "shasum": "" + }, + "require": { + "bacon/bacon-qr-code": "^2.0", + "ext-json": "*", + "illuminate/support": "^8.82|^9.0|^10.0", + "php": "^7.3|^8.0", + "pragmarx/google2fa": "^7.0|^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "^6.0|^7.0|^8.0", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Fortify\\FortifyServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Laravel\\Fortify\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Backend controllers and scaffolding for Laravel authentication.", + "keywords": [ + "auth", + "laravel" + ], + "support": { + "issues": "https://github.com/laravel/fortify/issues", + "source": "https://github.com/laravel/fortify" + }, + "time": "2023-09-12T11:19:24+00:00" + }, { "name": "laravel/framework", "version": "v10.26.2", @@ -3212,6 +3364,73 @@ ], "time": "2023-09-28T20:27:22+00:00" }, + { + "name": "paragonie/constant_time_encoding", + "version": "v2.6.3", + "source": { + "type": "git", + "url": "https://github.com/paragonie/constant_time_encoding.git", + "reference": "58c3f47f650c94ec05a151692652a868995d2938" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/58c3f47f650c94ec05a151692652a868995d2938", + "reference": "58c3f47f650c94ec05a151692652a868995d2938", + "shasum": "" + }, + "require": { + "php": "^7|^8" + }, + "require-dev": { + "phpunit/phpunit": "^6|^7|^8|^9", + "vimeo/psalm": "^1|^2|^3|^4" + }, + "type": "library", + "autoload": { + "psr-4": { + "ParagonIE\\ConstantTime\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com", + "role": "Maintainer" + }, + { + "name": "Steve 'Sc00bz' Thomas", + "email": "steve@tobtu.com", + "homepage": "https://www.tobtu.com", + "role": "Original Developer" + } + ], + "description": "Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)", + "keywords": [ + "base16", + "base32", + "base32_decode", + "base32_encode", + "base64", + "base64_decode", + "base64_encode", + "bin2hex", + "encoding", + "hex", + "hex2bin", + "rfc4648" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/constant_time_encoding/issues", + "source": "https://github.com/paragonie/constant_time_encoding" + }, + "time": "2022-06-14T06:56:20+00:00" + }, { "name": "phpoption/phpoption", "version": "1.9.1", @@ -3287,6 +3506,58 @@ ], "time": "2023-02-25T19:38:58+00:00" }, + { + "name": "pragmarx/google2fa", + "version": "v8.0.1", + "source": { + "type": "git", + "url": "https://github.com/antonioribeiro/google2fa.git", + "reference": "80c3d801b31fe165f8fe99ea085e0a37834e1be3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/antonioribeiro/google2fa/zipball/80c3d801b31fe165f8fe99ea085e0a37834e1be3", + "reference": "80c3d801b31fe165f8fe99ea085e0a37834e1be3", + "shasum": "" + }, + "require": { + "paragonie/constant_time_encoding": "^1.0|^2.0", + "php": "^7.1|^8.0" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.18", + "phpunit/phpunit": "^7.5.15|^8.5|^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "PragmaRX\\Google2FA\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Antonio Carlos Ribeiro", + "email": "acr@antoniocarlosribeiro.com", + "role": "Creator & Designer" + } + ], + "description": "A One Time Password Authentication package, compatible with Google Authenticator.", + "keywords": [ + "2fa", + "Authentication", + "Two Factor Authentication", + "google2fa" + ], + "support": { + "issues": "https://github.com/antonioribeiro/google2fa/issues", + "source": "https://github.com/antonioribeiro/google2fa/tree/v8.0.1" + }, + "time": "2022-06-13T21:57:56+00:00" + }, { "name": "psr/cache", "version": "3.0.0", @@ -4053,66 +4324,6 @@ ], "time": "2023-04-15T23:01:58+00:00" }, - { - "name": "spatie/laravel-package-tools", - "version": "1.16.1", - "source": { - "type": "git", - "url": "https://github.com/spatie/laravel-package-tools.git", - "reference": "cc7c991555a37f9fa6b814aa03af73f88026a83d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/cc7c991555a37f9fa6b814aa03af73f88026a83d", - "reference": "cc7c991555a37f9fa6b814aa03af73f88026a83d", - "shasum": "" - }, - "require": { - "illuminate/contracts": "^9.28|^10.0", - "php": "^8.0" - }, - "require-dev": { - "mockery/mockery": "^1.5", - "orchestra/testbench": "^7.7|^8.0", - "pestphp/pest": "^1.22", - "phpunit/phpunit": "^9.5.24", - "spatie/pest-plugin-test-time": "^1.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\LaravelPackageTools\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "role": "Developer" - } - ], - "description": "Tools for creating Laravel packages", - "homepage": "https://github.com/spatie/laravel-package-tools", - "keywords": [ - "laravel-package-tools", - "spatie" - ], - "support": { - "issues": "https://github.com/spatie/laravel-package-tools/issues", - "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.1" - }, - "funding": [ - { - "url": "https://github.com/spatie", - "type": "github" - } - ], - "time": "2023-08-23T09:04:39+00:00" - }, { "name": "symfony/console", "version": "v6.3.4", diff --git a/config/sanctum.php b/config/sanctum.php index 529cfdc..681f68a 100644 --- a/config/sanctum.php +++ b/config/sanctum.php @@ -17,7 +17,7 @@ 'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf( '%s%s', - 'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1', + 'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,zimrate.tyganeutronics.com', Sanctum::currentApplicationUrlWithPort() ))), diff --git a/database/migrations/2014_09_12_181218_create_zimrate_table.php b/database/migrations/2014_09_12_181218_create_zimrate_table.php index 9da1032..b6f71f0 100644 --- a/database/migrations/2014_09_12_181218_create_zimrate_table.php +++ b/database/migrations/2014_09_12_181218_create_zimrate_table.php @@ -4,13 +4,14 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ public function up(): void { - if (!Schema::hasTable('zimrate')) { + if (! Schema::hasTable('zimrate')) { Schema::create('zimrate', function (Blueprint $table) { $table->id(); $table->boolean('status')->default(false); diff --git a/database/migrations/2023_10_03_175714_create_last_rate_column.php b/database/migrations/2023_10_03_175714_create_last_rate_column.php index 6cd8abd..4c2c270 100644 --- a/database/migrations/2023_10_03_175714_create_last_rate_column.php +++ b/database/migrations/2023_10_03_175714_create_last_rate_column.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ /** * Run the migrations. */