PHPackages                             p1ho/google-analytics-api - 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. p1ho/google-analytics-api

AbandonedArchivedLibrary[API Development](/categories/api)

p1ho/google-analytics-api
=========================

Abstraction of Google PHP API Client to make simple and optimized requests to Google Reporting API v4

1.0.2(7y ago)0860[1 issues](https://github.com/p1ho/google-analytics-api/issues)MITPHP

Since Mar 31Pushed 7y agoCompare

[ Source](https://github.com/p1ho/google-analytics-api)[ Packagist](https://packagist.org/packages/p1ho/google-analytics-api)[ Docs](https://github.com/p1ho/google-analytics-api)[ RSS](/packages/p1ho-google-analytics-api/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

README
======

[](#readme)

[![Build Status](https://camo.githubusercontent.com/2beb16358de65001e650b1e58b052d1b4f98ff1fb8876a0d42a8c4d8ff65d294/68747470733a2f2f7472617669732d63692e636f6d2f7031686f2f676f6f676c652d616e616c79746963732d6170692e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/p1ho/google-analytics-api)[![Coverage Status](https://camo.githubusercontent.com/fbc9f1b815e0a31615ac85144ba6d3266c8aa5e47f28f4a2df7adf58603cd824/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7031686f2f676f6f676c652d616e616c79746963732d6170692f62616467652e737667)](https://coveralls.io/github/p1ho/google-analytics-api)

Table of Content
================

[](#table-of-content)

- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Report Structure](#report-structure)
- [Development](#development)
- [Contributors](#contributors)

Introduction
============

[](#introduction)

Google has a [PHP API Library](https://github.com/googleapis/google-api-php-client) that allows making requests to Google APIs from a PHP execution, including [Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/) which enables access to Google Analytics data. However, while the library is quite powerful, it can be unintuitive as users have to create objects from the library ([Google's Tutorial for PHP](https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php)), which takes a lot of digging in the documentation.

As someone who had to use this library extensively, I wanted something that's more intuitive. Specifically, if I'm already familiar with Google Analytics and the [dimensions and metrics](https://developers.google.com/analytics/devguides/reporting/core/dimsmets) available, I should immediately be able to make requests and get my data; and thus this package was born.

Features
--------

[](#features)

- **Validation**: If you make an API request and you had a typo, Google would return a [400 Bad Request](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400). In this case you didn't get your data, but from my experience it would still eat into your [daily quota](https://developers.google.com/analytics/devguides/reporting/core/v4/limits-quotas#analytics_reporting_api_v4). This package uses cached results from [metadata API](https://developers.google.com/analytics/devguides/reporting/metadata/v3/) which helps with offline validation. (Note: some dimensions and metrics cannot be queried together, this validator as of now does not address that)
- **Optimize Requests**: Google API forces users to break apart requests:

    - [A batchGet() request can take 5 subrequests](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#request-body).
    - [A subrequest can have 10 metrics](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#ReportRequest.FIELDS.metrics)

    This means it can be difficult to aggregate the requests needed for a batchGet() call. If done incorrectly, this can lead to wasting space (e.g., having a lower metrics count than 10 in a subrequest). This package optimizes the packaging process of building a `batchGet()` request so no space is wasted in a single request.
- **Intuitive Reports**: Just as this package helps you aggregate the requests, it also reorganizes the fragmented reports Google returned so it's structured minimally and intuitively. See [Report Structure](#report-structure) for details.

Requirements
============

[](#requirements)

- [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount) and store the downloaded `json` file somewhere secure. This is needed to make calls to Google API.
- Add the created Service Account Email to your Google Analytics view. You can do this by going to your [Analytics Homeapge](https://analytics.google.com) → Admin → User Management.
- Note: If the above seemed confusing, there is a [very well written tutorial](https://github.com/spatie/laravel-analytics#how-to-obtain-the-credentials-to-communicate-with-google-analytics) on an older Google interface.

Installation
============

[](#installation)

This package is available through [Composer](https://getcomposer.org/), and can be installed by entering:

```
$ composer require p1ho/google-analytics-api
```

Usage
=====

[](#usage)

```
