25/11/16

Retrieve data from Google Analytics

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

Because spatie/laravel-analytics package is just support PHP 7, so I need to customize it to support PHP 5.6 again.
Here are a few examples of the provided methods:
use Botble\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.
The config file stored in config/laravel-analytics.php

return [
    /*
     * The view id of which you want to display data.
     */
    'view_id' => env('ANALYTICS_VIEW_ID'),

    /*
     * Path to the json file with service account credentials. Take a look at the README of this package
     * to learn how to get this file.
     */
    'service_account_credentials_json' => storage_path('app/laravel-google-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,
];

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 click "Select a project" in the header.

Next up we must specify which API’s the project may consume. In the list of available API’s click "Google Analytics API". On the next screen click "Enable".

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".

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. Select "JSON" as the key type and click "Create" to download the JSON file.
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.

Granting permissions to your Analytics property

I'm assuming that you've already created a Analytics account on the Analytics site. Go to "User management" in the Admin-section of the property.

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. Read only access is enough.

Getting the view id

The last thing you'll have to do is fill in the view_id in the config file. You can get the right value on the Analytics site. Go to "View setting" in the Admin-section of the property.

You'll need the View ID displayed there.

Usage

When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Illuminate\Support\Collection-instance.
Here is an example to retrieve visitors and pageview data for the current day and the last seven days.
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
$analyticsData is a Collection in which each item is an array that holds keys datevisitors and pageViews
If you want to have more control over the period you want to fetch data for, you can pass a startDateand an endDate to the period object.
$startDate = Carbon::now();
$endDate = Carbon::now()->subYear();

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 datevisitorspageTitle and pageViews.

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 urlpageTitle and pageViews.

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 url and pageViews.

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 browser and sessions.

All other Google Analytics queries

To perform all other queries on the Google Analytics resource use performQueryGoogle's Core Reporting API provides more information on on which metrics and dimensions might be used.
public function performQuery(Period $period, string $metrics, array $others = [])
You can get access to the underlying Google_Service_Analytics object:
Analytics::getAnalyticsService();
Bạn đang đọc bài viết Retrieve data from Google Analytics tại Website: Học Lập Trình

0 nhận xét: