承接 educare/analytics 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

educare/analytics

Composer 安装命令:

composer require educare/analytics

包简介

Get whatever data you want from google analytics.

README 文档

README

This package helps php developers to use Google Analytics API V4 with convenient way. The code is clean and was written with good OOP practices. Any help to improve this package would be appreciated.

The package is compatible with laravel framework.

Installation

Install the package via composer:

composer require educare/analytics

To use it with laravel add the GoogleAnalyticsServiceProvider to the config/app.php:

'providers' => [
...
Educare\Analytics\GoogleAnalyticsServiceProvider::class
]

If you want to use the facade of the package add it to the config/app.php:

'aliases' => [
...
'Analytics' => Educare\Analytics\Facades\Analytics::class
]

Copy the analytics config file with the command:

php artisan vendor:publish --provider="Educare\Analytics\GoogleAnalyticsServiceProvider"

For those who have a credential with google analytics api continue here otherwise look at how to create credential with google analytics api.

Be sure that you have service-account-credentials.json file within storage\app\google-analytics\.

Add the view id to .env file:

GOOGLE_ANALYTICS_VIEW_ID=324235464

Usage

You can get analytics data simply using the facade

Educare\Analytics\Facades\Analytics

To get all sessions of the last week

Analytics::get();

To get all sessions depends on the specific date range
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::get();
To get whatever data you want
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::setMaxResults(20);
Analytics::setMetrics(['ga:entrances', 'ga:pageviews', 'ga:bounceRate']);
Analytics::setDimension(['ga:pagePath', 'ga:pageTitle']);
Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '/i-want-to-get-all-data-that-has-this-page-path');
Analytics::setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
return Analytics::get();

setMetrics and setDimension methods accept an array containing the wanted metrics and dimension. Available metrics and dimensions

setDimensionFilter accept 3 parameters. First parameter get the dimension name in which you want to filter analytics data. Second parameter get the operator (REGEXP, BEGINS_WITH, ENDS_WITH and more) you want. Third parameter get the expression. For example if you want to get all analytics data in which the page path include play or simple words you can use: Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '(\/play\/|\/simple\/)');

If you want to get analytics data for multiple pages in a single request by their exact paths, you can use the 'IN_LIST' operator and pass an array of paths as the third parameter. E.g. Analytics::setDimensionFilter('ga:pagePath', 'IN_LIST', ['/i-want-data-for-this-path', '/and/this-path-too']);

setOrder method accept 3 parameters. First parameter get the name in which you want to order the data. Second get OrderType usually VALUE. Third get the SortOrder usually ASCENDING or DESCENDING.

Get data using Analytics contract

Educare\Analytics\Contracts\Analytics

Instead of facade you can get analytics data using the Analytics interface:

use Educare\Analytics\Contracts\Analytics;

class GoogleAnalyticsController
{
    //inject analytics interface
    public function get(Analytics $analytics)
    {
        $analytics->setDateRange('2016-12-01', '2016-12-20');
        $analytics->setMaxResults(11);
        $analytics->setMetrics(['ga:pageviews', 'ga:uniquePageviews', 'ga:avgTimeOnPage', 'ga:entrances', 'ga:bounceRate']);
        $analytics->setDimension(['ga:pagePath', 'ga:pageTitle']);
        $analytics->setDimensionFilter('ga:pagePath', 'REGEXP', '(\/this-value-in-path\/|\/or-this-value-in-path\/)');
        $analytics->setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
        return $analytics->get();
    }
}    

Create credential with google analytics api v4

Guide from google analytics api v4: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php

  • Create a project using Google API Console:
  • Open the Service accounts page. If prompted, select your project that you have created and click open.
  • Click Create service account.
  • In the Create service account window, type a name for the service account, and select Furnish a new private key. In the key type select JSON and then click create. If you want to grant G Suite domain-wide authority to the service account, also select Enable G Suite Domain-wide Delegation.
  • Copy Service account ID you have created and then go to google analytics admin panel select User Management and add the account you have copied with the permissions that you want.
  • Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
  • Rename the json file to service-account-credentials.json.
  • To get the view id you can use the account explorer or from google analytics view settings from admin panel.

educare/analytics 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-09