/** * DashboardController constructor. */ public function __construct() { parent::__construct(); $end = Carbon::now()->minute(0); $this->limit = 16; $this->period = Period::create($end->copy()->subDays(30), $end); $this->country = env('ANALYTICS_COUNTRY'); }
/** * Execute the console command. * * @return mixed */ public function handle() { if (empty(config('laravel-analytics.view_id'))) { return; } Analytics::fetchTotalVisitorsAndPageViews(Period::days(14)); Analytics::fetchTotalVisitorsAndPageViews(Period::days(365)); Analytics::fetchMostVisitedPages(Period::days(365)); Analytics::fetchTopReferrers(Period::days(365)); Analytics::fetchTopBrowsers(Period::days(365)); $this->info('Analytics data prefetched!'); }
public function index() { $logItems = $this->getLatestActivityItems(); $view = view('back.dashboard.index')->with(compact('logItems')); if (empty(config('laravel-analytics.view_id'))) { return $view; } $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(14)); $dates = $analyticsData->pluck('date'); $visitors = $analyticsData->pluck('visitors'); $pageViews = $analyticsData->pluck('pageViews'); return $view->with(compact('dates', 'visitors', 'pageViews')); }
public function index() { if (empty(config('laravel-analytics.view_id'))) { return view('back.statistics.notconfigured'); } $visitors = Analytics::fetchTotalVisitorsAndPageViews(Period::days(365))->groupBy(function (array $visitorStatistics) { return $visitorStatistics['date']->format('Y-m'); })->map(function ($visitorStatistics, $yearMonth) { list($year, $month) = explode('-', $yearMonth); return ['date' => "{$month}-{$year}", 'visitors' => $visitorStatistics->sum('visitors'), 'pageViews' => $visitorStatistics->sum('pageViews')]; })->values(); $pages = Analytics::fetchMostVisitedPages(Period::days(365)); $referrers = Analytics::fetchTopReferrers(Period::days(365)); $browsers = Analytics::fetchTopBrowsers(Period::days(365)); return view('back.statistics.index')->with(compact('visitors', 'pages', 'referrers', 'browsers')); }
/** * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function getStatistics(Request $request) { // we check the current user permission if ($permission_denied = Permission::hasPermissionJson('dashboard.statistics')) { return response()->json(['message' => [$permission_denied]], 401); } // we replace the FR dates by database dates $request->merge(['start_date' => Carbon::createFromFormat('d/m/Y H:i', $request->start_date)->format('Y-m-d H:i:s'), 'end_date' => Carbon::createFromFormat('d/m/Y H:i', $request->end_date)->format('Y-m-d H:i:s')]); // we sanitize the entries $request->replace(InputSanitizer::sanitize($request->all())); // we check inputs validity $rules = ['start_date' => 'required|date|date_format:Y-m-d H:i:s', 'end_date' => 'required|date|date_format:Y-m-d H:i:s']; if (is_array($errors = Validation::check($request->all(), $rules, true))) { return response(['message' => $errors], 401); } // we set the google analytics period $start = new DateTime(Carbon::createFromFormat('Y-m-d H:i:s', $request->start_date)); $end = new DateTime(Carbon::createFromFormat('Y-m-d H:i:s', $request->end_date)); $period = Period::create($start, $end); $statistics = []; // we get the visitprs and page views data $visitors_and_page_views = Analytics::fetchVisitorsAndPageViews($period); $visitors_and_page_views_sorted_by_date = []; $formatted_visitors_and_page_views_data = []; foreach ($visitors_and_page_views as $detail) { $visitors_and_page_views_sorted_by_date[$detail['date']->format('d/m/Y')][] = $detail; } $dates = array_keys($visitors_and_page_views_sorted_by_date); foreach ($dates as $date) { $visitors = 0; $page_views = 0; foreach ($visitors_and_page_views_sorted_by_date[$date] as $data) { $visitors += $data['visitors']; $page_views += $data['pageViews']; } $formatted_visitors_and_page_views_data[$date] = ['date' => $date, 'visitors' => $visitors, 'page_views' => $page_views]; } $statistics['visitors_and_page_views'] = $formatted_visitors_and_page_views_data; // we get the most visited pages $statistics['most_visited_pages'] = Analytics::fetchMostVisitedPages($period); // we get the top referrers $statistics['top_referrers'] = Analytics::fetchTopReferrers($period, 10); // we get the top browsers $statistics['top_browsers'] = Analytics::fetchTopBrowsers($period); // we get the other stats $start_date = Carbon::createFromFormat('Y-m-d H:i:s', $request->start_date); $end_date = Carbon::createFromFormat('Y-m-d H:i:s', $request->end_date); $registration_form_download_repo = app(RegistrationFormDownloadRepositoryInterface::class); $qr_code_repository = app(QrCodeScanRepositoryInterface::class); $other_stats = []; $referrer = clone $start_date; do { $other_stats[] = ['date' => $referrer->format('d/m/Y'), 'form_downloads' => $registration_form_download_repo->getModel()->whereBetween('created_at', [$referrer->startOfDay()->format('Y-m-d H:i:s'), $referrer->endOfDay()->format('Y-m-d H:i:s')])->count(), 'qr_code_scans' => $qr_code_repository->getModel()->whereBetween('created_at', [$referrer->startOfDay()->format('Y-m-d H:i:s'), $referrer->endOfDay()->format('Y-m-d H:i:s')])->count()]; $stop = $referrer->lt($end_date); $referrer->addDay(); } while ($stop); $statistics['other_stats'] = $other_stats; return response()->json($statistics, 200); }