Google Analytics Measurement Protocol library for PHP
===========================
[![Build Status](https://travis-ci.org/theiconic/php-ga-measurement-protocol.svg?branch=v2.3.0)](https://travis-ci.org/theiconic/php-ga-measurement-protocol) [![Coverage Status](https://coveralls.io/repos/theiconic/php-ga-measurement-protocol/badge.svg?branch=master&service=github)](https://coveralls.io/github/theiconic/php-ga-measurement-protocol?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/theiconic/php-ga-measurement-protocol/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/theiconic/php-ga-measurement-protocol/?branch=master) [![Latest Stable Version](https://poser.pugx.org/theiconic/php-ga-measurement-protocol/v/stable)](https://packagist.org/packages/theiconic/php-ga-measurement-protocol) [![Total Downloads](https://poser.pugx.org/theiconic/php-ga-measurement-protocol/downloads)](https://packagist.org/packages/theiconic/php-ga-measurement-protocol) [![License](https://poser.pugx.org/theiconic/php-ga-measurement-protocol/license)](https://packagist.org/packages/theiconic/php-ga-measurement-protocol) [![Documentation Status](https://readthedocs.org/projects/php-ga-measurement-protocol/badge/?version=latest)](http://php-ga-measurement-protocol.readthedocs.org/en/latest/) [![Dependency Status](https://www.versioneye.com/user/projects/54fa7f46fcd47acff1000068/badge.svg?style=flat)](https://www.versioneye.com/user/projects/54fa7f46fcd47acff1000068)
## Description
Send data to Google Analytics from the server using PHP. This library fully implements GA measurement protocol so its possible to send any data that you would usually do from analytics.js on the client side. You can send data regarding the following parameters categories [(Full List)](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters):
* General
* User
* Session
* Traffic Sources
* System Info
* Hit
* Content Information
* App Tracking
* Event Tracking
* E-Commerce
* Enhanced E-Commerce
* Social Interactions
* Timing
* Exceptions
* Custom Dimensions / Metrics
* Content Experiments
* Content Grouping
## Installation
Use Composer to install this package.
If you are using ```PHP 5.5 or above``` and ```Guzzle 6``` then:
```json
{
"require": {
"theiconic/php-ga-measurement-protocol": "^2.0"
}
}
```
Or if you are using ```PHP 5.4 or above``` and ```Guzzle 5``` then:
```json
{
"require": {
"theiconic/php-ga-measurement-protocol": "^1.1"
}
}
```
Take notice v1 won't receive more updates, you are encourage to update to v2.
## Integrations
You can use this package on its own, or use a convenient framework integration:
* Laravel 4/5 - https://github.com/irazasyed/laravel-gamp
* Yii 2 - https://github.com/baibaratsky/yii2-ga-measurement-protocol
* Symfony2 - https://github.com/fourlabsldn/GampBundle
Feel free to create an integration with your favourite framework, let us know so we list it here.
## Usage
The required parameters for all hits are Protocol Version, Tracking ID and Client ID. Some optional ones like IP Override are recommended if you don't want all hits to seem like coming from your servers.
```php
use TheIconic\Tracking\GoogleAnalytics\Analytics;
// Instantiate the Analytics object
// optionally pass TRUE in the constructor if you want to connect using HTTPS
$analytics = new Analytics(true);
// Build the GA hit using the Analytics class methods
// they should Autocomplete if you use a PHP IDE
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-26293728-11')
->setClientId('12345678')
->setDocumentPath('/mypage')
->setIpOverride("202.126.106.175");
// When you finish bulding the payload send a hit (such as an pageview or event)
$analytics->sendPageview();
```
The hit should have arrived to the GA property UA-26293728-11. You can verify this in your Real Time dashboard. Take notice, if you need GA reports to tie this event with previous user actions you must get and set the ClientId to be same as the GA Cookie. Read ([here](https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie)).
The library is 100% done, full documentation is a work in progress, but basically all parameters can be set the same way.
```php
// Look at the parameter names in Google official docs at
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
$analytics->set