Skip to content

Added browser and OS info on clicks #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/ClickUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ClickUrl extends Model
* @var array
*/
protected $fillable = [
'short_url', 'click', 'real_click', 'country', 'country_full', 'referer', 'ip_address', 'ip_hashed', 'ip_anonymized',
'short_url', 'click', 'real_click', 'country', 'country_full', 'user_agent', 'browser', 'browser_version', 'os', 'referer', 'ip_address', 'ip_hashed', 'ip_anonymized',
];

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('browscap:update')
->weekly();
}

/**
Expand Down
36 changes: 35 additions & 1 deletion app/Http/Controllers/ClickUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use GeoIp2\Database\Reader;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use BrowscapPHP\Browscap;
use BrowscapPHP\Exception;
use Doctrine\Common\Cache\FilesystemCache;
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;

/**
* Controller handling the actual page of the corresponding URL, that redirects the user.
Expand Down Expand Up @@ -77,17 +81,24 @@ public function click($url)
$real_click = 0;
}

$userAgent = request()->server('HTTP_USER_AGENT');
$browser = $this->getBrowser($userAgent);

$data = [
'short_url' => $url,
'click' => $click,
'real_click' => $real_click,
'country' => $countries['countryCode'],
'country_full' => $countries['countryName'],
'user_agent' => $userAgent,
'browser' => $browser['browser'],
'browser_version' => $browser['browserVersion'],
'os' => $browser['os'],
'referer' => $referer ?? null,
'ip_address' => $ip,
'ip_hashed' => $hashed,
'ip_anonymized' => $anonymized,
];
];

ClickUrl::store($data);

Expand All @@ -113,4 +124,27 @@ public function getCountries($ip)
return compact('countryCode', 'countryName');
}
}

protected function getBrowser($userAgent)
{
$fileCache = new FilesystemCache(config('browscap.cache'));
$cache = new SimpleCacheAdapter($fileCache);
$browscap = new Browscap($cache, logger()->driver());

try {
$result = $browscap->getBrowser($userAgent);
} catch (Exception $e) {
return [
'browser' => null,
'browserVersion' => null,
'os' => null
];
}

return [
'browser' => $result->browser,
'browserVersion' => $result->version,
'os' => $result->platform
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"laravel/passport": "^7.2",
"laravel/tinker": "^1.0",
"phpunit/phpunit": "8",
"propa/laravel-browscap": "^2.0",
"simplesoftwareio/simple-qrcode": "^2.0",
"spatie/laravel-honeypot": "^1.3",
"yajra/laravel-datatables-oracle": "~9.0"
Expand Down
Loading