Giter Club home page Giter Club logo

universalanalytics's Introduction

Google Universal Analytics - Measurement Protocol

Master: Build Status

Develop: Build Status


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

  1. Request should have requester interface with Buzz implementation, for testing/maintainability.

universalanalytics's People

Contributors

fideloper avatar dshiells avatar lucasmichot avatar djekl avatar aronduby avatar

Watchers

 avatar

Forkers

twine-net

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.