Skip to content
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

Add filter for dashboard based on uri #46

Merged
merged 4 commits into from
Oct 27, 2023
Merged
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 resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel Analytics</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="min-h-screen bg-gray-100 text-gray-500 py-6 flex flex-col sm:py-16">
Expand Down
8 changes: 6 additions & 2 deletions resources/views/data/devices-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
<div>Users</div>
</div>
<div class="divide-y divide-gray-200 max-h-64 overflow-y-auto">
@foreach ($devices as $device)
@forelse ($devices as $device)
<div class="px-4 sm:px-6 py-3 flex justify-between hover:bg-gray-50">
<div class="pr-5 text-sm leading-5 text-gray-800 truncate">{{ $device->type }}</div>
<div class="text-sm leading-5 text-gray-600">{{ $device->users }}</div>
</div>
@endforeach
@empty
<div class="px-4 sm:px-6 py-3 flex justify-center hover:bg-gray-50">
<div class="text-sm leading-5 text-gray-800 truncate">No data</div>
</div>
@endforelse
</div>
</div>
42 changes: 28 additions & 14 deletions resources/views/data/filter.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
<div class="relative inline-block text-left">
<div>
<button type="button" class="inline-flex justify-center w-full rounded-md shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-gray-200" id="filter-button">
{{ $periods[$period] }}
<svg class="-mr-1 ml-2 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
</div>
<div class="flex items-center justify-end gap-2">
@if ($uri)
<div class="inline-flex items-center justify-center rounded-md shadow-sm px-4 py-2 bg-white">
<span class="text-sm font-medium text-gray-700">{{ $uri }}</span>
<a href="{{ route('analytics', ['period' => $period]) }}" class="text-gray-700 hover:text-gray-400">
<span class="sr-only">Remove</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor" class="-mr-1 ml-2 h-4 w-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</a>
</div>
@endif

<div class="relative inline-block text-left">
<div>
<button type="button" class="inline-flex justify-center rounded-md shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-gray-200" id="filter-button">
{{ $periods[$period] }}
<svg class="-mr-1 ml-2 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
</div>

<div class="origin-top-right absolute right-0 mt-2 w-40 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display: none;" id="filter-dropdown">
<div class="p-2" role="menu" aria-orientation="vertical" aria-labelledby="filter-button">
@foreach ($periods as $key => $value)
<a href="{{ url(config('analytics.prefix')) }}?period={{ $key }}" class="block px-4 py-2 rounded-lg text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">{{ $value }}</a>
@endforeach
<div class="origin-top-right absolute right-0 mt-2 w-40 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" style="display: none;" id="filter-dropdown">
<div class="p-2" role="menu" aria-orientation="vertical" aria-labelledby="filter-button">
@foreach ($periods as $key => $value)
<a href="{{ url(config('analytics.prefix')) }}?period={{ $key }}" class="block px-4 py-2 rounded-lg text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">{{ $value }}</a>
@endforeach
</div>
</div>
</div>
</div>
10 changes: 7 additions & 3 deletions resources/views/data/pages-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
<div>Users</div>
</div>
<div class="divide-y divide-gray-200 max-h-64 overflow-y-auto">
@foreach ($pages as $page)
@forelse ($pages as $page)
<div class="px-4 sm:px-6 py-3 flex justify-between hover:bg-gray-50">
<div class="pr-5 text-sm leading-5 text-gray-800 truncate">
<a href="{{ $page->page }}" target="_blank" class="hover:underline">
<a href="{{ route('analytics', ['period' => $period, 'uri' => $page->page]) }}" class="hover:underline">
{{ $page->page }}
</a>
</div>
<div class="text-sm leading-5 text-gray-600">{{ $page->users }}</div>
</div>
@endforeach
@empty
<div class="px-4 sm:px-6 py-3 flex justify-center hover:bg-gray-50">
<div class="text-sm leading-5 text-gray-800 truncate">No data</div>
</div>
@endforelse
</div>
</div>
8 changes: 6 additions & 2 deletions resources/views/data/sources-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div>Users</div>
</div>
<div class="divide-y divide-gray-200 max-h-64 overflow-y-auto">
@foreach ($sources as $source)
@forelse ($sources as $source)
<div class="px-4 sm:px-6 py-3 flex justify-between hover:bg-gray-50">
<div class="pr-5 text-sm leading-5 text-gray-800 truncate">
<div class="flex items-center">
Expand All @@ -20,6 +20,10 @@
</div>
<div class="text-sm leading-5 text-gray-600">{{ $source->users }}</div>
</div>
@endforeach
@empty
<div class="px-4 sm:px-6 py-3 flex justify-center hover:bg-gray-50">
<div class="text-sm leading-5 text-gray-800 truncate">No data</div>
</div>
@endforelse
</div>
</div>
8 changes: 6 additions & 2 deletions resources/views/data/users-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
<div>Users</div>
</div>
<div class="divide-y divide-gray-200 max-h-64 overflow-y-auto">
@foreach ($users as $user)
@forelse ($users as $user)
<div class="px-4 sm:px-6 py-3 flex justify-between hover:bg-gray-50">
<div class="pr-5 text-sm leading-5 text-gray-800 truncate">{{ $user->country }}</div>
<div class="text-sm leading-5 text-gray-600">{{ $user->users }}</div>
</div>
@endforeach
@empty
<div class="px-4 sm:px-6 py-3 flex justify-center hover:bg-gray-50">
<div class="text-sm leading-5 text-gray-800 truncate">No data</div>
</div>
@endforelse
</div>
</div>
8 changes: 6 additions & 2 deletions resources/views/data/utm-card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
<div>Count</div>
</div>
<div class="divide-y divide-gray-200 max-h-64 overflow-y-auto">
@foreach ($data['items'] as $item)
@forelse ($data['items'] as $item)
<div class="px-4 sm:px-6 py-3 flex justify-between hover:bg-gray-50">
<div class="pr-5 text-sm leading-5 text-gray-800 truncate">{{ $item['value'] }}</div>
<div class="text-sm leading-5 text-gray-600">{{ $item['count'] }}</div>
</div>
@endforeach
@empty
<div class="px-4 sm:px-6 py-3 flex justify-center hover:bg-gray-50">
<div class="text-sm leading-5 text-gray-800 truncate">No data</div>
</div>
@endforelse
</div>
</div>
28 changes: 17 additions & 11 deletions src/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@

class HomeController extends Controller
{
/** @var string */
protected $period;
protected array $scopes = [];

public function index(Request $request): View
{
$this->period = $request->get('period', 'today');
$period = $request->input('period', 'today');
$uri = $request->input('uri');

$this->scopes = [
'filter' => [$period],
'uri' => [$uri],
];

return view('analytics::dashboard', [
'period' => $this->period,
'period' => $period,
'uri' => $uri,
'periods' => $this->periods(),
'stats' => $this->stats(),
'pages' => $this->pages(),
Expand Down Expand Up @@ -48,15 +54,15 @@ protected function stats(): array
[
'key' => 'Unique Users',
'value' => PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->groupBy('session')
->pluck('session')
->count(),
],
[
'key' => 'Page Views',
'value' => PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->count(),
],
];
Expand All @@ -65,7 +71,7 @@ protected function stats(): array
protected function pages(): Collection
{
return PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->select('uri as page', DB::raw('count(*) as users'))
->groupBy('page')
->orderBy('users', 'desc')
Expand All @@ -75,7 +81,7 @@ protected function pages(): Collection
protected function sources(): Collection
{
return PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->select('source as page', DB::raw('count(*) as users'))
->whereNotNull('source')
->groupBy('source')
Expand All @@ -86,7 +92,7 @@ protected function sources(): Collection
protected function users(): Collection
{
return PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->select('country', DB::raw('count(*) as users'))
->groupBy('country')
->orderBy('users', 'desc')
Expand All @@ -96,7 +102,7 @@ protected function users(): Collection
protected function devices(): Collection
{
return PageView::query()
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->select('device as type', DB::raw('count(*) as users'))
->groupBy('type')
->orderBy('users', 'desc')
Expand All @@ -115,7 +121,7 @@ protected function utm(): Collection
'key' => $key,
'items' => PageView::query()
->select([$key, DB::raw('count(*) as count')])
->scopes(['filter' => [$this->period]])
->scopes($this->scopes)
->whereNotNull($key)
->groupBy($key)
->orderBy('count', 'desc')
Expand Down
7 changes: 7 additions & 0 deletions src/Models/PageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ public function scopeFilter($query, $period = 'today')

return $query->whereDate('created_at', today());
}

public function scopeUri($query, $uri = null)
{
$query->when($uri, function ($query, string $uri) {
$query->where('uri', $uri);
});
}
}
21 changes: 21 additions & 0 deletions tests/Feature/DashboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ public function it_can_get_data_for_30_days()
],
]);
}

/** @test */
public function it_can_get_data_for_30_days_filtered_by_uri()
{
$this->get(route('analytics', [
'period' => '30_days',
'uri' => '/test1',
]))
->assertViewHas('period', '30_days')
->assertViewHas('uri', '/test1')
->assertViewHas('stats', [
[
'key' => 'Unique Users',
'value' => 1,
],
[
'key' => 'Page Views',
'value' => 1,
],
]);
}
}
Loading