SlmGoogleAnalytics
, (*1)
Created by Jurian Sluiman, (*2)
Introduction
SlmGoogleAnalytics is a Zend Framework 2 module that enables integration with
Google Analytics., (*3)
It helps to provide the logging of pages, events and ecommerce transactions to
Google Analytics in your application. It provides a small set of tools to
configure the logging and uses a view helper to convert the configation into
javascript code for the Google tracker., (*4)
Requirements
Installation
Add "slm/google-analytics"
to your composer.json
file and update your
dependencies. Enable SlmGoogleAnalytics
in your application.config.php
., (*5)
If you do not have a composer.json
file in the root of your project, copy the
contents below and put that into a file called composer.json
and save it in
the root of your project:, (*6)
{
"require": {
"slm/google-analytics": "~0.4"
}
}
Then execute the following commands in a CLI:, (*7)
curl -s http://getcomposer.org/installer | php
php composer.phar install
Now you should have a vendor
directory, including a slm/google-analytics
. In
your bootstrap code, make sure you include the vendor/autoload.php
file to
properly load the SlmGoogleAnalytics module., (*8)
Configuration
In the /config
directory of this module (/vendor/slm/google-analytics
) you
find a file slmgoogleanalytics.global.php.dist
. Copy this file to your
/config/autoload/
directory of your application., (*9)
Open the configuration file and modify the values to your need. The minimum
requirement is to set your tracker id in the config:, (*10)
/**
* Web property ID (something like UA-xxxxx-x)
*/
'id' => '',
In the configuration, you can modify the following settings:, (*11)
- Your tracking ID
- Disable page tracking completely (useful for your local environment)
- Switch to the universal.js code instead of ga.js
- Anonymize the IP address of visitors
- Enable tracking across multiple domains
- Enable display advertising features (DoubleClick)
Usage
After configuration, SlmGoogleAnalytics should work automatically. However, there
is a PHP API available to update all above settings and perform operations for
event tracking and e-commerce., (*12)
The SlmGoogleAnalytics\Analytics\Tracker
is aliased to google-analytics
in
the Service Manager configuration. This object is used to configure the Google
Analytics tracking. You can access this object inside a controller using the locator:, (*13)
public function fooAction ()
{
$ga = $this->getServiceLocator()->get('google-analytics');
}
You can disable the tracking completely. This will result in no javascript code rendered at all:, (*14)
$ga->setEnableTracking(false);
If you want to track events and/or ecommerce transactions, but no page tracking,
you can turn off the page tracking only too:, (*15)
$ga->setEnablePageTracking(false);
Events
To track an event, you must instantiate a SlmGoogleAnalytics\Analytics\Event
and add it to the tracker:, (*16)
use SlmGoogleAnalytics\Analytics\Event;
$event = new Event('Videos', 'Play');
$ga->addEvent($event);
The constructor signature of the event is, (*17)
__construct($category, $action, $label = null, $value = null)
Transactions
To track a transaction, you should use the
SlmGoogleAnalytics\Analytics\Ecommerce\Transaction
and add one or more
SlmGoogleAnalytics\Analytics\Ecommerce\Item
objects., (*18)
use SlmGoogleAnalytics\Analytics\Ecommerce\Transaction;
use SlmGoogleAnalytics\Analytics\Ecommerce\Item;
$transaction = new Transaction('1234', '28.28');
$item = new Item('5678', '11.99', '2', 'Blue elephant');
$transaction->addItem($item);
$ga->addTransaction($transaction);
The transaction's constructor has the following signature:, (*19)
__construct($id, $total)
An item is as follows:, (*20)
__construct($sku, $price, $quantity = null, $product = null, $category = null)
The Transaction
and Item
have accessors and mutators for every property
Google is able to track (like getTax()
, getShipping()
and getSku()
) but
left out in this example for the sake of clarity., (*21)
Anonymize IP address
Some webapplications require the tracker to collect data anonymously. Google
Analytics will remove the last octet of the IP address prior to its storage.
This will reduce the accuracy of the geographic reporting, so understand the
consequences of this feature., (*22)
To collect data anonymously, set the flag in the tracker:, (*23)
$ga->setAnonymizeIp(true);
Or, alternatively, you can set this flag inside the configuration:, (*24)
'google_analytics' => array(
'anonymize_ip' => true,
),
More information about what to set in which scenario is available on the Google Help page., (*25)
Tracking multiple domains
Google Analytics offers to track statistics from multiple domain names. In order
to do so, you can set the canonical domain name and optionally allow links
between the different domains:, (*26)
$ga->setDomainName('example.com');
$ga->setAllowLinker(true);
Or, alternatively, you can set these variables inside the configuration:, (*27)
'google_analytics' => array(
'domain_name' => 'example.com',
'allow_linker' => true,
),
More information about what to set in which scenario is available on the
Google Help page., (*28)
Custom variables
The tracker is capable to track custom variables. This feature differs from events,
so check the Google Help
for more information about custom variables., (*29)
To track a variable, instantiate a SlmGoogleAnalytics\Analytics\CustomVariable
and
add it to the tracker:, (*30)
use SlmGoogleAnalytics\Analytics\CustomVariable;
$index = 1;
$name = 'Section';
$value = 'Life & Style';
$var = new CustomVariable($index, $name, $value);
$ga->addCustomVariable($var);
You can, if required, set the scope of the variable:, (*31)
$scope = CustomVariable::SCOPE_SESSION;
$var = new CustomVariable($index, $name, $value, $scope);
The scope can be SCOPE_VISITOR
, SCOPE_SESSION
or (the default) SCOPE_PAGE_LEVEL
., (*32)
Display Advertising
To enable Google Analytics Display Advertising features simply call the appropiate method on the tracker., (*33)
$ga->setEnableDisplayAdvertising(true);
Or, alternatively, you can set these variables inside the configuration:, (*34)
'google_analytics' => array(
'enable_display_advertising' => true,
),
The Google Analytics Display Advertising features include the following:, (*35)
Universal Analytics or Google Analytics
You can switch to the universal.js code instead of ga.js. the default setting is to use the ga.js style. To change to the other renderer, set a variable inside the configuration:, (*36)
/**
* Set the type of javascript to use for Google Analytics
*
* Allowed values: "google-analytics-ga" or "google-analytics-universal".
*/
'google_analytics' => array(
'script' => 'google-analytics-universal',
),
More information about Benefits of using Universal Analytics is available on this Google Developers page., (*37)
Track client side events
You can use the javascript Google Analytics API to track events. This is done via the following API:, (*38)
// ga.js
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);
// analytics.js
ga('send', 'event', 'category', 'action', 'label');
If you want to track clicks on outbound links or form submits, you might consider this approach:, (*39)
<a href="http://example.com" onclick="ga('send', 'event', 'category', 'action');">
Click me!
</a>
The problem here is that browsers kill the Google Analytics request to register this event before it is completed. The browser wants to follow the new document location http://example.com
and therefore abort all running asynchronous HTTP requests, including this Analytics tracking request. There is a method to track the event and thereafter follow the link, but this only works with the universal analytics.js. To help you with this, there is a utility Tracker.js., (*40)