Google Analytics Bundle

Latest Version Software License Build Status Code Coverage Quality Score Total Downloads

GoogleAnalyticsBundle is a Symfony2 bundle that helps you to push data to Google Analytics. It could be data like pageview, events etc. It is a bundle implementation of the Measurement Protocol

This is not a library to pull data from Google analytics. That feature is deprecated from the classic-analytics branch. The master branch and version 3 and above will be supporting Google analytics universal.

Special feature

This bundle has a special feature. Say that you want to post data to Analytics. You want to post an event every time someone downloads a file. You may do that from the server like any other library. When looking at the reports you will find that you are missing the information about the actual user for the download event. You can not use a segment to find out which referal the user came from.

This bundle helps you with just that. Before we submit any data we look at the _ga cookie to find the user's clientId. So now you may use segments and advanced queries to analytics and you will get the expected result.


Read the documentation of the protocol.

<br />//in some container aware class
$tracker = $this->get('happyr.google_analytics.tracker');
$tracker->send($data, 'pageview');


Install with composer.

composer require happyr/google-analytics-bundle

Step 2: Register the bundle

Register the bundle in the AppKernel.php


// in AppKernel::registerBundles()

    // ...
    new Happyr\GoogleAnalyticsBundle\HappyrGoogleAnalyticsBundle(),
    // ...

### Step 3: Configure the bundle

``` yaml
# app/config/config.yml

    // ...
    tracking_id: UA-XXXX-Y

### Step 4: Provide a HTTP client and message factory service. 

You need to provide two services to that know how to create and send message factories. The services must implement
`Http\Message\RequestFactory` and `Http\Client\HttpClient`. If you use [HTTPlugBundle](
 this will be taken care of automatically. You will aslo get some nice logging features. 

``` yaml
# app/config/config.yml

    // ...
    http_client: 'httplug.client'
    http_message_factory: 'httplug.message_factory

## Fetching data

If you want to fetch data from Google Analytics you must install and configure [GoogleSiteAuthenticatorBundle][siteAuth]. Read its documentaion and then configure the analytics bundle with a `client service` and a `view id`. The `view id` is found in the admin section on Google analytics. Go to Admin > Account > Property > View > View settings. 

``` yaml
# app/config/config.yml

    // ...
    tracking_id: UA-XXXX-Y
        client_service: 'google.client.tobias_gmail'
        view_id: 0123456789
        cache_service: 'cache.provider.my_memcached' # optinally a PSR6 cache service
        cache_lifetime: 3600 # default

You may then run the following code to get the page views for /example-page.

``` php

$pv = $fetcher->getPageViews('/example-page');

Configuration reference

Property Description
profile_id The google analytics profile id. This is not the same as the tracking code.
token_file_path The path where to save a temporary token.
host This is your domain name.
tracker_enabled Boolean true if the tracker should be enabled. You might want to set this to false in the develop environment.
tracker_id This is your google analytics tracker id. Like “UA-XXXXXXXX-X”
tracker.requestTimeout Seconds (float allowed) to wait until timeout when connecting to the Google analytics endpoint host. Default value is 1.
tracker.sendOnShutdown Whether to just queue all requests on HttpRequest::fire() and actually send them on PHP script shutdown after all other tasks are done. This has two advantages:

  1. It effectively doesn’t affect app performance
  2. It can e.g. handle custom variables that were set after scheduling a request

Default value is false.

tracker.fireAndForget Whether to make asynchronous requests to GA without waiting for any response (speeds up doing requests). Default value is false.
tracker.anonymizeIpAddresses Whether to anonymize IP addresses within Google Analytics by stripping the last IP address block, will be mapped to “aip” parameter. Default value is false.
cache.cache_lifetime How many seconds should we cache the result. Default value is 3600.
cache.service Which cache service should we use? A cache service must implement the HappyR\Google\AnalyticsBundle\CacheServices\CacheInterface. You may specify “doctrine” to and use the cache.doctrine_class to use the doctrine cache. Default value a dummy cache service.
cache.doctrine_class If cache.service is “doctrine” you may specify a class that implements Doctrine\Common\Cache\CacheProvider. Default value is “Doctrine\Common\Cache\ApcCache”.


The source code is hosted onĀ GitHub.