DEV Community

Yakubu Damilola
Yakubu Damilola

Posted on

How to get user activity on your web application

Activity log

This is an open source laravel project that you can use to monitor whatever post activity a user create on your project (The most reputable Laravel package provided for detecting user agent is Agent developed by Jens Segers and its the one I will use. All the information you need to know about user are accessible by using Agent)

it saves the user

  • device
  • platform
  • browser
  • IP address
  • user auth id
  • user auth email
  • user activity

in your laravel project run:

composer require jenssegers/agent


Then find the config/app.php and then find providers array add:


Jenssegers\Agent\AgentServiceProvider::class,


In the same config/app.php file find the aliases array and add:

'Agent' => Jenssegers\Agent\Facades\Agent::class,

then run:


php artisan make:model ActivityLog -m

this will create 2 files

  1. ActivityLog.php in your app folder
  2. activity log migration file in your database/migration folder (something like 2020_06_21_151755_create_activity_logs_table) open your ActivityLog.php and add the following codes inside the class {...}

protected $fillable = [
'device',
'platform',
'browser',
'ip_address',
'user_id',
'user_email',
'user_activity'
];



open your migration file in 2 above and add the following lines inside the schema function after $table->id();

        $table->string('device');
        $table->string('platform');
        $table->string('browser');
        $table->string('ip_address');
        $table->string('user_id')->nullable();
        $table->string('user_email');
        $table->string('user_activity');

if you don't have an authentication scaffold already in your project run:

php artisan make:auth for laravel 5.8 and below

for laravel 6.0 and above run:
composer require laravel/ui

php artisan ui vue --auth


create a folder called "Services" in your app folder and create a file called ActivityService.php (app\services\ActivityServices.php), open your ActivityService.php file and add the following code:

<?php

namespace App\Services;
use App\ActivityLog;
use Jenssegers\Agent\Agent;
class ActivityService
{
protected $activityLog;
public function __construct(
ActivityLog $activityLog
) {
$this->activityLog = $activityLog;
}

public function enterActivity($user_activity,$email)
{
    $agent = new Agent();
    $platform = $agent->platform();
    // Ubuntu, Windows, OS X, ...
    $browser = $agent->browser();
    // Chrome, IE, Safari, Firefox, ...
    $this->activityLog->create([
        'platform' => $agent->version($platform),
        'browser' => $agent->version($browser),
        'device' => $agent->device(),
        'ip_address' => \Request::ip(),
        'user_id' => null,
        'user_email' => $email,
        'user_activity' => $user_activity
    ]);
}

}

run migration in order to create the activity log table in your DB


open RegisterController.php and call the Activity service:
use App\Services\ActivityService;

in your RegisterController class add protected $activityService;

in your __construct() method add ActivityService $activityService and in the __construct() function add $this->activityService = $activityService;
your __construct() method code should look like


protected $activityService;
public function __construct(ActivityService $activityService)
{
$this->activityService = $activityService;
$this->middleware('guest');
}
then add the below code in your create function

$user_activity = 'user attempted to login';
$email = $data['email'];
$this->activityService->enterActivity($user_activity,$email);

try to register, once you have registered, check the activity log table you will see all the details of the person
including his IP address, the $user_activity can be changed to whatever you like depending on the user activity, The service can be called anywhere in your controller, just remember to include the $user_activity and $email

below is the github link to the repo, you can fork it if you wish:

Activity log

This is an open source laravel project that you can use to monitor whatever post activity a user create on your project (The most reputable Laravel package provided for detecting user agent is Agent developed by Jens Segers and its the one I will use. All the information you need to know about user are accessible by using Agent)
it saves the user

  • device
  • platform
  • browser
  • IP address
  • user auth id
  • user auth email
  • user activity

in your laravel project run:
composer require jenssegers/agent

Then find the config/app.php and then find providers array add:

Jenssegers\Agent\AgentServiceProvider::class,

In the same config/app.php file find the aliases array and add:
'Agent' => Jenssegers\Agent\Facades\Agent::class,
then run
php artisan make:model ActivityLog -m
this will create 2 files

  1. ActivityLog.php in your app folder
  2. activity log migration file in your database/migration folder (something like 2020_06_21_151755_create_activity_logs_table) open your ActivityLog.php and add the following codes inside…

hope this helps, I can be reached via damilola.yakubu@yahoo.com

twitter: https://twitter.com/dammydev

Thanks

Top comments (0)