Using this package you can easily retrieve data from Google Analytics.
Here are a few examples of the provided methods:
use 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.
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.
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.
This package can be installed through Composer.
composer require spatie/laravel-analytics
Optionally, you can publish the config file of this package with this command:
php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider"
The following config file will be published in config/analytics.php
return [
/*
* The view id of which you want to display data.
*/
'view_id' => env('ANALYTICS_VIEW_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',
],
];
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.
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.
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.
When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Illuminate\Support\Collection
-instance.
Here are a few examples using periods
//retrieve visitors and pageview data for the current day and the last seven days
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
//retrieve visitors and pageviews since the 6 months ago
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::months(6));
//retrieve sessions and pageviews with yearMonth dimension since 1 year ago
$analyticsData = Analytics::performQuery(
Period::years(1),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth'
]
);
$analyticsData
is a Collection
in which each item is an array that holds keys date
, visitors
and pageViews
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.
$startDate = Carbon::now()->subYear();
$endDate = Carbon::now();
Period::create($startDate, $endDate);
public function fetchVisitorsAndPageViews(Period $period): Collection
The function returns a Collection
in which each item is an array that holds keys date
, visitors
, pageTitle
and pageViews
.
public function fetchTotalVisitorsAndPageViews(Period $period): Collection
The function returns a Collection
in which each item is an array that holds keys date
, visitors
, and pageViews
.
public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection
The function returns a Collection
in which each item is an array that holds keys url
, pageTitle
and pageViews
.
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
.
public function fetchUserTypes(Period $period): Collection
The function returns a Collection
in which each item is an array that holds keys type
and sessions
.
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
.
To perform all other queries on the Google Analytics resource use performQuery
. Google's Core Reporting API provides more information 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();
Run the tests with:
vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
And a special thanks to Caneco for the logo
The MIT License (MIT). Please see License File for more information.
github地址:https://github.com/irazasyed/laravel-gamp 1.简介 Laravel GAMP:适用于Laravel和Lumen的Google Analytics(分析)测量协议包。 从Laravel / Lumen向Google Analytics(分析)发送数据。 支持所有GA Measurement Protocol API方法。 2.快
安装elasticsearch-rtf,github上下载安装即可,前提必须安装java包。 接下来安装laravel扩展Scout、Scout-elastic 首先,我们通过 Composer 包管理器来安装 Scout: composer require laravel/scout 接下来,需要添加 ScoutServiceProvider 到配置文件 config/app.php 的pro
name: The name of the package. It consists of vendor name and project name, separated by / description: A short description of the package. Usually this is just one line long keywords: An array of key
Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr
我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))
Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap
简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer
简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果
WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D