spatie / analytics-reports
Requires
- php: >=5.4.0
- google/apiclient: 1.1.*
- illuminate/support: 4.*|5.*
- thujohn/analytics: ^1.0
README
🚨 此包已被放弃 🚨
我们不再在我们自己的项目中使用此包,也无法再证明维护它所需的时间。这就是我们选择放弃它的原因。您可以自由地复制我们的代码并维护自己的副本。
从 Google Analytics 获取数据
这是一个用于检索 Google Analytics 数据的 Laravel 4 包。
如果您使用的是 Laravel 5,请查看此包。
Spatie 是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 在这里。
Postcardware
您可以使用此包(它是 MIT 许可的),但如果它进入您的生产环境,您需要向我们寄一张来自您家乡的明信片,说明您正在使用我们的哪个包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
最好的明信片将被发布在我们的网站上的开源页面。
安装
此包可以通过 Composer 安装。
composer require spatie/analytics-reports
您必须安装此服务提供者。
// app/config/app.php 'providers' => [ '...', 'Spatie\AnalyticsReports\AnalyticsReportsServiceProvider' ];
此包还提供了一个门面,它提供了一个方便的方式来调用类。
// app/config/app.php 'aliases' => array( ... 'AnalyticsReports' => 'Spatie\AnalyticsReports\AnalyticsReportsFacade', )
尽管此包的 composer.json 指定必须同时拉取 "google/apiclient" 和 "thujohn/analytics",但它有时会失败。如果您也遇到这个问题,请将以下行包含在您的 composer.json 中
{ "require": { ... "google/apiclient" : "1.1.*", "thujohn/analytics": "dev-master", ... } }
您可以使用 artisan 发布包的配置文件
php artisan config:publish spatie/analytics-reports
配置文件发布后,您需要手动将其移动到您的应用程序的配置文件夹。(希望这个步骤在下一个版本中不再需要)
要将配置文件移动,请将 config.php
文件从
/app/config/packages/spatie/analytics-reports/
复制到您的 /app/config
文件夹,并将其重命名为 analyticsReports.php
在配置文件中,您可以指定两个值
- siteId:Google 网站ID,形式为 ga:xxxxxxxx
- cacheLifeTime:Google API 响应的缓存时间(以分钟为单位)。如果您将此值设置为零,则不会缓存任何响应。
内部此包使用 thujohn/analytics-l4 与 Google 进行身份验证。因此,为了使用此包,您还必须遵循 他们的安装说明。
用法
安装完成后,您可以轻松检索 Analytics 数据。大多数方法将返回一个 Illuminate\Support\Collection
实例。
以下是一个示例,用于检索过去七天内的访客和页面浏览数据。
/* * $analyticsData now contains a Collection with 3 columns: "date", "visitors" and "pageViews" */ $analyticsData = AnalyticsReports::getVisitorsAndPageViews(7)
以下是一个示例,用于获取过去365天访问量最高的20个页面。
/* * $analyticsData now contains a Collection with 2 columns: "url" and "pageViews" */ $analyticsData = AnalyticsReports::getMostVisitedPages(365, 20)
提供的方法
###访客和页面浏览量这些方法返回一个包含“日期”、“访客”和“页面浏览量”列的集合。当按年月分组时,第一列将被称为“年月”。
/** * 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')
###关键词这些方法返回一个包含“关键词”和“会话”列的集合。
/** * 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”和“页面浏览量”列的集合。
/** * 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)
###浏览器这些方法返回一个包含“浏览器”和“会话”列的集合。
如果使用的浏览器数量超过了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”和“页面浏览量”列的集合。
/** * 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)
###所有其他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())
##便利方法getSiteIdByUrl
可以用于获取给定URL的网站ID
/** * Returns the site id (ga:xxxxxxx) for the given url * * @param $url * @throws \Exception * @return string */ public function getSiteIdByUrl($url)
关于Spatie
Spatie 是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 在这里。