-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Click model for advanced analytics, add Setup settings, use Chart…
….js for charting, create stat pages and routes
- Loading branch information
Showing
18 changed files
with
508 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace App\Helpers; | ||
use App\Models\Click; | ||
use App\Models\Link; | ||
use Illuminate\Http\Request; | ||
|
||
class ClickHelper { | ||
static private function getCountry($ip) { | ||
// TODO | ||
return 'US'; | ||
} | ||
|
||
static private function getHost($url) { | ||
// Return host given URL; NULL if host is | ||
// not found. | ||
return parse_url($url, PHP_URL_HOST); | ||
} | ||
|
||
static public function recordClick(Link $link, Request $request) { | ||
/** | ||
* Given a Link model instance and Request object, process post click operations. | ||
* @param Link model instance $link | ||
* @return boolean | ||
*/ | ||
|
||
$ip = $request->ip(); | ||
$referer = $request->server('HTTP_REFERER'); | ||
|
||
$click = new Click; | ||
$click->link_id = $link->id; | ||
$click->ip = $ip; | ||
$click->country = self::getCountry($ip); | ||
$click->referer = $referer; | ||
$click->referer_host = ClickHelper::getHost($referer); | ||
$click->user_agent = $request->server('HTTP_USER_AGENT'); | ||
$click->save(); | ||
|
||
return true; | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
namespace App\Http\Controllers; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Redirect; | ||
|
||
use App\Models\Link; | ||
use App\Models\Clicks; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class StatsController extends Controller { | ||
private function getDayStats($link_id) { | ||
// date => x | ||
// clicks => y | ||
$stats = DB::table('clicks') | ||
->select(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') as x, count(*) as y")) | ||
->where('link_id', $link_id) | ||
->groupBy(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')")) | ||
->orderBy('x', 'asc') | ||
->get(); | ||
|
||
return $stats; | ||
} | ||
|
||
private function getCountryStats($link_id) { | ||
$stats = DB::table('clicks') | ||
->select(DB::raw("country as label, count(*) as clicks")) | ||
->where('link_id', $link_id) | ||
->groupBy('country') | ||
->orderBy('clicks', 'desc') | ||
->get(); | ||
|
||
return $stats; | ||
} | ||
|
||
private function getRefererStats($link_id) { | ||
$stats = DB::table('clicks') | ||
->select(DB::raw("COALESCE(referer_host, 'Direct') as label, count(*) as clicks")) | ||
->where('link_id', $link_id) | ||
->groupBy('referer_host') | ||
->orderBy('clicks', 'desc') | ||
->get(); | ||
|
||
return $stats; | ||
} | ||
|
||
|
||
public function displayStats(Request $request, $short_url) { | ||
if (!$this->isLoggedIn()) { | ||
return redirect(route('login'))->with('error', 'Please login to view link stats.'); | ||
} | ||
|
||
$link = Link::where('short_url', $short_url) | ||
->first(); | ||
$link_id = $link->id; | ||
|
||
// Return 404 if link not found | ||
if ($link == null) { | ||
return redirect(route('admin'))->with('error', 'Cannot show stats for nonexistent link.'); | ||
} | ||
|
||
if ( (session('username') != $link->creator) && !self::currIsAdmin() ) { | ||
return redirect(route('admin'))->with('error', 'You do not have permission to view stats for this link.'); | ||
} | ||
|
||
$day_stats = $this->getDayStats($link_id); | ||
$country_stats = $this->getCountryStats($link_id); | ||
$referer_stats = $this->getRefererStats($link_id); | ||
|
||
return view('link_stats', [ | ||
'link' => $link, | ||
'day_stats' => $day_stats, | ||
'country_stats' => $country_stats, | ||
'referer_stats' => $referer_stats | ||
]); | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
namespace App\Models; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Click extends Model { | ||
protected $table = 'clicks'; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
<?php | ||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Link extends Model { | ||
|
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 |
---|---|---|
|
@@ -5,5 +5,4 @@ | |
|
||
class User extends Model { | ||
protected $table = 'users'; | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
database/migrations/2016_12_27_232934_create_clicks_table.php
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,44 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateClicksTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('clicks', function(Blueprint $table) | ||
{ | ||
$table->engine = 'InnoDB'; | ||
|
||
$table->increments('id'); | ||
$table->string('ip'); | ||
$table->string('country')->nullable(); | ||
$table->string('referer')->nullable(); | ||
$table->string('referer_host')->nullable(); | ||
$table->text('user_agent')->nullable(); | ||
|
||
$table->index('ip'); | ||
$table->index('referer'); | ||
$table->integer('link_id')->unsigned(); | ||
$table->foreign('link_id')->references('id')->on('links')->onDelete('cascade'); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('clicks'); | ||
} | ||
} |
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,3 @@ | ||
.stats-header { | ||
text-align: center; | ||
} |
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,84 @@ | ||
polr.controller('StatsCtrl', function($scope, $compile) { | ||
$scope.dayChart = null; | ||
$scope.refererChart = null; | ||
$scope.countryChart = null; | ||
|
||
$scope.dayData = dayData; | ||
$scope.refererData = refererData; | ||
$scope.countryData = countryData; | ||
|
||
$scope.initDayChart = function () { | ||
var ctx = $("#dayChart"); | ||
$scope.dayChart = new Chart(ctx, { | ||
type: 'line', | ||
data: { | ||
datasets: [{ | ||
label: 'Clicks', | ||
data: $scope.dayData | ||
}] | ||
}, | ||
options: { | ||
scales: { | ||
xAxes: [{ | ||
type: 'time', | ||
time: { | ||
unit: 'day' | ||
} | ||
}], | ||
yAxes: [{ | ||
ticks: { | ||
min: 0 | ||
} | ||
}] | ||
} | ||
} | ||
}); | ||
}; | ||
$scope.initRefererChart = function () { | ||
// Traffic sources | ||
var ctx = $("#refererChart"); | ||
|
||
var srcLabels = []; | ||
// var bgColors = []; | ||
var bgColors = [ '#003559', '#162955', '#2E4272', '#4F628E', '#7887AB', '#b9d6f2']; | ||
var srcData = []; | ||
|
||
_.each($scope.refererData, function (item) { | ||
if (srcLabels.length > 6) { | ||
// If more than 6 referers are listed, push the seventh and | ||
// beyond into "other" | ||
srcLabels[6] = 'Other'; | ||
srcData[6] += item.clicks; | ||
bgColors[6] = 'brown'; | ||
return; | ||
} | ||
|
||
srcLabels.push(item.label); | ||
srcData.push(item.clicks); | ||
}); | ||
|
||
$scope.refererChart = new Chart(ctx, { | ||
type: 'pie', | ||
data: { | ||
labels: srcLabels, | ||
datasets: [{ | ||
data: srcData, | ||
backgroundColor: bgColors | ||
}] | ||
} | ||
}); | ||
|
||
$('#refererTable').DataTable(); | ||
}; | ||
$scope.initCountryChart = function () { | ||
|
||
}; | ||
$scope.init = function () { | ||
$scope.initDayChart(); | ||
$scope.initRefererChart(); | ||
$scope.initCountryChart(); | ||
}; | ||
|
||
$scope.init(); | ||
|
||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.