2017 © Pedro Peláez
 

library google-analytics

Google Analytics tracking integration for Zend Framework 2

image

slm/google-analytics

Google Analytics tracking integration for Zend Framework 2

  • Wednesday, January 17, 2018
  • by juriansluiman
  • Repository
  • 9 Watchers
  • 41 Stars
  • 52,655 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 20 Forks
  • 5 Open issues
  • 10 Versions
  • 3 % Grown

The README.md

SlmGoogleAnalytics

Build Status Latest Stable Version Scrutinizer Code Quality, (*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)

  1. Your tracking ID
  2. Disable page tracking completely (useful for your local environment)
  3. Switch to the universal.js code instead of ga.js
  4. Anonymize the IP address of visitors
  5. Enable tracking across multiple domains
  6. 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)

The Versions

15/12 2014

v0.4.2

0.4.2.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

zf2 google analytics ga

26/06 2014

v0.4.1

0.4.1.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

zf2 google analytics ga

15/06 2014

v0.4.0

0.4.0.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

zf2 google analytics ga

26/05 2014

v0.4.0-beta2

0.4.0.0-beta2 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga

06/11 2013

v0.4.0-beta1

0.4.0.0-beta1 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga

14/07 2013

v0.3.1

0.3.1.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga

27/05 2013

v0.3.0

0.3.0.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga

29/04 2013

v0.2.0

0.2.0.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga

10/03 2013

v0.1.0

0.1.0.0 https://github.com/juriansluiman/SlmGoogleAnalytics

Google Analytics tracking integration for Zend Framework 2

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 google analytics ga