fideloper / universalanalytics
Implement Google's Measurement Protocol for Universal Analytics on the Server Side
Installs: 18 396
Dependents: 0
Suggesters: 0
Security: 0
Stars: 21
Watchers: 6
Forks: 4
Open Issues: 1
Requires
- kriswallsmith/buzz: dev-master
Requires (Dev)
- mockery/mockery: 0.8.*
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2024-11-06 08:43:46 UTC
README
This implements Google's Measurement Protocol. This protocol is a REST api which you can use to send data from your client or server side.
This is a server-side implementation, which you might want to use to send analytics to Google that's related to your application - server metrics, exception tracking, etc.
Google developer docs can be found here.
Install
This is available via Packagist. You can install this by adding a dependency in your composer.json
file.
{ "require": { "fideloper/universalanalytics": "dev-master" }, }
Then run:
$ composer install # or composer update
Note: Composer will now install the dev dependencies by default. For this project, those are phpunit and mockery.
Basic Usage
Here is some basic usage:
<?php $ua = new \UniversalAnalytics\UA(array( 'v' => 1, 'tid' => 'UX-XXXX-XXX', 'cid' => 555, )); $event = new \UniversalAnalytics\Track\Event; $event->category = 'Video'; $event->action = 'Play'; $event->label = 'Cat Video 42'; $event->value = '0'; $request = $ua->track($event); $response = $request->send();
Alternatively, there's a slightly easier implementation:
$ua = new \UniversalAnalytics\UA(array( 'v' => 1, 'tid' => 'UX-XXXX-XXX', 'cid' => 555, )); $request = $ua->event(array( 'category' => 'Video', 'action' => 'Play', 'label' => 'Cat Video 42', 'value' => '0', ))->track(); $response = $request->send();
IoC Container
Binding to your app
You'll likely want to put this into some sort of IoC. How that looks will depend on your application. Here's an example for Laravel 4.
Set up a config file:
// app/config/ga.php <?php return array( 'trackingid' => 'UX-XXXX-XXX', );
Then in your Laravel code, perhaps a start.php
file:
App::bind('ua', function() { return new \UniversalAnalytics\UA(array( 'v' => 1, // This likely won't change anytime soon 'tid' => Config::get('ga.trackingid') )); }); // Later, somewhere in your code... $ua = App::make('ua'); $ua->clientid(Auth::user()->id); // Pass in some sort of session-based user id $request = $ua->event(array( 'category' => 'Video', 'action' => 'Play', 'label' => 'Cat Video 42', 'value' => '0', ))->track(); $response = $request->send();
To Do
- Request should have requester interface with Buzz implementation, for testing/maintainability.