samedi 13 août 2016

Laravel 5.1 visitor counter doesn't count the correct visits

I have a self-made visitor counter (sort of) I'm using the pragmarx plugin, but getting the data by myself.

So I have a chart, wich looks like this: chart

When I count everything from the day, there is no problem, it returns the correct values. But when I try to get the count of visits of each week, month or year, it returns the totally wrong numbers.

This is how I get my chart:

public function getVisitors()
    {
        $begin = Carbon::now()->startOfMonth();
        $end = Carbon::now()->endOfMonth();

        $stats = DB::table('tracker_sessions')
            ->whereRaw("created_at between '$begin' and '$end'")
            ->where('is_robot', '=', 0)
            ->groupBy(DB::raw('DATE_FORMAT(created_at,"%d-%m-%Y")'))
            ->orderBy(DB::raw('DATE_FORMAT(created_at,"%d-%m-%Y")'))
            ->get([
            DB::raw('DATE_FORMAT(created_at,"%d-%m-%Y") as y'),
            DB::raw('COUNT(DISTINCT client_ip) as b')
          ]
        );

          return $stats;
    }

And this is how I get everything by week:

public static function getVisitorsThisWeek()
    {
        $begin = Carbon::now()->startOfWeek();
        $end = Carbon::now()->endOfWeek();

        $stats = DB::table('tracker_sessions')
            ->whereRaw("created_at between '$begin' and '$end'")
            ->where('is_robot', '=', 0)
            ->count(DB::raw('DISTINCT client_ip'));

        return $stats;
    }

I'm doing the same for the month and the year, except then I change the Carbon::now()->startOfWeek(); to the Year or Month type of Carbon.

I really don't know what I'm doing wrong right now? I already tried to add the groupBy and orderBy, or switch them and so on, but noting seems to work. Hopefully someone can help me out with this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire