-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sashagm
committed
Jun 25, 2023
1 parent
727ec5a
commit c8b5601
Showing
6 changed files
with
143 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,75 @@ | ||
<?php | ||
|
||
namespace Sashagm\Analytics\Traits; | ||
|
||
|
||
use Exception; | ||
use Closure; | ||
use Illuminate\Support\Facades\Log; | ||
use Jenssegers\Agent\Facades\Agent; | ||
use Illuminate\Support\Facades\Route; | ||
use Sashagm\Analytics\Models\Visitor; | ||
use Illuminate\Support\Facades\Cookie; | ||
use Sashagm\Analytics\Models\Statistic; | ||
|
||
|
||
trait UniqueViewsCounterTrait | ||
{ | ||
|
||
public function start($request) | ||
{ | ||
|
||
$isEnabled = config('analytics.enabled', true); | ||
$cookieLifetime = config('analytics.cookie_lifetime', 60 * 24 * 30); // 30 дней | ||
$savePeriod = config('analytics.save_period', 60 * 24 * 7); // 7 дней | ||
$ip = $_SERVER['REMOTE_ADDR']; | ||
|
||
// ... | ||
|
||
if ($isEnabled) { | ||
$routeName = $request->route()->getName(); | ||
|
||
if ($routeName && strpos($routeName, 'admin.') !== 0) { | ||
$views = Cookie::get('views') ? json_decode(Cookie::get('views'), true) : []; | ||
|
||
if (!is_null($views) && !in_array($routeName, $views)) { | ||
$views[] = $routeName; | ||
Cookie::queue('views', json_encode($views), $cookieLifetime); | ||
|
||
Visitor::create([ | ||
'category' => 'route', | ||
'value' => $routeName, | ||
'route' => Route::current()->uri(), | ||
'ip_address' => $ip, | ||
]); | ||
|
||
if (config('analytics.logger')) { | ||
Log::info("Route {$routeName} visited by {$ip}!"); | ||
} | ||
} | ||
} | ||
|
||
// сохраняем статистику раз в неделю | ||
if (time() % ($savePeriod * 60) == 0) { | ||
Statistic::create([ | ||
'category' => 'route', | ||
'data' => json_encode($views), | ||
]); | ||
|
||
if (config('analytics.logger')) { | ||
Log::info("Created logs to models Statistic!"); | ||
} | ||
} | ||
} | ||
|
||
|
||
// ... | ||
|
||
return $request; | ||
|
||
|
||
|
||
} | ||
|
||
} | ||
|
This file contains 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,49 @@ | ||
<?php | ||
|
||
namespace Sashagm\Analytics\Traits; | ||
|
||
|
||
use Exception; | ||
use Closure; | ||
use Sashagm\Analytics\Models\Visitor; | ||
use Illuminate\Support\Facades\Cookie; | ||
|
||
|
||
trait UniqueVisitorsCounterTrait | ||
{ | ||
|
||
public function starting($request) | ||
{ | ||
|
||
$ip = $_SERVER['REMOTE_ADDR']; | ||
$ua = strtolower($_SERVER['HTTP_USER_AGENT']); | ||
$isBot = preg_match('/bot|crawl|slurp|spider/i', $ua); | ||
|
||
if (!$isBot) { | ||
// обработка для обычных пользователей | ||
$user = "user"; | ||
} else { | ||
// обработка для поисковых роботов | ||
$user = "bot"; | ||
} | ||
|
||
|
||
$visitorId = Cookie::get('visitor_id'); | ||
|
||
if (!$visitorId) { | ||
$visitorId = uniqid(); | ||
Cookie::queue('visitor_id', $visitorId, 60 * 24 * 30); | ||
|
||
Visitor::create([ | ||
'category' => $user, | ||
'value' => $visitorId, | ||
'ip_address' => $ip, | ||
]); | ||
} | ||
|
||
return $request; | ||
|
||
} | ||
|
||
} | ||
|