rtcustom / analytics
Google Analytics
Requires
- php: >=5.4.0
- google/apiclient: 1.1.*
- illuminate/contracts: ~5.0
- illuminate/support: ~5.0
- nesbot/carbon: ~1.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is not auto-updated.
Last update: 2024-10-02 09:59:46 UTC
README
安装
此软件包可以通过Composer安装。
composer require RTcustom/analytics
您必须安装此服务提供者。
// config/app.php
'provider' => [
...
'RTcustom\Analytics\AnalyticsServiceProvider',
...
];
此软件包还包含一个门面,它提供了一种轻松调用类的方法。
// config/app.php
'aliases' => [
...
'Analytics' => 'RTcustom\Analytics\AnalyticsFacade',
...
];
您可以使用以下命令发布此软件包的配置文件
php artisan vendor:publish --provider="RTcustom\Analytics\AnalyticsServiceProvider"
以下配置文件将发布在 config/laravel-analytics.php
return
[
/*
* The siteId is used to retrieve and display Google Analytics statistics
* in the admin-section.
*
* Should look like: ga:xxxxxxxx.
*/
'siteId' => env('ANALYTICS_SITE_ID'),
/*
* Set the client id
*
* Should look like:
* xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
*/
'clientId' => env('ANALYTICS_CLIENT_ID'),
/*
* Set the service account name
*
* Should look like:
* xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com
*/
'serviceEmail' => env('ANALYTICS_SERVICE_EMAIL'),
/*
* You need to download a p12-certifciate from the Google API console
* Be sure to store this file in a secure location.
*/
'certificatePath' => storage_path('laravel-analytics/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12'),
/*
* 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.
*/
'cacheLifetime' => 60 * 24 * 2,
/*
* The amount of seconds the Google API responses will be cached for
* queries that use the real time query method. If you set this to zero,
* the responses of real time queries won't be cached at all.
*/
'realTimeCacheLifetimeInSeconds' => 5,
];
如何获取与Google Analytics通信的凭证
如果您还没有这样做,请设置一个Google Analytics属性并在您的网站上安装跟踪代码。
此软件包需要有效的配置值,包括siteId、clientId和serviceEmail。此外,还需要一个p12-file。
要获取这些凭证,请先访问Google Developers Console。
如果您在控制台中还没有项目,请创建一个。如果您单击项目名称,您将在左侧的APIs & auth下看到APIs菜单项。单击它将转到启用的API屏幕。在该屏幕上,您应该启用Analytics API。现在,再次在APIs & Auth-菜单下单击Credentials。在此屏幕上,您应该单击Create new Client ID。在创建屏幕上,请确保您选择应用程序类型Service Account和密钥类型P12-key。
这将为您生成一个新的公钥/私钥对,并且.p12文件将下载到您的计算机上。将此文件存储在软件包配置文件中指定的位置。
在新建的服务帐户属性中,您将找到作为CLIENT ID和EMAIL ADDRESS列出的serviceEmail和clientId值。
要找到siteId的正确值,请登录到Google Analytics,转到管理部分。在属性列中,选择您想检索数据的网站名称,然后在“视图”列中单击“查看设置”。带有'ga:'前缀的“视图ID”可以用作siteId。
请确保您已将ANALYTICS_SERVICE_EMAIL添加到Google Analytics帐户,否则您将收到一个403: User does not have any Google Analytics Account错误。您可以在这里阅读Google的说明。
如果您想使用实时方法,您应该请求访问Google的实时报告API的测试版Google's Real Time Reporting API。
用法
安装完成后,您可以轻松检索分析数据。大多数方法将返回一个Illuminate\Support\Collection-实例。
以下是一个示例,说明如何检索过去七天内的访问者和页面浏览数据。
/*
* $analyticsData now contains a Collection with 3 columns: "date", "visitors" and "pageViews"
*/
$analyticsData = Analytics::getVisitorsAndPageViews(7);
以下是一个示例,说明如何获取过去365天内访问次数最多的20个页面。
/*
* $analyticsData now contains a Collection with 2 columns: "url" and "pageViews"
*/
$analyticsData = Analytics::getMostVisitedPages(365, 20);
提供的方法
访问者和页面浏览量
这些方法返回一个包含“日期”、“访问者”和“页面浏览量”列的Collection。当按年月分组时,第一列将被称为“年月”。
/**
* Get the amount of visitors and pageviews
*
* @param int $numberOfDays
* @param string $groupBy Possible values: date, yearMonth
* @return Collection
*/
public function getVisitorsAndPageViews($numberOfDays = 365, $groupBy = 'date')
/**
* Get the amount of visitors and pageviews for the given period
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param string $groupBy Possible values: date, yearMonth
* @return Collection
*/
public function getVisitorsAndPageViewsForPeriod($startDate, $endDate, $groupBy = 'date')
关键词
这些方法返回一个包含“关键词”和“会话”列的Collection。
/**
* Get the top keywords
*
* @param int $numberOfDays
* @param int $maxResults
* @return Collection
*/
public function getTopKeywords($numberOfDays = 365, $maxResults = 30)
/**
* Get the top keywords for the given period
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param int $maxResults
* @return Collection
*/
public function getTopKeyWordsForPeriod($startDate, $endDate, $maxResults = 30)
引用者
这些方法返回一个包含“url”和“pageViews”列的集合。
/**
* Get the top referrers
*
* @param int $numberOfDays
* @param int $maxResults
* @return Collection
*/
public function getTopReferrers($numberOfDays = 365, $maxResults = 20)
/**
* Get the top referrers for the given period
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param $maxResults
* @return Collection
*/
public function getTopReferrersForPeriod($startDate, $endDate, $maxResults)
浏览器
这些方法返回一个包含“browser”和“sessions”列的集合。
如果使用的浏览器数量超过maxResults指定的数量,则将添加一个新结果行,其浏览器名为“其他”,并包含所有剩余浏览器的总和。
/**
* Get the top browsers
*
* @param int $numberOfDays
* @param int $maxResults
* @return Collection
*/
public function getTopBrowsers($numberOfDays = 365, $maxResults = 6)
/**
* Get the top browsers for the given period
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param $maxResults
* @return Collection
*/
public function getTopBrowsersForPeriod($startDate, $endDate, $maxResults)
最常访问的页面
这些方法返回一个包含“url”和“pageViews”列的集合。
/**
* Get the most visited pages
*
* @param int $numberOfDays
* @param int $maxResults
* @return Collection
*/
public function getMostVisitedPages($numberOfDays = 365, $maxResults = 20)
/**
* Get the most visited pages for the given period
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param int $maxResults
* @return Collection
*/
public function getMostVisitedPagesForPeriod($startDate, $endDate, $maxResults = 20)
当前活跃访客
此方法使用实时报告API。它返回现在正在查看您网站的访客数量。
/**
* Get the number of active users currently on the site
*
*/
public function getActiveUsers()
所有其他Google Analytics查询
要执行所有其他GA查询,请使用`performQuery`。有关可能使用的指标和维度的更多信息,请参阅Google的核心报告API。
/**
* Call the query method on the autenthicated client
*
* @param \DateTime $startDate
* @param \DateTime $endDate
* @param $metrics
* @param array $others
* @return mixed
*/
public function performQuery($startDate, $endDate, $metrics, $others = array())
在运行时手动设置siteId。
您也可以在运行时设置siteId。如果您需要从一个项目中获取多个网站的统计数据,这可能很有用。
Analytics::setSiteId('ga:12345')->getVisitorsAndPageViews(); // will use the given siteId
Analytics->getVisitorsAndPageViews();` // will use the siteId specified in the config file.
测试
使用以下命令运行测试:
vendor/bin/phpunit
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件freek@RTcustom.be联系,而不是使用问题跟踪器。
鸣谢
关于RTcustom
RTcustom是位于比利时安特卫普的一家网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述此处。
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。