Developed with love by KnpLabs Hire us for your project!
29

GoogleAnalyticsBundle

by Happyr

The Google Analytics Bundle lets you send data (like event tracking) to Google.

Google Analytics Bundle

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.

Usage

Read the documentation of the protocol.


//in some container aware class
$tracker = $this->get('happyr.google.analytics.tracker');
$data=array(
    'dh'=>'mydemo.com',
    'dp'=>'/home',
    'dt'=>'homepage',
);
$tracker->send($data, 'pageview');

Installation

Install with composer.

composer require happyr/google-analytics-bundle:3.0.*

Step 2: Register the bundle

Register the bundle in the AppKernel.php

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Happyr\Google\AnalyticsBundle\HappyrGoogleAnalyticsBundle(),
    // ...
);

Step 3: Configure the bundle

# app/config/config.yml

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

Fetching data

If you want to fetch data from google analytics you must install and configure GoogleSiteAuthenticatorBundle. 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 > Proterty > View > View settings.

# app/config/config.yml

happyr_google_analytics:
    // ...
    tracking_id: UA-XXXX-Y
    fetching:
        client_service: 'google.client.tobias_gmail'
        view_id: 0123456789
        cache_service: 'doctrine_cache.providers.apc_cache' # optinally a cache service
        cache_lifetime: 3600 # default

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

$fetcher = $this->get('happyr.google.analytics.data_fetcher');
$pv = $fetcher->getPageViews('/example-page');
  • Merge pull request #4 from sergiostrampelli/patch-1
    By Nyholm, 2 years ago
  • Update ClientIdProvider.php
    By , 2 years ago
  • Adding user agent
    By Nyholm, 2 years ago
  • Merge pull request #3 from chasen/patch-1
    By Nyholm, 2 years ago
  • typo
    By Nyholm, 2 years ago
  • Updated to use the latest stable guzzle version
    By chasen, 2 years ago
  • Added info about fetching data
    By Nyholm, 3 years ago
  • completed adding data fetcher
    By Nyholm, 3 years ago
  • Readding functionallity to fetch data from analytics
    By Nyholm, 3 years ago
  • Removed extra slash in url
    By Nyholm, 3 years ago
  • Whitelist instead of blacklist
    By Nyholm, 3 years ago
  • Added path to source
    By Nyholm, 3 years ago
  • Added info about the special feature
    By Nyholm, 3 years ago
  • Updated client id.
    By Nyholm, 3 years ago
  • Using the whole cookie as client ID
    By Nyholm, 3 years ago
  • PSR-2 fixes, typos in filenames, doc updates
    By Nyholm, 3 years ago
  • improved travis build. Added coveralls
    By Nyholm, 3 years ago
  • Added config and implementation for fireAndForget and requestTimeout
    By Nyholm, 3 years ago
  • Added more tests
    By Nyholm, 3 years ago
  • Added client id provider and some tests
    By Nyholm, 3 years ago
  • Bugfixes and added client Id provider
    By Nyholm, 3 years ago
  • Guzzle does not support php 5.3
    By Nyholm, 3 years ago
  • Added tests
    By Nyholm, 3 years ago
  • Added Usage
    By Nyholm, 3 years ago
  • First commit of de complete refactoring of the bunle to fit Universial analytics
    By Nyholm, 3 years ago
  • bugfixes
    By Nyholm, 3 years ago
  • Update composer.json
    By Nyholm, 3 years ago
  • Added travis and updated tests
    By Nyholm, 3 years ago
  • Update README.md
    By Nyholm, 3 years ago
  • Added "google/apiclient": "@beta"
    By Nyholm, 3 years ago