2017 © Pedro PelĂĄez
 

library laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

image

spatie/laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

  • Friday, May 4, 2018
  • by Spatie
  • Repository
  • 70 Watchers
  • 1435 Stars
  • 401,983 Installations
  • PHP
  • 39 Dependents
  • 0 Suggesters
  • 240 Forks
  • 8 Open issues
  • 45 Versions
  • 12 % Grown

The README.md

Social Card of Laravel Analytics, (*1)

Retrieve data from Google Analytics

Latest Version MIT Licensed Check & fix styling Total Downloads, (*2)

Using this package you can easily retrieve data from Google Analytics., (*3)

Here are a few examples of the provided methods:, (*4)

use Spatie\Analytics\Facades\Analytics;
use Spatie\Analytics\Period;

//fetch the most visited pages for today and the past week
Analytics::fetchMostVisitedPages(Period::days(7));

//fetch visitors and page views for the past week
Analytics::fetchVisitorsAndPageViews(Period::days(7));

Most methods will return an \Illuminate\Support\Collection object containing the results., (*5)

Support us

, (*6)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*7)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*8)

Installation

This package can be installed through Composer., (*9)

``` bash composer require spatie/laravel-analytics, (*10)


Optionally, you can publish the config file of this package with this command: ``` bash php artisan vendor:publish --tag="analytics-config"

The following config file will be published in config/analytics.php, (*11)

return [

    /*
     * The property id of which you want to display data.
     */
    'property_id' => env('ANALYTICS_PROPERTY_ID'),

    /*
     * Path to the client secret json file. Take a look at the README of this package
     * to learn how to get this file. You can also pass the credentials as an array
     * instead of a file path.
     */
    'service_account_credentials_json' => storage_path('app/analytics/service-account-credentials.json'),

    /*
     * The amount of minutes the Google API responses will be cached.
     * If you set this to zero, the responses won't be cached at all.
     */
    'cache_lifetime_in_minutes' => 60 * 24,

    /*
     * Here you may configure the "store" that the underlying Google_Client will
     * use to store it's data.  You may also add extra parameters that will
     * be passed on setCacheConfig (see docs for google-api-php-client).
     *
     * Optional parameters: "lifetime", "prefix"
     */
    'cache' => [
        'store' => 'file',
    ],
];

How to obtain the credentials to communicate with Google Analytics

Getting credentials

The first thing you’ll need to do is to get some credentials to use Google API’s. I’m assuming that you’ve already created a Google account and are signed in. Head over to Google API’s site and select or create a project., (*12)

1, (*13)

Next up we must specify which API’s the project may consume. Go to the API Library and search for "Google Analytics Data API"., (*14)

2 3, (*15)

Choose enable to enable the API. 4, (*16)

Now that you’ve created a project that has access to the Analytics API it’s time to download a file with these credentials. Click "Credentials" in the sidebar. You’ll want to create a "Service account key". 5, (*17)

On the next screen you can give the service account a name. You can name it anything you’d like. In the service account id you’ll see an email address. We’ll use this email address later on in this guide., (*18)

6, (*19)

Go to the details screen of your created service account and select "keys", from the "Add key" dropdown select "Create new key"., (*20)

7, (*21)

Select "JSON" as the key type and click "Create" to download the JSON file., (*22)

8, (*23)

Save the json inside your Laravel project at the location specified in the service_account_credentials_json key of the config file of this package. Because the json file contains potentially sensitive information I don't recommend committing it to your git repository., (*24)

Granting permissions to your Analytics property

I'm assuming that you've already created a Analytics account on the Analytics site and are using the new GA4 properties., (*25)

First you will need to know your property ID. In Analytics, go to Settings > Property Settings. Here you will be able to copy your property ID. Use this value for the ANALYTICS_PROPERTY_ID key in your .env file., (*26)

a1, (*27)

Now we will need to give access to the service account you created. Go to "Property Access Management" in the Admin-section of the property. Click the plus sign in the top right corner to add a new user., (*28)

On this screen you can grant access to the email address found in the client_email key from the json file you download in the previous step. Analyst role is enough., (*29)

a2, (*30)

Usage

When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Illuminate\Support\Collection-instance., (*31)

Here are a few examples using periods, (*32)

use Spatie\Analytics\Facades\Analytics;

//retrieve visitors and page view data for the current day and the last seven days
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

//retrieve visitors and page views since the 6 months ago
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::months(6));

$analyticsData is a Collection in which each item is an array that holds keys date, visitors and pageViews, (*33)

If you want to have more control over the period you want to fetch data for, you can pass a startDate and an endDate to the period object., (*34)

$startDate = Carbon::now()->subYear();
$endDate = Carbon::now();

Period::create($startDate, $endDate);

Provided methods

Visitors and page views

public function fetchVisitorsAndPageViews(Period $period): Collection

The function returns a Collection in which each item is an array that holds keys activeUsers, screenPageViews and pageTitle., (*35)

Visitors and page views by date

public function fetchVisitorsAndPageViewsByDate(Period $period): Collection

The function returns a Collection in which each item is an array that holds keys date, activeUsers, screenPageViews and pageTitle., (*36)

Total visitors and pageviews

public function fetchTotalVisitorsAndPageViews(Period $period): Collection

The function returns a Collection in which each item is an array that holds keys date, date, visitors, and pageViews., (*37)

Most visited pages

public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection

The function returns a Collection in which each item is an array that holds keys fullPageUrl, pageTitle and screenPageViews., (*38)

Top referrers

public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection

The function returns a Collection in which each item is an array that holds keys screenPageViews and pageReferrer., (*39)

User Types

public function fetchUserTypes(Period $period): Collection

The function returns a Collection in which each item is an array that holds keys activeUsers and newVsReturning which can equal to new or returning., (*40)

Top browsers

public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection

The function returns a Collection in which each item is an array that holds keys screenPageViews and browser., (*41)

Top countries

public function fetchTopCountries(Period $period, int $maxResults = 10): Collection

The function returns a Collection in which each item is an array that holds keys screenPageViews and country., (*42)

Top operating systems

public function fetchTopOperatingSystems(Period $period, int $maxResults = 10): Collection

The function returns a Collection in which each item is an array that holds keys screenPageViews and operatingSystem., (*43)

All other Google Analytics queries

For all other queries you can use the get function., (*44)

public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = [], FilterExpression $dimensionFilter = null, FilterExpression $metricFilter = null): Collection

Here's some extra info on the arguments you can pass:, (*45)

Period $period: a Spatie\Analytics\Period object to indicate that start and end date for your query., (*46)

array $metrics: an array of metrics to retrieve. You can find a list of all metrics here., (*47)

array $dimensions: an array of dimensions to group the results by. You can find a list of all dimensions here., (*48)

int $limit: the maximum number of results to return., (*49)

array $orderBy: of OrderBy objects to sort the results by., (*50)

array $offset: Defaults to 0, you can use this in combination with the $limit param to have pagination., (*51)

bool $keepEmptyRows: If false or unspecified, each row with all metrics equal to 0 will not be returned. If true, these rows will be returned if they are not separately removed by a filter., (*52)

For example:, (*53)

$orderBy = [
    OrderBy::dimension('date', true),
    OrderBy::metric('pageViews', false),
];

FilterExpression $dimensionFilter: filter the result to include only specific dimension values. You can find more details here., (*54)

For example:, (*55)

use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Filter\StringFilter;
use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;

$dimensionFilter = new FilterExpression([
    'filter' => new Filter([
        'field_name' => 'eventName',
        'string_filter' => new StringFilter([
            'match_type' => MatchType::EXACT,
            'value' => 'click',
        ]),
    ]),    
]);

FilterExpression $metricFilter: filter applied after aggregating the report's rows, similar to SQL having-clause. Dimensions cannot be used in this filter. You can find more details here., (*56)

For example:, (*57)

use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Filter\NumericFilter;
use Google\Analytics\Data\V1beta\NumericValue;
use Google\Analytics\Data\V1beta\Filter\NumericFilter\Operation;

$metricFilter = new FilterExpression([
    'filter' => new Filter([
        'field_name' => 'eventCount',
        'numeric_filter' => new NumericFilter([
            'operation' => Operation::GREATER_THAN,
            'value' => new NumericValue([
                'int64_value' => 3,
            ]),
        ]),
    ]),    
]);

## Testing

Run the tests with:

``` bash
vendor/bin/pest

Changelog

Please see CHANGELOG for more information what has changed recently., (*58)

Contributing

Please see CONTRIBUTING for details., (*59)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*60)

Credits

And a special thanks to Caneco for the logo ✹, (*61)

License

The MIT License (MIT). Please see License File for more information., (*62)

The Versions

19/04 2017

v2.x-dev

2.9999999.9999999.9999999-dev https://github.com/spatie/laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

23/01 2017
23/07 2016

2.2.0

2.2.0.0 https://github.com/spatie/laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

22/06 2016

2.1.0

2.1.0.0 https://github.com/spatie/laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

20/06 2016

2.0.0

2.0.0.0 https://github.com/spatie/laravel-analytics

A Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

13/05 2016

1.4.1

1.4.1.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

12/05 2016

1.4.0

1.4.0.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

06/01 2016

1.3.1

1.3.1.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

17/11 2015

1.2.3

1.2.3.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

08/10 2015

1.2.2

1.2.2.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

29/06 2015

1.2.1

1.2.1.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

29/06 2015

1.2.0

1.2.0.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

24/06 2015

1.1.5

1.1.5.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

09/06 2015

1.1.4

1.1.4.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

22/05 2015

1.1.3

1.1.3.0 https://github.com/spatie/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

19/03 2015

1.1.2

1.1.2.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

19/03 2015

1.1.1

1.1.1.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

18/03 2015

1.1.0

1.1.0.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

16/03 2015

1.0.4

1.0.4.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

14/03 2015

1.0.3

1.0.3.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

14/03 2015

1.0.2

1.0.2.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

14/03 2015

1.0.1

1.0.1.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

13/03 2015

1.0.0

1.0.0.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie

13/03 2015

0.0.1

0.0.1.0 https://github.com/freekmurze/laravel-analytics

An opinionated Laravel 5 package to retrieve Google Analytics data.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel analytics google reports retrieve spatie