Coder Social home page Coder Social logo

googleanalyticsbundle's Introduction

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.

Usage

Read the documentation of the protocol.

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

Installation

Install with composer.

composer require happyr/google-analytics-bundle

Step 2: Register the bundle

Register the bundle in the AppKernel.php

<?php

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

Step 3: Configure the bundle

# app/config/config.yml

happyr_google_analytics:
    // ...
    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.

# app/config/config.yml

happyr_google_analytics:
    // ...
    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. 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.

# app/config/config.yml

happyr_google_analytics:
    // ...
    tracking_id: UA-XXXX-Y
    fetching:
        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.

$fetcher = $this->get(AnalyticsDataFetcher::class);
$pv = $fetcher->getPageViews('/example-page');

googleanalyticsbundle's People

Contributors

chasen avatar gpenverne avatar nyholm avatar sergiostrampelli avatar th3mouk avatar

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.