PHPackages                             sherin/google-analytics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. sherin/google-analytics

ActiveLibrary[API Development](/categories/api)

sherin/google-analytics
=======================

Google analytics reporting api v4 wrapper (supports batchGet, segments, filters and more)

v1.0.19(6y ago)42.4k1Apache-2.0PHP

Since Aug 8Pushed 6y ago1 watchersCompare

[ Source](https://github.com/SherinBloemendaal/php-google-analytics-reporting-api-v4)[ Packagist](https://packagist.org/packages/sherin/google-analytics)[ RSS](/packages/sherin-google-analytics/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (21)Used By (0)

PHP 7 - Google Analytics Reporting Api v4 wrapper
=================================================

[](#php-7---google-analytics-reporting-api-v4-wrapper)

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Limitations](#limitations)
- [Testing](#testing)
- [Support](#support)
- [License](#license)
- [FAQ](#faq)

Installation
------------

[](#installation)

**Requirements:**

- PHP ^7.0 (or higher)
- Composer ^1.9.0
- google/apiclient ^2.0
- doctrine/collections ^1.4

**Installation**Implementation in existing project with composer:

```
$ composer require sherin/google-analytics

```

Or you can start from scratch:

```
$ mkdir google-analytics-api
$ cd google-analytics-api
$ composer init
$ composer require sherin/google-analytics

```

Its not recommended/not supported to use this package without composer. It won't be documented.

Usage
-----

[](#usage)

This library is based on PHP 7.0 strict typing. Also we are using Doctrine's ArrayCollection because of the extra features.

All the libraries classes are wrapped inside the namespace: `sherin\google\analytics\`

**Workflow:**

1. Login to the Google api using the `Client` and `Credentials` classes.
2. Determine what data you want and build the query using the `QueryBuilder`.
3. If you have multiple queries, create a `Batch` object so the requests can be send in chunks of 5 (max allowed by Google).
4. Collect the response from Google, its possible to get JSON data or use the PHP objects.

**Getting started**

> If you already know what Service Credentials are and you already own a json file, you can continue at step 2

1. First you need to obtain Service Credentials from Google from [THIS](https://console.developers.google.com/iam-admin/serviceaccounts) link.

- Create a new project or use an existing project in the IAM and admin page.
- Then create a new Service Account and enter a name you want (for example `WebsiteService` or `PHPService`)
- Give the service account the role "Viewer" or "Owner". Depends if you will use other Google Libraries (you can use this key for almost all google api's)
- When you're on *Step 3* click on the *\[+ create key\]* button and select the JSON type and click *\[create\]*. Now a JSON file will be downloaded and that's your credentials file that we need.
- After that, go to API's and services and open the Library. Search for "Google Analytics Reporting API" and enable the API for your account.
- For now its recommended to move the JSON array to your `.env` file because you should NEVER commit private keys to git. You can find an example [here](https://gist.github.com/SherinBloemendaal/41b4c5051cf39189b6ebee858b0f2a9c).

2. First we need to create a new `Credentials` class that contains your credentials. You can use the setters to setup your credentials or you can use the `setFromArray` function. The `setFromArray` function requires the following array setup:

```
$credentials = new Credentials();
$credentials->setFromArray($this->getGoogleCredentials());

```

And the `getGoogleCredentials()`function example:

```
private function getGoogleCredentials(): array
{
    return [
        "type" => getenv("GOOGLE_TYPE"),
        "project_id" => getenv("GOOGLE_TYPE"),
        "private_key_id" => getenv('GOOGLE_PRIVATE_KEY_ID'),
        "private_key" => str_replace('\n', PHP_EOL, getenv('GOOGLE_PRIVATE_KEY')),  //Dirty fix because older DotEnv versions does not support multi-lined variables. (since v3.0 but only supports php 7.2.
        "client_email" => getenv('GOOGLE_CLIENT_EMAIL'),
        "client_id" => getenv('GOOGLE_CLIENT_ID'),
        "auth_uri" => getenv("GOOGLE_AUTH_URI"),
        "token_uri" => getenv("GOOGLE_TOKEN_URI"),
        "auth_provider_x509_cert_url" => getenv("GOOGLE_AUTH_PROVIDER_CERT_URL"),
        "client_x509_cert_url" => getenv('GOOGLE_CLIENT_CERT_URL')
    ];
}

```

3. Now you have created the `Credentials` class with the required authentication tokens. Now we can start create a Client.

```
$client = new Client($credentials);

```

**The following is only required if you already have a class called** `Client`You might have a duplicate declaration Client (if you use Guzzle for example). So instead of importing (`use`) the class. Call it from the namespace.

```
$client = new \sherin\google\analytics\Authentication\Client($credentials);

```

4. And finally, we create a instance of the `Analytics` api class so we can create requests to Google. We need this instance later.

```
$analytics = new Analytics($client);

```

Or if you can't `use`/`import` the class, use the following:

```
$analytics = new \sherin\google\analytics\Analytics($client);

```

Limitations
-----------

[](#limitations)

The currently limitations:

1. This library only supports *[Service Applications](https://console.developers.google.com/iam-admin/serviceaccounts)* (aka Server to Server) currently.
2. For now, a metric filter on a segment is not supported, only dimensions.

Testing
-------

[](#testing)

Testing is not added yet! *FYI: PHPUnit will be used for unit testing*

Support
-------

[](#support)

You can use the issue tracker of this project to create issues. Use stackoverflow for supporting issues. The issue tracker is only for bugs reports and feature requests. *(TODO ISSUE\_TEMPLATE.md)*

Licence
-------

[](#licence)

This project is licensed under the Apache License - see the LICENSE.md file for details

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~15 days

Recently: every ~70 days

Total

20

Last Release

2235d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6769455?v=4)[Sherin Bloemendaal](/maintainers/SherinBloemendaal)[@SherinBloemendaal](https://github.com/SherinBloemendaal)

---

Top Contributors

[![SherinBloemendaal](https://avatars.githubusercontent.com/u/6769455?v=4)](https://github.com/SherinBloemendaal "SherinBloemendaal (9 commits)")[![Jimbolino](https://avatars.githubusercontent.com/u/5860587?v=4)](https://github.com/Jimbolino "Jimbolino (2 commits)")

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/sherin-google-analytics/health.svg)

```
[![Health](https://phpackages.com/badges/sherin-google-analytics/health.svg)](https://phpackages.com/packages/sherin-google-analytics)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M719](/packages/sylius-sylius)[chartmogul/chartmogul-php

ChartMogul API PHP Client

181.5M](/packages/chartmogul-chartmogul-php)[api-platform/doctrine-common

Common files used by api-platform/doctrine-orm and api-platform/doctrine-odm

273.9M47](/packages/api-platform-doctrine-common)[rekalogika/mapper

An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.

3850.9k1](/packages/rekalogika-mapper)[tomshaw/google-api

A Laravel Google API Client.

601.7k](/packages/tomshaw-google-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
