Download the PHP package p1ho/google-analytics-api without Composer
On this page you can find all versions of the php package p1ho/google-analytics-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download p1ho/google-analytics-api
More information about p1ho/google-analytics-api
Files in p1ho/google-analytics-api
Package google-analytics-api
Short Description Abstraction of Google PHP API Client to make simple and optimized requests to Google Reporting API v4
License MIT
Homepage https://github.com/p1ho/google-analytics-api
Informations about the package google-analytics-api
README
Table of Content
- Introduction
- Requirements
- Installation
- Usage
- Report Structure
- Development
- Contributors
Introduction
Google has a PHP API Library that allows making requests to Google APIs from a PHP execution, including Reporting API 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), 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 available, I should immediately be able to make requests and get my data; and thus this package was born.
Features
-
Validation: If you make an API request and you had a typo, Google would return a 400 Bad Request. In this case you didn't get your data, but from my experience it would still eat into your daily quota. This package uses cached results from metadata API 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:
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 for details.
Requirements
-
Create a Service Account 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 → Admin → User Management.
- Note: If the above seemed confusing, there is a very well written tutorial on an older Google interface.
Installation
This package is available through Composer, and can be installed by entering:
Usage
If needed, you can also add a filter expression
Note: The filter expression is the reason why I still require the ga:
prefixes, if I had taken it out, it could make writing the filters expression confusing. I could also make it so the getData() method receives optional parameters array $dimensionFilters
and array $metricFilters
, but even that could introduce confusion. If this is useful to anyone, please open an issue and let me know your preference, and why.
Report Structure
Note: Empty rows and 0 metric values are not displayed to minimize memory usage. Since users have the list of metrics used to make the requests, the 0 can be implied.
Development
-
$ composer test
will run all the tests (use$ composer test-win
on Windows). To enable coverage analysis, a code coverage driver is needed. I used Xdebug when developing on Windows. Afterwards, run$ composer phpcov-merge
(use$ composer phpcov-merge-win
on Windows) to mergebuild/cov/coverage.cov
withbuild/logs/clover.xml
as instructed on php-coveralls doc. -
Run
$ composer style-fix-download
to download the latest php-cs-fixer file to project directory. Afterwards, you can run$ composer style-fix
to auto style fix all your code. - No caching mechanism has been developed, it is expected to be taken care of by the user of this package depending on their use case.
Contributors
![]() |
---|
p1ho |