Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

garrettmassey's avatar

Google Analytics API with Spatie/laravel-analytics

I have been reading through the documentation for Google Analytics API for days without any luck in figuring out how to do a specific query. I am using the spatie/laravel-analytics package to access the Google Analytics API, and what I am looking to do is this:

  • Get the Total & Unique pageviews for a specific pagePath (ex: mysite.com/home)
  • Get the Total & Unique pageViews for a second specific pagePath (ex: mysite.com/home/upgrade/)

The backend I have does some calculations based on internally tracked user behavior, but in order to get a more complete picture, I need to get the two metrics listed above.

With spatie/laravel-analytics, I can get the total and unique pageViews and I can sort them into an array by pagePath like so:

$period = Period::days(7);
$metrics_total = 'ga:pageviews';
$metrics_unique = 'ga:uniquePageviews';
$dimensions = [
        'dimensions' => 'ga:pagePath',
];
$total = Analytics::performQuery($period, $metrics_total, $dimensions);
$unique = Analytics::performQuery($period, $metrics_unique, $dimensions);

And this works just fine, but I can't figure out how to properly add a dimension filter to the query so that I only get the views from the last 7 days for a specific pagePath.

Any help would be greatly appreciated, as the Google Analytics API documentation hasn't been helpful, and the spatie/laravel-analytics documentation is limited and refers users to the Google Analytics API.

0 likes
1 reply
garrettmassey's avatar
garrettmassey
OP
Best Answer
Level 6

I figured it out. The Google Analytics API uses different filter formatting for GET and POST methods. In my example, I am using the GET method with the Analytics::performQuery(). I found the documentation for the proper filtering here. After playing around with the code, my new filters look like this:

$period = Period::days(7);
$metrics_total = 'ga:pageviews';
$metrics_unique = 'ga:uniquePageviews';
$dimensions_results = [
        'dimensions' => 'ga:pagePath',
        'filters' => 'ga:pagePath==/results',
];

$dimensions_home = [
        'dimensions' => 'ga:pagePath',
        'filters' => 'ga:pagePath==/',
];

    
$total_results = Analytics::performQuery($period, $metrics_total, $dimensions_results);
$unique_results = Analytics::performQuery($period, $metrics_unique, $dimensions_results);

$total_home = Analytics::performQuery($period, $metrics_total, $dimensions_home);
$unique_home = Analytics::performQuery($period, $metrics_unique, $dimensions_home);

where the 'filters' element of the $dimensions array indicates what is being filtered, the operator, and the condition. In my case, the filter is if a page path equals a certain string. Spatie/laravel-analytics takes care of the URL encoding required, so I can type it in plain text like above. The result returns a collection instance containing the Google Analytic data requested.

Please or to participate in this conversation.