Skip to content

Commit

Permalink
Merge pull request #26 from richard-muvirimi/development
Browse files Browse the repository at this point in the history
Switch Google Analytics logger
  • Loading branch information
richard-muvirimi committed Oct 6, 2023
2 parents 471d9db + 4835a55 commit eb3f59b
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 145 deletions.
12 changes: 7 additions & 5 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": [
Expand Down
70 changes: 62 additions & 8 deletions app/Http/Middleware/LogApiAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,82 @@

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
{
/**
* 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);
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Loading

0 comments on commit eb3f59b

Please sign in to comment.