Skip to content

Commit

Permalink
Add database flag to disable analytics for certain URLs
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
NFarrington committed Aug 20, 2019
1 parent f1683a8 commit 61b2fe2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/UrlController.php
Expand Up @@ -53,7 +53,7 @@ public function redirect(Request $request, $prefix = null, $shortUrl = null)
throw new NotFoundHttpException();
}

request()->session()->flash('short.url_id', $url->id);
request()->session()->flash('short.url', $url);

return redirect()->to($url->redirect_url);
}
Expand Down
36 changes: 21 additions & 15 deletions app/Http/Middleware/LogRequests.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Middleware;

use App\Models\Url;
use App\Models\UrlAnalytics;
use Auth;
use Closure;
Expand Down Expand Up @@ -40,21 +41,26 @@ public function handle($request, Closure $next)
*/
public function terminate(Request $request, Response $response)
{
$serverData = $this->filterFillable($_SERVER);
UrlAnalytics::create([
'user_id' => Auth::check() ? Auth::user()->id : null,
'url_id' => $request->session()->pull('short.url_id'),
'request_time' => $_SERVER['REQUEST_TIME'],
'http_host' => $request->root(),
'http_referer' => $request->headers->get('referer'),
'http_user_agent' => $request->userAgent(),
'remote_addr' => $request->ip(),
'request_uri' => $request->path(),
'get_data' => $_GET,
'post_data' => $_POST,
'custom_headers' => array_diff_key($this->getHeaders(), $serverData),
'response_code' => $response->getStatusCode(),
]);
/** @var Url $url */
$url = $request->session()->pull('short.url');

if (!$url->analytics_disabled) {
$serverData = $this->filterFillable($_SERVER);
UrlAnalytics::create([
'user_id' => Auth::check() ? Auth::user()->id : null,
'url_id' => $url->id,
'request_time' => $_SERVER['REQUEST_TIME'],
'http_host' => $request->root(),
'http_referer' => $request->headers->get('referer'),
'http_user_agent' => $request->userAgent(),
'remote_addr' => $request->ip(),
'request_uri' => $request->path(),
'get_data' => $_GET,
'post_data' => $_POST,
'custom_headers' => array_diff_key($this->getHeaders(), $serverData),
'response_code' => $response->getStatusCode(),
]);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Url.php
Expand Up @@ -14,6 +14,7 @@
* @property bool $prefix
* @property string $url
* @property string $redirect_url
* @property int $analytics_disabled
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
Expand All @@ -29,6 +30,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url public()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url query()
* @method static bool|null restore()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereAnalyticsDisabled($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereDomainId($value)
Expand Down
6 changes: 6 additions & 0 deletions database/factories/UrlFactory.php
Expand Up @@ -32,3 +32,9 @@
'user_id' => null,
];
});

$factory->state(Url::class, 'analytics_disabled', function (Faker $faker) {
return [
'analytics_disabled' => true,
];
});
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddAnalyticsDisabledToUrlsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('urls', function (Blueprint $table) {
$table->boolean('analytics_disabled')->default(0)->after('redirect_url');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('urls', function (Blueprint $table) {
$table->dropColumn('analytics_disabled');
});
}
}
15 changes: 15 additions & 0 deletions tests/Feature/UrlTest.php
Expand Up @@ -78,4 +78,19 @@ function short_url_clicks_are_logged()
'response_code' => 302,
]);
}

/** @test */
function short_url_clicks_for_ignored_urls_are_not_logged()
{
$domain = create(Domain::class, ['url' => config('app.url')]);
$loggedUrl = create(Url::class, ['domain_id' => $domain->id]);
$ignoredUrl = factory(Url::class)->states('analytics_disabled')
->create(['domain_id' => $domain->id]);

$this->get(route('short-url', $loggedUrl->url));
$this->get(route('short-url', $ignoredUrl->url));
$this->assertDatabaseMissing((new UrlAnalytics())->getTable(), [
'request_uri' => $ignoredUrl->url,
]);
}
}

0 comments on commit 61b2fe2

Please sign in to comment.