spatie / analytics-statistics
一个具有观点的PHP包,用于检索Google Analytics数据。
Requires
- php: >=5.3.0
- google/apiclient: 1.1.*
- nesbot/carbon: ^1.19
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is auto-updated.
Last update: 2024-09-21 18:36:45 UTC
README
Analytics Statistics: 从Google Analytics检索数据
一个用于检索Google Analytics数据的具有观点的包。兼容PHP >= 5.3。
如果你使用laravel 5,请查看我们的laravel-analytics包!
Spatie是比利时安特卫普的一家网络设计公司。你可以在我们的网站上找到所有开源项目的概述在这里。
支持我们
我们投入了大量资源来创建一流的开放源代码包。你可以通过购买我们的付费产品之一来支持我们。
我们非常感谢你从你的家乡寄来明信片,说明你正在使用我们的哪些包。你可以在我们的联系方式页面找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。
明信片软件
你可以自由使用这个包(它是MIT授权),但如果它进入你的生产环境,你必须向我们寄来一张来自你的家乡的明信片,说明你正在使用我们的哪些包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
最好的明信片将被发布在我们的网站上开放源代码页面。
安装
这个包可以通过Composer安装。
composer require spatie/analytics-statistics
如何获取与Google Analytics通信的凭据
如果你还没有这样做,请设置Google Analytics属性并在你的网站上安装跟踪代码。
此包需要有效的配置值,包括siteId
、clientId
和serviceEmail
。此外还需要一个p12-file
。
要获取这些凭据,首先转到Google Developers Console。
如果你在控制台中还没有项目,请创建一个。如果你点击项目名称,你将在左侧看到“APIs & auth”下的“APIs”菜单项。点击它进入“已启用的APIs”页面。在该页面上,你应该启用Analytics API。现在,再次在“APIs & Auth”菜单下点击“Credentials”。在该页面上,你应该点击“创建新的Client ID”。在创建页面上,确保选择应用程序类型为“Service Account”和密钥类型为“P12-key”。
这将生成一个新的公私钥对,.p12文件将被下载到你的机器上。将此文件存储在包的configfile中指定的位置。
在新创建的服务帐户属性中,你将找到作为“CLIENT ID”和“EMAIL ADDRESS”列出的serviceEmail
和clientId
的值。
要找到正确的 siteId
值,请登录到 Google Analytics,进入管理部分。在属性列中选择您要获取数据的网站名称,然后在 视图
列中点击 查看设置
。以 'ga:' 开头的 视图 ID
可以用作 siteId
。
请确保您已将 ANALYTICS_SERVICE_EMAIL
添加到 Google Analytics 账户,否则您将收到 403: 用户没有 Google Analytics 账户
错误。您可以在此处阅读 Google 的说明 。
如果您想使用实时方法,应 请求访问 Google 的实时报告 API 的测试版 。
用法
设置
设置 Analytics 最容易的方式是通过工厂方法 \Spatie\Analytics\Analytics::create()
。该 create
方法需要以下参数
/** * @param string $siteId * @param string $clientId * @param string $serviceEmail * @param string $certificatePath */
您可以可选地添加一些额外的参数来设置缓存
/** * @param \Spatie\Analytics\Cache $cache * @param int $cacheLifetimeInMinutes * @param int $realTimeCacheLifetime */
注意:如果您想使用缓存,您需要编写自己的实现/适配器,使用 \Spatie\Analytics\Analytics\Cache
接口。
示例
use Spatie\Analytics\Analytics; $analytics = Analytics::create( 'ga:xxxxxxxx', 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com', 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com', '/keys/analytics/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12', $myCache, 60 * 24 * 2, 5 );
设置完成后,您可以轻松地检索 Analytics 数据。方法通常返回一个普通数组。
以下是一个获取过去七天访问者和页面浏览数据的示例。
/* * $analyticsData now contains an array with 3 columns: "date", "visitors" and "pageViews" */ $analyticsData = $analytics->getVisitorsAndPageViews(7);
以下是一个获取过去 365 天访问量最高的 20 个页面的示例。
/* * $analyticsData now contains an array with 2 columns: "url" and "pageViews" */ $analyticsData = $analytics->getMostVisitedPages(365, 20);
提供的方法
访问者和页面浏览量
这些方法返回一个包含 "日期"、"访问者" 和 "页面浏览量" 列的数组。当按年月分组时,第一列将称为 "年月"。
/** * Get the amount of visitors and pageviews * * @param int $numberOfDays * @param string $groupBy Possible values: date, yearMonth * @return array */ 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 array */ public function getVisitorsAndPageViewsForPeriod($startDate, $endDate, $groupBy = 'date')
关键词
这些方法返回一个包含 "关键词" 和 "会话" 列的数组。
/** * Get the top keywords * * @param int $numberOfDays * @param int $maxResults * @return array */ 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 array */ public function getTopKeyWordsForPeriod($startDate, $endDate, $maxResults = 30)
引用者
这些方法返回一个包含 "URL" 和 "页面浏览量" 列的数组。
/** * Get the top referrers * * @param int $numberOfDays * @param int $maxResults * @return array */ public function getTopReferrers($numberOfDays = 365, $maxResults = 20) /** * Get the top referrers for the given period * * @param \DateTime $startDate * @param \DateTime $endDate * @param $maxResults * @return array */ public function getTopReferrersForPeriod($startDate, $endDate, $maxResults)
浏览器
这些方法返回一个包含 "浏览器" 和 "会话" 列的数组。
如果有更多浏览器被使用,并且超过了指定的最大结果数,则将添加一个浏览器名为 "其他" 的新结果行,其中包含所有剩余浏览器的总和。
/** * Get the top browsers * * @param int $numberOfDays * @param int $maxResults * @return array */ public function getTopBrowsers($numberOfDays = 365, $maxResults = 6) /** * Get the top browsers for the given period * * @param \DateTime $startDate * @param \DateTime $endDate * @param $maxResults * @return array */ public function getTopBrowsersForPeriod($startDate, $endDate, $maxResults)
访问量最高的页面
这些方法返回一个包含 "URL" 和 "页面浏览量" 列的数组。
/** * Get the most visited pages * * @param int $numberOfDays * @param int $maxResults * @return array */ 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 array */ public function getMostVisitedPagesForPeriod($startDate, $endDate, $maxResults = 20)
当前活跃的访问者
此方法使用 实时报告 API。它返回当前正在查看您网站的用户数量。
/** * Get the number of active users currently on the site * * @param array $others * @return array */ public function getActiveUsers($others = array())
所有其他 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())
测试
运行测试
vendor/bin/phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现有关安全的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
鸣谢
关于 Spatie
Spatie是比利时安特卫普的一家网络设计公司。你可以在我们的网站上找到所有开源项目的概述在这里。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。