Skip to content

Commit

Permalink
Merge branch 'uniqueness'
Browse files Browse the repository at this point in the history
  • Loading branch information
epalmans committed Aug 5, 2022
2 parents 41b18f9 + c53178b commit 782f1f4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
'enabled' => true,

/*
|--------------------------------------------------------------------------
| Unique visits
|--------------------------------------------------------------------------
|
| When enabled, only unique page-visits per session are persisted.
|
*/
'only_unique' => false,

/*
|--------------------------------------------------------------------------
| Site View Model
Expand Down
5 changes: 3 additions & 2 deletions migrations/0000_00_00_000000_create_site_views_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
Schema::create('site_views', function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable()->index();

$table->string('request_method', 16);
$table->string('url', 2000);
Expand All @@ -27,8 +27,9 @@ public function up()
$table->string('http_referer', 2000)->nullable();
$table->string('http_user_agent')->nullable();
$table->string('http_accept_language', 64)->nullable();
$table->string('locale', 8)->index();
$table->string('locale', 8);
$table->string('ip')->nullable()->index();
$table->boolean('unique');

$table->timestamp('requested_at')->nullable();
$table->integer('app_time');
Expand Down
1 change: 1 addition & 0 deletions src/SiteView.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SiteView extends Eloquent
protected $casts = [
'requested_at' => 'datetime',
'created_at' => 'datetime',
'unique' => 'boolean',
];

protected $guarded = [];
Expand Down
37 changes: 23 additions & 14 deletions src/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ protected function collectVisitData()
'http_accept_language' => $request->server('HTTP_ACCEPT_LANGUAGE'),
'locale' => $this->app->getLocale(),
'ip' => $request->ip(),
'unique' => $this->isViewUniqueForSession(),
'requested_at' => Carbon::createFromTimestamp($request->server('REQUEST_TIME')),
];
}
Expand All @@ -138,23 +139,31 @@ protected function collectVisitData()
*/
public function saveCurrent()
{
if ($this->saveEnabled() && $this->isViewValid() && $this->isViewUnique()) {
$success = $this->saveCurrentModel();
if (! $this->saveEnabled() || ! $this->isViewValid()) {
return false;
}

// Keep on only if the model save has succeeded
if ($success) {
$this->storeCurrentHash();
$isUnique = $this->isViewUniqueForSession();

if ($this->config->get('tracker.only_unique', false) && ! $isUnique) {
return false;
}

$success = $this->saveCurrentModel();

$this->saveTrackables(
$this->getCurrent(),
$success
);
// Keep on only if the model save has succeeded
if ($success) {
if ($isUnique) {
$this->storeCurrentHash();
}

return $success;
$this->saveTrackables(
$this->getCurrent(),
$success
);
}

return false;
return $success;
}

/**
Expand Down Expand Up @@ -225,11 +234,11 @@ public function getBotFilter()
}

/**
* Checks if the current request is unique
* Checks if the current request is unique for the session
*
* @return bool
*/
public function isViewUnique()
public function isViewUniqueForSession()
{
$hash = $this->getCurrentHash();

Expand All @@ -251,7 +260,7 @@ protected function getCurrentHash()
$this->currentHash = md5(
$this->request->fullUrl().
$this->request->method().
$this->request->getClientIp()
$this->request->ip()
);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function resetDatabase()
Schema::create('site_views', function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable()->index();

$table->string('request_method', 16);
$table->string('url', 2000);
Expand All @@ -54,8 +54,9 @@ protected function resetDatabase()
$table->string('http_referer', 2000)->nullable();
$table->string('http_user_agent')->nullable();
$table->string('http_accept_language', 64)->nullable();
$table->string('locale', 8)->index();
$table->string('locale', 8);
$table->string('ip')->nullable()->index();
$table->boolean('unique');

$table->timestamp('requested_at')->nullable();
$table->integer('app_time');
Expand Down
33 changes: 30 additions & 3 deletions tests/TrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Palmans\Tracker\Tests;

use Illuminate\Support\Facades\Config;
use Palmans\Tracker\SiteView;
use Palmans\Tracker\Tracker;
use Prophecy\PhpUnit\ProphecyTrait;

class TrackerTest extends TestBase
{
use ProphecyTrait;

/**
* Setup test
*/
Expand Down Expand Up @@ -76,13 +79,13 @@ public function it_checks_if_the_current_view_is_unique()
$this->get('/home');

$this->assertTrue(
$tracker->isViewUnique()
$tracker->isViewUniqueForSession()
);

$tracker->saveCurrent();

$this->assertFalse(
$tracker->isViewUnique()
$tracker->isViewUniqueForSession()
);
}

Expand Down Expand Up @@ -115,19 +118,43 @@ public function it_saves_the_current_view()
}

/** @test */
public function it_saves_only_if_unique()
public function it_saves_only_once_if_uniqueness_configured()
{
$this->app->config->set('tracker.only_unique', true);

$tracker = $this->getTracker();

$this->assertTrue(
$tracker->saveCurrent()
);

// $this->assertTrue($tracker->getCurrent()->unique);

$this->assertFalse(
$tracker->saveCurrent()
);
}

/** @test */
public function it_saves_if_uniqueness_not_configured()
{
$this->app->config->set('tracker.only_unique', false);

$tracker = $this->getTracker();

$this->assertTrue(
$tracker->saveCurrent()
);

// $this->assertTrue($tracker->getCurrent()->unique);

$this->assertTrue(
$tracker->saveCurrent()
);

// $this->assertFalse($tracker->getCurrent()->unique);
}

/** @test */
public function it_adds_and_saves_trackables()
{
Expand Down

0 comments on commit 782f1f4

Please sign in to comment.