GoogleAnalyticsBundle
Work in progress, (*1)
This Symfony bundle will simplify the management of interactions with google analytics., (*2)
Installation
Install composer, (*3)
Add the following to your require block in composer.json config, (*4)
"hamaryuginh/google-analytics-bundle": "dev-master"
Then, execute the following command in your symfony poject root, (*5)
$ composer install
Configuration
Register the bundle in app/AppKernel.php
:, (*6)
public function registerBundles()
{
...
new Hamaryuginh\GoogleAnalyticsBundle\HamaryuginhGoogleAnalyticsBundle(),
...
}
Add to your app/config/config.yml
the following:, (*7)
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:, (*8)
{{ ga_initialize() }}
At the end of your document, before the </body>
tag, add:, (*9)
{{ ga_start() }}
Install assets (from your symfony project root):, (*10)
$ 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'}"
), (*11)
Page tracking, (*12)
Add data-ga-page
attribute on <body>
tag or any other tag:, (*13)
<body data-ga-page="{'page':'/home'}">
...
</body>
Or:, (*14)
<body>
...
<div data-ga-page="{'page':'/home'}"></div>
...
</body>
Or whatever you want..., (*15)
Warning! data-ga-page
must be defined only once per document !, (*16)
For further information, take a look at the Page tracking documentation., (*17)
Custom dimentions and metrics, (*18)
Add data-ga-dimension
attribute on any tag:, (*19)
<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., (*20)
Event tracking (only "click" event at the moment), (*21)
Add data-ga-event
attribute on any tag:, (*22)
<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., (*23)
Social interactions (only homemade social buttons at the moment), (*24)
Add data-ga-social
attribute on any tag:, (*25)
<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., (*26)
Last but not least, (*27)
Obviously, you can still use the Google Analytics library by the default way:, (*28)
<script type="text/javascript">
ga('set', 'dimension1', 'toto');
...
ga('send', 'event', ...);
...
ga('send', 'pageview');
</script>
Ecommerce usage
1. Start the ecommerce tracking, (*29)
Be sure to only declare this once and AFTER initializing the GA tracker (see Configuration), (*30)
{{ ga_ecommerce_initialize() }}
2. Add a transaction, (*31)
A transaction is like a basket, (*32)
{{ ga_ecommerce_addTransaction('transactionid',
'affiliation',
'totalAmount',
'shipping',
'taxRate') }}
3.Add an item to the transaction, (*33)
An item is a product in your basket. Add as many items in your transaction as you want., (*34)
{{ ga_ecommerce_addItem('transactionId',
'productName',
'sku',
'category',
'price',
'quantity') }}
4. Send the transaction to GA, (*35)
Call this after having added all the items to the transaction., (*36)
{{ ga_ecommerce_send() }}
Have fun!, (*37)
Contributions
Feel free to open an issue or add a pull request., (*38)