rtcustom/laravel-analytics

一个有观点的Laravel 5包,用于检索Google Analytics数据。

dev-master 2015-09-08 14:20 UTC

This package is not auto-updated.

Last update: 2024-10-02 08:48:11 UTC


README

Latest Version Software License Build Status Quality Score Total Downloads

这是一个有观点的Laravel 5包,用于检索Google Analytics数据。

RTcustom是比利时安特卫普的一家网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

安装

此包可以通过Composer安装。

composer require RTcustom/laravel-analytics

您必须安装此服务提供者。

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

此包还包含一个门面,它提供了一个方便的方式来调用类。

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

您可以使用此命令发布此包的配置文件

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

以下配置文件将发布在 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属性并在您的网站上安装跟踪代码

此包需要有效的配置值,包括 siteIdclientIdserviceEmail。此外还需要一个 p12-file

要获得这些凭据,请先访问Google Developers Console

如果您在控制台中还没有项目,请创建一个。如果您单击项目名称,您将在左侧看到 APIs 菜单项,位于 APIs & auth 下。单击它转到“已启用API”屏幕。在此屏幕上,您应该启用Analytics API。现在,再次在 APIs & Auth-菜单下单击 Credentials。在此屏幕上,您应该按下 创建新的客户端ID。在创建屏幕上,确保您选择应用程序类型 Service Account 和密钥类型 P12-key

这将生成一个新的公钥/私钥对,并将.p12文件下载到您的计算机上。将此文件存储在配置文件中指定的位置。

在新创建的服务帐户属性中,您会找到作为 CLIENT IDEMAIL ADDRESS 列出的 serviceEmailclientId 的值。

要找到正确的 siteId 值,请登录到 Google Analytics,转到管理员部分。在属性列中选择您要获取数据的网站名称,然后在 View 列中单击 View Settings。作为以 'ga:' 开头的 View Id 提供的值可以用作 siteId

确保您已将 ANALYTICS_SERVICE_EMAIL 添加到Google Analytics帐户中,否则您将收到一个 403: 用户没有Google Analytics帐户 错误。您可以在这里阅读谷歌的说明

如果您想使用实时方法,您应该请求访问谷歌的实时报告API的beta版本Google's Real Time Reporting API

用法

安装完成后,您可以轻松检索分析数据。大部分方法都会返回一个 Illuminate\Support\Collection 实例。

以下是一个示例,用于检索过去七天内的访问者和页面浏览数据。

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

以下是一个示例,用于获取过去365天访问量最高的20个页面。

/*
* $analyticsData now contains a Collection with 2 columns: "url" and "pageViews"
*/
$analyticsData = LaravelAnalytics::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”和“页面浏览量”列的 Collection。

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

### 浏览器 这些方法返回一个包含“浏览器”和“会话”列的 Collection。

如果有更多浏览器被使用,超过了指定的 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”和“页面浏览量”列的 Collection。

    /**
     * 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 查询,请使用 performQueryGoogle 的核心报告 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。如果需要从一个项目中获取多个网站的统计数据,这可能很有用。

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

测试

使用以下命令运行测试:

vendor/bin/phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何安全相关的问题,请通过电子邮件 freek@RTcustom.be 联系,而不是使用问题跟踪器。

鸣谢

关于 RTcustom

RTcustom是比利时安特卫普的一家网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件