Search code examples
laravelgoogle-analyticsgoogle-analytics-api

What is the time period for "Period::days(1)" in laravel analytics?


If i make a request like this in spatie analytics:

$analyticsData = Analytics::performQuery(
    Period::days(1),
    'ga:sessions',
    [
        'metrics' => 'ga:sessions, ga:pageviews',
        'dimensions' => 'ga:yearMonth'
    ]
);

what does "days(1) refer to ? is it from 00:00 to 23:59 or from 24 hour ago until now?

i tried reading the start and end time in the result but it is not provided.


Solution

  • As you can see from the code, the period begins at the start of the day.

    public static function days(int $numberOfDays): static
    {
        $endDate = Carbon::today();
        $startDate = Carbon::today()->subDays($numberOfDays)->startOfDay();
        return new static($startDate, $endDate);
    }
    

    From the Carbon docs, today() and startOfDay() return midnight-aligned datetime values:

    $today = Carbon::today();
    echo $today;                             // 2021-07-01 00:00:00
    
    $dt = Carbon::create(2012, 1, 31, 12, 0, 0);
    echo $dt->startOfDay();                  // 2012-01-31 00:00:00