hamaryuginh/google-analytics-bundle

Symfony2 Google Analytics Bundle

v0.2 2017-03-11 08:37 UTC

This package is not auto-updated.

Last update: 2024-04-13 16:01:35 UTC


README

Work in progress

This Symfony bundle will simplify the management of interactions with google analytics.

Installation

Install composer

Add the following to your require block in composer.json config

"hamaryuginh/google-analytics-bundle": "dev-master"

Then, execute the following command in your symfony poject root

$ composer install

Configuration

Register the bundle in app/AppKernel.php:

public function registerBundles()
{
    ...
    new Hamaryuginh\GoogleAnalyticsBundle\HamaryuginhGoogleAnalyticsBundle(),
    ...
}

Add to your app/config/config.yml the following:

hamaryuginh_google_analytics:
    account: UA-XXXXXX-Y   # Your GA account ID
    debug:   false         # [Facultative] set to true to activate debug mode
    enabled: false         # Enable or disable tracking

Add the following before </head> tag:

{{ ga_initialize() }}

At the end of your document, before the </body> tag, add:

{{ ga_start() }}

Install assets (from your symfony project root):

$ php app/console assets:install --symlink

Usage

Globally, you define a data-ga-xxx attribute on any tag and you give arguments as JSON string (ex: data-ga-page="{'page':'/home'}")

Page tracking

Add data-ga-page attribute on <body> tag or any other tag:

<body data-ga-page="{'page':'/home'}">
  ...
</body>

Or:

<body>
  ...
  <div data-ga-page="{'page':'/home'}"></div>
  ...
</body>

Or whatever you want...

Warning! data-ga-page must be defined only once per document !

For further information, take a look at the Page tracking documentation.

Custom dimentions and metrics

Add data-ga-dimension attribute on any tag:

<body>
  ...
  <div data-ga-dimension="{'index':1, 'value':'Hello'}"></div>
  <div data-ga-dimension="{'index':2, 'value':'World!!!'}"></div>
  ...
  <div data-ga-metric="{'index':18, 'value':8000}"></div>
  <div data-ga-metric="{'index':19, 'value':24.99}"></div>
  ...
</body>

For further information, take a look at the Custom dimensions and metrics documentation.

Event tracking (only "click" event at the moment)

Add data-ga-event attribute on any tag:

<body>
  ...
  <a href="#" data-ga-event="{'eventCategory':'Link','eventAction':'click','eventLabel':'link 1'}">Home</a>
  ...
  <div data-ga-event="{'eventCategory':'Element','eventAction':'click','eventLabel':'On div'}"></div>
  ...
</body>

For further information, take a look at the Event tracking documentation.

Social interactions (only homemade social buttons at the moment)

Add data-ga-social attribute on any tag:

<body>
  ...
  <a href="http://example.com/hello" target="_blank" data-ga-social="{'socialNetwork':'facebook','socialAction':'like','socialTarget':'http://example.com/hello'}">
    <img src="facebook.png" alt="Facebook">
  </a>
  ...
</body>

For further information, take a look at the Social interactions documentation.

Last but not least

Obviously, you can still use the Google Analytics library by the default way:

<script type="text/javascript">
  ga('set', 'dimension1', 'toto');
  ...
  ga('send', 'event', ...);
  ...
  ga('send', 'pageview');
</script>

Ecommerce usage

1. Start the ecommerce tracking

Be sure to only declare this once and AFTER initializing the GA tracker (see Configuration)

{{ ga_ecommerce_initialize() }}

2. Add a transaction

A transaction is like a basket

{{ ga_ecommerce_addTransaction('transactionid',
                            'affiliation',
                            'totalAmount',
                            'shipping',
                            'taxRate') }}

3.Add an item to the transaction

An item is a product in your basket. Add as many items in your transaction as you want.

{{ ga_ecommerce_addItem('transactionId',
                        'productName',
                        'sku',
                        'category',
                        'price',
                        'quantity') }}

4. Send the transaction to GA

Call this after having added all the items to the transaction.

{{ ga_ecommerce_send() }}

Have fun!

Contributions

Feel free to open an issue or add a pull request.