rtcustom/laravel-analytics 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rtcustom/laravel-analytics

Composer 安装命令:

composer require rtcustom/laravel-analytics

包简介

An opinionated Laravel 5 package to retrieve Google Analytics data.

README 文档

README

Latest Version Software License Build Status Quality Score Total Downloads

This is an opinionated Laravel 5 package to retrieve Google Analytics data.

RTcustom is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Install

This package can be installed through Composer.

composer require RTcustom/laravel-analytics

You must install this service provider.

// config/app.php
'provider' => [
    ...
    'RTcustom\LaravelAnalytics\LaravelAnalyticsServiceProvider',
    ...
];

This package also comes with a facade, which provides an easy way to call the the class.

// config/app.php
'aliases' => [
    ...
    'LaravelAnalytics' => 'RTcustom\LaravelAnalytics\LaravelAnalyticsFacade',
    ...
];

You can publish the config file of this package with this command:

php artisan vendor:publish --provider="RTcustom\LaravelAnalytics\LaravelAnalyticsServiceProvider"

The following config file will be published in 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,
    ];

How to obtain the credentials to communicate with Google Analytics

If you haven't already done so, set up a Google Analtyics property and install the tracking code on your site.

This package needs valid configuration values for siteId, clientId and serviceEmail. Additionally a p12-fileis required.

To obtain these credentials start by going to the Google Developers Console.

If you don't have a project present in the console yet, create one. If you click on the project name, you'll see a menu item APIs under APIs & auth on the left hand side. Click it to go the the Enabled API's screen. On that screen you should enable the Analytics API. Now, again under the APIs & Auth-menu click Credentials. On this screen you should press Create new Client ID. In the creation screen make sure you select application type Service Account and key type P12-key.

This wil generate a new public/private key pair and the .p12-file will get downloaded to your machine. Store this file in the location specified in the configfile of this package.

In the properties of the newly created Service Account you'll find the values for the serviceEmail and clientId listed as CLIENT ID and EMAIL ADDRESS.

To find the right value for siteId log in to Google Analytics go the the Admin section. In the property-column select the website name of which you want to retrieve data, then click View Settings in the View-column. The value presented as View Id prepended with 'ga:' can be used as siteId.

Make sure you've added the ANALYTICS_SERVICE_EMAIL to the Google Analytics Account otherwise you will get a 403: User does not have any Google Analytics Account error. You can read Google's instructions here.

If you want to use the realtime methods you should request access to the beta version of Google's Real Time Reporting API.

Usage

When the installation is done you can easily retrieve Analytics data. Mostly all methods will return an Illuminate\Support\Collection-instance.

Here is an example to retrieve visitors and pageview data for the last seven days.

/*
* $analyticsData now contains a Collection with 3 columns: "date", "visitors" and "pageViews"
*/
$analyticsData = LaravelAnalytics::getVisitorsAndPageViews(7);

Here's another example to get the 20 most visited pages of the last 365 days

/*
* $analyticsData now contains a Collection with 2 columns: "url" and "pageViews"
*/
$analyticsData = LaravelAnalytics::getMostVisitedPages(365, 20);

Provided methods

###Visitors and pageviews These methods return a Collection with columns "date", "vistors" and "pageViews". When grouping by yearMonth, the first column will be called "yearMonth".

    /**
     * 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')

###Keywords These methods return a Collection with columns "keyword" and "sessions".

   /**
     * 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)

###Referrers These methods return a Collection with columns "url" and "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)

###Browsers These methods return a Collection with columns "browser" and "sessions".

If there are more used browsers than the number specified in maxResults, then a new resultrow with browser-name "other" will be appended with a sum of all the remaining browsers.

    /**
     * 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) 

###Most visited pages These methods return a Collection with columns "url" and "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)

###Currently active visitors This method uses the Real Time Reporting API. It returns the amount of visitors that is viewing your site right now.

    /**
     * Get the number of active users currently on the site
     *
     */
    public function getActiveUsers()

###All other Google Analytics Queries To perform all other GA queries use performQuery. Google's Core Reporting API provides more information on on which metrics and dimensions might be used.

    /**
     * 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())

###Manually setting the siteId at runtime. You can also set the siteId at runtime. This can be useful if you need to get statistics from multiple sites in one project.

 LaravelAnalytics::setSiteId('ga:12345')->getVisitorsAndPageViews(); // will use the given siteId
 LaravelAnalytics->getVisitorsAndPageViews();` // will use the siteId specified in the config file.

Testing

Run the tests with:

vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@RTcustom.be instead of using the issue tracker.

Credits

About RTcustom

RTcustom is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

rtcustom/laravel-analytics 适用场景与选型建议

rtcustom/laravel-analytics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「analytics」 「google」 「laravel」 「reports」 「retrieve」 「RTcustom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rtcustom/laravel-analytics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rtcustom/laravel-analytics 我们能提供哪些服务?
定制开发 / 二次开发

基于 rtcustom/laravel-analytics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-08